1# Copyright (C) 2021 The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14""" 15Functions to fetch pre-pinned Perfetto prebuilts. 16 17This function is used in different places: 18- Into the //tools/{trace_processor, traceconv} scripts, which are just plain 19 wrappers around executables. 20- Into the //tools/{heap_profiler, record_android_trace} scripts, which contain 21 some other hand-written python code. 22 23The manifest argument looks as follows: 24TRACECONV_MANIFEST = [ 25 { 26 'arch': 'mac-amd64', 27 'file_name': 'traceconv', 28 'file_size': 7087080, 29 'url': https://commondatastorage.googleapis.com/.../trace_to_text', 30 'sha256': 7d957c005b0dc130f5bd855d6cec27e060d38841b320d04840afc569f9087490', 31 'platform': 'darwin', 32 'machine': 'x86_64' 33 }, 34 ... 35] 36 37The intended usage is: 38 39 from perfetto.prebuilts.manifests.traceconv import TRACECONV_MANIFEST 40 bin_path = get_perfetto_prebuilt(TRACECONV_MANIFEST) 41 subprocess.call(bin_path, ...) 42""" 43 44import hashlib 45import os 46import platform 47import subprocess 48import sys 49 50 51def download_or_get_cached(file_name, url, sha256): 52 """ Downloads a prebuilt or returns a cached version 53 54 The first time this is invoked, it downloads the |url| and caches it into 55 ~/.local/share/perfetto/prebuilts/$tool_name. On subsequent invocations it 56 just runs the cached version. 57 """ 58 dir = os.path.join( 59 os.path.expanduser('~'), '.local', 'share', 'perfetto', 'prebuilts') 60 os.makedirs(dir, exist_ok=True) 61 bin_path = os.path.join(dir, file_name) 62 sha256_path = os.path.join(dir, file_name + '.sha256') 63 needs_download = True 64 65 # Avoid recomputing the SHA-256 on each invocation. The SHA-256 of the last 66 # download is cached into file_name.sha256, just check if that matches. 67 if os.path.exists(bin_path) and os.path.exists(sha256_path): 68 with open(sha256_path, 'rb') as f: 69 digest = f.read().decode() 70 if digest == sha256: 71 needs_download = False 72 73 if needs_download: 74 # Either the filed doesn't exist or the SHA256 doesn't match. 75 tmp_path = bin_path + '.tmp' 76 print('Downloading ' + url) 77 subprocess.check_call(['curl', '-f', '-L', '-#', '-o', tmp_path, url]) 78 with open(tmp_path, 'rb') as fd: 79 actual_sha256 = hashlib.sha256(fd.read()).hexdigest() 80 if actual_sha256 != sha256: 81 raise Exception('Checksum mismatch for %s (actual: %s, expected: %s)' % 82 (url, actual_sha256, sha256)) 83 os.chmod(tmp_path, 0o755) 84 os.replace(tmp_path, bin_path) 85 with open(sha256_path, 'w') as f: 86 f.write(sha256) 87 return bin_path 88 89 90def get_perfetto_prebuilt(manifest, soft_fail=False, arch=None): 91 """ Downloads the prebuilt, if necessary, and returns its path on disk. """ 92 plat = sys.platform.lower() 93 machine = platform.machine().lower() 94 manifest_entry = None 95 for entry in manifest: 96 # If the caller overrides the arch, just match that (for Android prebuilts). 97 if arch: 98 if entry.get('arch') == arch: 99 manifest_entry = entry 100 break 101 continue 102 # Otherwise guess the local machine arch. 103 if entry.get('platform') == plat and machine in entry.get('machine', []): 104 manifest_entry = entry 105 break 106 if manifest_entry is None: 107 if soft_fail: 108 return None 109 raise Exception( 110 ('No prebuilts available for %s-%s\n' % (plat, machine)) + 111 'See https://perfetto.dev/docs/contributing/build-instructions') 112 113 return download_or_get_cached( 114 file_name=manifest_entry['file_name'], 115 url=manifest_entry['url'], 116 sha256=manifest_entry['sha256']) 117 118 119def run_perfetto_prebuilt(manifest): 120 bin_path = get_perfetto_prebuilt(manifest) 121 if sys.platform.lower() == 'win32': 122 sys.exit(subprocess.check_call([bin_path, *sys.argv[1:]])) 123 os.execv(bin_path, [bin_path] + sys.argv[1:]) 124