summaryrefslogtreecommitdiff
path: root/update.py
blob: 7648c3d989908ab5e78450c610c4be1ffda614c6 (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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/env python3
#
# Copyright (C) 2016 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 simpleperf prebuilts from the build server."""
import argparse
import glob
import logging
import os
from pathlib import Path
import shlex
import shutil
import stat
import subprocess
import textwrap
from typing import List, Union


THIS_DIR = os.path.realpath(os.path.dirname(__file__))


class InstallEntry(object):
    def __init__(self, target, name, install_path, need_strip=False):
        self.target = target
        self.name = name
        self.install_path = install_path
        self.need_strip = need_strip


MINGW = 'local:../../prebuilts/gcc/linux-x86/host/x86_64-w64-mingw32-4.8/x86_64-w64-mingw32/'
bin_install_list = [
    # simpleperf on device.
    InstallEntry('MODULES-IN-system-extras-simpleperf',
                 'simpleperf/android/arm64/simpleperf_ndk',
                 'android/arm64/simpleperf'),
    InstallEntry('MODULES-IN-system-extras-simpleperf_arm',
                 'simpleperf/android/arm/simpleperf_ndk32',
                 'android/arm/simpleperf'),
    InstallEntry('MODULES-IN-system-extras-simpleperf_x86',
                 'simpleperf/android/x86_64/simpleperf_ndk',
                 'android/x86_64/simpleperf'),
    InstallEntry('MODULES-IN-system-extras-simpleperf_x86',
                 'simpleperf/android/x86/simpleperf_ndk32',
                 'android/x86/simpleperf'),
    InstallEntry('MODULES-IN-system-extras-simpleperf_riscv64',
                 'simpleperf_ndk',
                 'android/riscv64/simpleperf'),

    # simpleperf on host. Linux and macOS are 64-bit only these days.
    InstallEntry('MODULES-IN-system-extras-simpleperf',
                 'simpleperf/linux/x86_64/simpleperf',
                 'linux/x86_64/simpleperf', True),
    InstallEntry('MODULES-IN-system-extras-simpleperf_mac',
                 'simpleperf/darwin/x86_64/simpleperf',
                 'darwin/x86_64/simpleperf'),
    InstallEntry('MODULES-IN-system-extras-simpleperf',
                 'simpleperf/windows/x86_64/simpleperf.exe',
                 'windows/x86_64/simpleperf.exe', True),

    # libsimpleperf_report.so on host
    InstallEntry('MODULES-IN-system-extras-simpleperf',
                 'simpleperf/linux/x86_64/libsimpleperf_report.so',
                 'linux/x86_64/libsimpleperf_report.so', True),
    InstallEntry('MODULES-IN-system-extras-simpleperf_mac',
                 'simpleperf/darwin/x86_64/libsimpleperf_report.dylib',
                 'darwin/x86_64/libsimpleperf_report.dylib'),
    InstallEntry('MODULES-IN-system-extras-simpleperf',
                 'simpleperf/windows/x86_64/libsimpleperf_report.dll',
                 'windows/x86_64/libsimpleperf_report.dll', True),

    # libwinpthread-1.dll on windows host
    InstallEntry(MINGW + '/bin/libwinpthread-1.dll', 'libwinpthread-1.dll',
                 'windows/x86_64/libwinpthread-1.dll', False),
]

script_install_entry = InstallEntry(
    'MODULES-IN-system-extras-simpleperf', 'simpleperf/simpleperf_script.zip',
    'simpleperf_script.zip')


def logger():
    """Returns the main logger for this module."""
    return logging.getLogger(__name__)


def check_call(cmd: Union[str, List[str]]):
    """Proxy for subprocess.check_call with logging."""
    if isinstance(cmd, list):
        cmd = shlex.join(cmd)
    logger().debug('check_call `%s`', cmd)
    subprocess.run(cmd, shell=True, check=True)


def remove(path: Union[str, Path]):
    if isinstance(path, str):
        path = Path(path)
    if not path.exists():
        return
    if path.is_dir():
        shutil.rmtree(path.as_posix())
    else:
        path.unlink()


def fetch_artifact(branch, build, target, pattern):
    """Fetches and artifact from the build server."""
    logger().info('Fetching %s from %s %s (artifacts matching %s)', build,
                  target, branch, pattern)
    if target.startswith('local:'):
        shutil.copyfile(target[6:], pattern)
        return
    fetch_artifact_path = '/google/data/ro/projects/android/fetch_artifact'
    cmd = [fetch_artifact_path, '--branch', branch, '--target', target,
           '--bid', build, pattern]
    check_call(cmd)


def start_branch(build):
    """Creates a new branch in the project."""
    branch_name = 'update-' + (build or 'latest')
    logger().info('Creating branch %s', branch_name)
    check_call(['repo', 'start', branch_name, '.'])


def commit(branch, build, add_paths):
    """Commits the new prebuilts."""
    logger().info('Making commit')
    check_call(['git', 'add'] + add_paths)
    message = textwrap.dedent("""\
        Update NDK prebuilts to build {build}.

        Taken from branch {branch}.""").format(branch=branch, build=build)
    check_call(['git', 'commit', '-m', message])


def list_prebuilts() -> List[str]:
    """List all prebuilts in current directory."""
    result = []
    for d in ['app_api', 'bin', 'doc', 'inferno', 'proto', 'purgatorio', 'test', 'testdata']:
        if os.path.isdir(d):
            result.append(d)
    result += glob.glob('*.py') + glob.glob('*.js')
    result.remove('update.py')
    result += ['inferno.sh', 'inferno.bat']
    return result


def remove_old_release():
    """Removes the old prebuilts."""
    old_prebuilts = list_prebuilts()
    if not old_prebuilts:
        return
    logger().info('Removing old prebuilts %s', old_prebuilts)
    check_call(['git', 'rm', '-rf', '--ignore-unmatch'] + old_prebuilts)

    # Need to check again because git won't remove directories if they have
    # non-git files in them.
    for prebuilt in old_prebuilts:
        remove(prebuilt)


def install_new_release(branch, build):
    """Installs the new release."""
    for entry in bin_install_list:
        install_entry(branch, build, 'bin', entry)
    install_entry(branch, build, '.', script_install_entry)
    unzip_simpleperf_scripts(script_install_entry.install_path)
    install_repo_prop(branch, build)


def install_entry(branch, build, install_dir, entry):
    """Installs one prebuilt file specified by entry."""
    target = entry.target
    name = entry.name
    install_path = os.path.join(install_dir, entry.install_path)
    need_strip = entry.need_strip

    fetch_artifact(branch, build, target, name)
    name = os.path.basename(name)
    exe_stat = os.stat(name)
    os.chmod(name, exe_stat.st_mode | stat.S_IEXEC)
    if need_strip:
        check_call(['strip', name])
    dir = os.path.dirname(install_path)
    if not os.path.isdir(dir):
        os.makedirs(dir)
    shutil.move(name, install_path)


def unzip_simpleperf_scripts(zip_path: str):
    check_call('unzip %s' % zip_path)
    remove(zip_path)

    # Move scripts.
    for sub_path in Path('scripts').iterdir():
        if sub_path.name not in ['bin', 'pylintrc', 'update.py', 'Android.bp']:
            shutil.move(sub_path, '.')
    remove('scripts')
    remove('inferno/Android.bp')
    remove('CONTRIBUTING.md')

    # Move proto files.
    proto_dir = Path('proto')
    proto_dir.mkdir()
    for sub_path in Path.cwd().iterdir():
        if sub_path.suffix == '.proto':
            shutil.move(sub_path, proto_dir)

    # Build testdata.
    testdata_dir = Path('test/testdata')
    testdata_dir.mkdir()
    for source_dir in ['demo', 'runtest', 'testdata', 'test/script_testdata']:
        for sub_path in Path(source_dir).iterdir():
            shutil.move(sub_path, testdata_dir)
        remove(source_dir)
    remove(testdata_dir / 'Android.bp')


def install_repo_prop(branch, build):
    """Installs the repo.prop from the build for auditing."""
    # We took everything from the same build number, so we only need the
    # repo.prop from one of our targets.
    fetch_artifact(branch, build, 'MODULES-IN-system-extras-simpleperf', 'repo.prop')


def get_args():
    """Parses and returns command line arguments."""
    parser = argparse.ArgumentParser()

    parser.add_argument(
        '-b', '--branch', default='aosp-simpleperf-release',
        help='Branch to pull build from.')
    parser.add_argument('--build', required=True, help='Build number to pull.')
    parser.add_argument(
        '--use-current-branch', action='store_true',
        help='Perform the update in the current branch. Do not repo start.')
    parser.add_argument(
        '-v', '--verbose', action='count', default=0,
        help='Increase output verbosity.')

    return parser.parse_args()


def main():
    """Program entry point."""
    os.chdir(THIS_DIR)

    args = get_args()
    verbose_map = (logging.WARNING, logging.INFO, logging.DEBUG)
    verbosity = args.verbose
    if verbosity > 2:
        verbosity = 2
    logging.basicConfig(level=verbose_map[verbosity])

    if not args.use_current_branch:
        start_branch(args.build)
    remove_old_release()
    install_new_release(args.branch, args.build)
    artifacts = ['repo.prop'] + list_prebuilts()
    commit(args.branch, args.build, artifacts)


if __name__ == '__main__':
    main()