summaryrefslogtreecommitdiff
path: root/harnesses/host_controller/build/build_provider_ab.py
blob: 3debdb4aac39b64395bac43da1115c6c720b47eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#
# Copyright (C) 2017 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import logging
import os

from host_controller.build import build_provider
from vts.utils.python.build.api import artifact_fetcher


class BuildProviderAB(build_provider.BuildProvider):
    """A build provider for Android Build (AB)."""

    def __init__(self):
        super(BuildProviderAB, self).__init__()
        if 'run_ab_key' in os.environ:
            logging.info(
                "For AB, use the key at %s", os.environ['run_ab_key'])
            self._artifact_fetcher = artifact_fetcher.AndroidBuildClient(
                os.environ['run_ab_key'])
        else:
            self._artifact_fetcher = None

    def GetLatestBuildId(self, branch, target):
        """Get the latest build id.

        Args:
            branch: string, android branch to pull resource from.
            target: string, build target name.

        Returns:
            string, latest build id. None if _artifact_fetcher is not initialized.
        """
        if not self._artifact_fetcher:
            return None

        recent_build_ids = self._artifact_fetcher.ListBuildIds(
            branch, target)

        return recent_build_ids[0]

    def Fetch(self,
              branch,
              target,
              artifact_name,
              build_id="latest",
              full_device_images=False):
        """Fetches Android device artifact file(s) from Android Build.

        Args:
            branch: string, android branch to pull resource from.
            target: string, build target name.
            artifact_name: string, file name.
            build_id: string, ID of the build or latest.

        Returns:
            a dict containing the device image info.
            a dict containing the test suite package info.
            a dict containing the artifact info.
        """
        fetch_info = {}
        fetch_info["build_id"] = None

        if not self._artifact_fetcher:
            return self.GetDeviceImage(), self.GetTestSuitePackage(), fetch_info

        if build_id == "latest":
            build_id = self.GetLatestBuildId(branch, target)
        fetch_info["build_id"] = build_id

        if "{build_id}" in artifact_name:
            artifact_name = artifact_name.replace("{build_id}", build_id)

        dest_filepath = os.path.join(self.tmp_dirpath, artifact_name)
        self._artifact_fetcher.DownloadArtifactToFile(
            branch, target, build_id, artifact_name,
            dest_filepath=dest_filepath)

        self.SetFetchedFile(dest_filepath, full_device_images)

        return self.GetDeviceImage(), self.GetTestSuitePackage(), fetch_info