|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Downloads perfetto's trace_processor_shell, matching the SDK version vendored |
| 3 | +# in deps/perfetto. The trace events tests read back the binary traces that |
| 4 | +# perfetto builds write through this tool. |
| 5 | + |
| 6 | +import os |
| 7 | +import platform |
| 8 | +import shutil |
| 9 | +import stat |
| 10 | +import sys |
| 11 | +import tempfile |
| 12 | +import urllib.request |
| 13 | +import zipfile |
| 14 | + |
| 15 | +script_dir = os.path.dirname(os.path.abspath(__file__)) |
| 16 | +version_file = os.path.join(script_dir, '..', '..', 'deps', 'perfetto', 'VERSION') |
| 17 | +is_windows = platform.system() == 'Windows' |
| 18 | + |
| 19 | +# Perfetto names its release archives <os>-<arch>, which does not match what |
| 20 | +# platform reports. |
| 21 | +OS_NAMES = {'Darwin': 'mac', 'Linux': 'linux', 'Windows': 'windows'} |
| 22 | +ARCH_NAMES = {'x86_64': 'amd64', 'amd64': 'amd64', 'arm64': 'arm64', 'aarch64': 'arm64'} |
| 23 | + |
| 24 | + |
| 25 | +def release_name(): |
| 26 | + os_name = OS_NAMES.get(platform.system()) |
| 27 | + arch = ARCH_NAMES.get(platform.machine().lower()) |
| 28 | + if os_name is None or arch is None: |
| 29 | + sys.exit(f'No perfetto release for {platform.system()} {platform.machine()}') |
| 30 | + # Perfetto releases only an x64 build for Windows. Windows on arm64 runs it |
| 31 | + # under emulation. |
| 32 | + if is_windows: |
| 33 | + arch = 'amd64' |
| 34 | + return f'{os_name}-{arch}' |
| 35 | + |
| 36 | + |
| 37 | +def main(): |
| 38 | + exe = 'trace_processor_shell.exe' if is_windows else 'trace_processor_shell' |
| 39 | + trace_processor = sys.argv[1] if len(sys.argv) > 1 else os.path.join(script_dir, exe) |
| 40 | + |
| 41 | + # The tool is current when it is newer than the version file it was |
| 42 | + # downloaded for, so a version bump re-downloads it. |
| 43 | + if (os.path.exists(trace_processor) and |
| 44 | + os.path.getmtime(trace_processor) > os.path.getmtime(version_file)): |
| 45 | + return |
| 46 | + |
| 47 | + with open(version_file) as f: |
| 48 | + version = f.read().strip() |
| 49 | + name = release_name() |
| 50 | + url = f'https://github.com/google/perfetto/releases/download/v{version}/{name}.zip' |
| 51 | + |
| 52 | + print(f'Downloading {url}') |
| 53 | + with tempfile.TemporaryFile() as archive: |
| 54 | + with urllib.request.urlopen(url) as response: |
| 55 | + shutil.copyfileobj(response, archive) |
| 56 | + with zipfile.ZipFile(archive) as zf, \ |
| 57 | + zf.open(f'{name}/{exe}') as src, \ |
| 58 | + open(trace_processor, 'wb') as dst: |
| 59 | + shutil.copyfileobj(src, dst) |
| 60 | + |
| 61 | + if not is_windows: |
| 62 | + mode = os.stat(trace_processor).st_mode |
| 63 | + os.chmod(trace_processor, mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) |
| 64 | + |
| 65 | + |
| 66 | +if __name__ == '__main__': |
| 67 | + main() |
0 commit comments