summaryrefslogtreecommitdiff
path: root/init.py
blob: 944b9e8ea8d15d1224ce056bee6b2d9accaed66c (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env python3
#
# Copyright (C) 2024 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.

"""Downloads and run the appropriate DDK init script."""

import argparse
import io
import json
import logging
import os
import pathlib
import shutil
import subprocess
import sys
import tempfile
from typing import BinaryIO
import urllib.error
import urllib.request

# Googlers: go/android-build-api-getting-started
_API_ENDPOINT_PREFIX = (
    "https://androidbuildinternal.googleapis.com/android/internal/build/v3/"
)
_ARTIFACT_URL_FMT = (
    _API_ENDPOINT_PREFIX
    # pylint: disable-next=line-too-long
    + "builds/{build_id}/{build_target}/attempts/latest/artifacts/{filename}/url?redirect=true"
)
_BUILD_IDS_URL_FMT = (
    _API_ENDPOINT_PREFIX
    # pylint: disable-next=line-too-long
    + "builds?branch={branch}&target={build_target}&buildAttemptStatus=complete&buildType=submitted&maxResults=1&fields=builds.buildId"
)
_DEFAULT_BUILD_TARGET = "kernel_aarch64"
_TOOLS_BAZEL = "tools/bazel"
_INIT_DDK_TARGET = "//build/kernel:init_ddk"


class KleafBootstrapError(RuntimeError):
    pass


def _resolve(opt_path: pathlib.Path | None) -> pathlib.Path | None:
    if opt_path:
        return opt_path.resolve()
    return None


class KleafBootstrap:
    """Calculates the necessary work needed to setup a DDK workspace."""

    def __init__(self, known_args: argparse.Namespace, unknown_args: list[str]):
        self.branch: str | None = known_args.branch
        self.build_id: str | None = known_args.build_id
        self.build_target: str | None = known_args.build_target
        self.ddk_workspace: pathlib.Path = _resolve(known_args.ddk_workspace)
        self.kleaf_repo: pathlib.Path | None = _resolve(known_args.kleaf_repo)
        self.local: bool = known_args.local
        self.url_fmt: str = known_args.url_fmt
        self.unknown_args = unknown_args

    @staticmethod
    def _run_script(args: list[str], cwd: str | pathlib.Path = None):
        logging.debug("Running %s from %s", args, cwd)
        subprocess.check_call(args, stderr=subprocess.STDOUT, cwd=cwd)

    def _common_args(self):
        common_args = []
        if self.build_target:
            common_args += ["--build_target", self.build_target]
        if self.kleaf_repo:
            common_args += ["--kleaf_repo", self.kleaf_repo]
        common_args += ["--url_fmt", self.url_fmt]
        common_args += ["--ddk_workspace", self.ddk_workspace]
        common_args += self.unknown_args
        return common_args

    def run(self):
        if self.local:
            args = [_TOOLS_BAZEL, "run", _INIT_DDK_TARGET]
            args += ["--", "--local"]
            args += self._common_args()
            self._run_script(args, cwd=self.kleaf_repo)
            return

        if not self.build_id:
            self._set_build_id()

        with tempfile.NamedTemporaryFile(
            prefix="init_ddk_", suffix=".zip", mode="w+b"
        ) as init_ddk:
            self._download_artifact("init_ddk.zip", init_ddk)
            args = ["python3", init_ddk.name]
            # Do not add --branch, because its meaning may change during the
            # process of this script.
            if self.build_id:
                args += ["--build_id", self.build_id]
            args += self._common_args()
            self._run_script(args)

    def _set_build_id(self) -> str:
        build_ids_fp = io.BytesIO()
        url = _BUILD_IDS_URL_FMT.format(
            branch=self.branch,
            build_target=self.build_target,
        )
        self._download(url, "build_id", build_ids_fp)
        build_ids_fp.seek(0)
        try:
            build_ids_res = json.load(
                io.TextIOWrapper(build_ids_fp, encoding="utf-8")
            )
        except json.JSONDecodeError as exc:
            raise KleafBootstrapError(
                "Unable to get build_id: not json"
            ) from exc

        try:
            self.build_id = build_ids_res["builds"][0]["buildId"]
        except (KeyError, IndexError) as exc:
            raise KleafBootstrapError(
                "Unable to get build_id: json not in expected format"
            ) from exc

        if not isinstance(self.build_id, str):
            raise KleafBootstrapError(
                "Unable to get build_id: json not in expected format: "
                "build id is not string"
            )

    @staticmethod
    def _download(url, remote_filename, out_f: BinaryIO):
        try:
            with urllib.request.urlopen(url) as in_f:
                logging.debug("Scheduling download for %s", remote_filename)
                shutil.copyfileobj(in_f, out_f)
        except urllib.error.URLError as exc:
            raise KleafBootstrapError(f"Fail to download {url}") from exc

    def _download_artifact(self, remote_filename, out_f: BinaryIO):
        url = self.url_fmt.format(
            build_id=self.build_id,
            build_target=self.build_target,
            filename=urllib.parse.quote(remote_filename, safe=""),  # / -> %2F
        )
        self._download(url, remote_filename, out_f)


if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description=__doc__, formatter_class=argparse.RawTextHelpFormatter
    )
    # For every use case, one of the following is needed:
    #   --branch | --build_id for the remote use case.
    #   --local for an existing checkout (--kleaf_repo becomes required).
    group = parser.add_mutually_exclusive_group(required=True)
    group.add_argument(
        "--branch",
        help=(
            "Android Kernel branch from CI. e.g."
            " aosp_kernel-common-android-mainline."
        ),
        type=str,
        default=None,
    )
    group.add_argument(
        "--build_id",
        type=str,
        help="the build id to download the build for, e.g. 6148204",
        default=None,
    )
    group.add_argument(
        "--local",
        help="Whether to use a local source tree containing Kleaf.",
        action="store_true",
        default=False,
    )
    parser.add_argument(
        "--build_target",
        type=str,
        help='the build target to download, e.g. "kernel_aarch64"',
        default=_DEFAULT_BUILD_TARGET,
    )
    parser.add_argument(
        "--ddk_workspace",
        help=(
            "Path for the new DDK workspace. If not specified defaults to"
            " current directory."
        ),
        type=pathlib.Path,
        default=os.getcwd(),
    )
    parser.add_argument(
        "--kleaf_repo",
        help=(
            "Path to the Kleaf source tree. Build dependencies are taken from"
            " there when --local is provided, or downloaded there otherwise."
        ),
        type=pathlib.Path,
        default=None,
    )
    parser.add_argument(
        "--url_fmt",
        help="URL format endpoint for CI downloads.",
        default=_ARTIFACT_URL_FMT,
    )
    known_args, unknown_args = parser.parse_known_args()
    # Validate pre-condition.
    if known_args.local and not known_args.kleaf_repo:
        parser.error("--local requires --kleaf_repo.")
    logging.basicConfig(
        level=logging.DEBUG, format="%(levelname)s: %(message)s"
    )

    try:
        KleafBootstrap(known_args=known_args, unknown_args=unknown_args).run()
    except KleafBootstrapError as e:
        logging.error(e, exc_info=e)
        sys.exit(1)