1#!/usr/bin/env python3 2# Copyright (C) 2021 The Android Open Source Project 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 17# DO NOT EDIT. Auto-generated by tools/gen_amalgamated_python_tools 18# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 19 20# This file should do the same thing when being invoked in any of these ways: 21# ./tracebox 22# python tracebox 23# bash tracebox 24# cat ./tracebox | bash 25# cat ./tracebox | python - 26 27BASH_FALLBACK=""" " 28exec python3 - "$@" <<'#'EOF 29#""" # yapf: disable 30 31 32# ----- Amalgamator: begin of python/perfetto/prebuilts/manifests/tracebox.py 33# This file has been generated by: /Users/hjd/src/perfetto/tools/roll-prebuilts v34.0 34TRACEBOX_MANIFEST = [{ 35 'arch': 36 'mac-amd64', 37 'file_name': 38 'tracebox', 39 'file_size': 40 1432064, 41 'url': 42 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v34.0/mac-amd64/tracebox', 43 'sha256': 44 '4ceb7646cd99303224ab5e7ff0a9f84c04f3c5466fff65a55dab65171ae9d482', 45 'platform': 46 'darwin', 47 'machine': ['x86_64'] 48}, { 49 'arch': 50 'mac-arm64', 51 'file_name': 52 'tracebox', 53 'file_size': 54 1325704, 55 'url': 56 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v34.0/mac-arm64/tracebox', 57 'sha256': 58 '2c560fcce5e19eb692e50487af134e2078347cdb79decba0c572917860528388', 59 'platform': 60 'darwin', 61 'machine': ['arm64'] 62}, { 63 'arch': 64 'linux-amd64', 65 'file_name': 66 'tracebox', 67 'file_size': 68 2155496, 69 'url': 70 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v34.0/linux-amd64/tracebox', 71 'sha256': 72 '10b92180bb461a7e21be3f8b3d4640430a98d0547238ce095709213b378217d2', 73 'platform': 74 'linux', 75 'machine': ['x86_64'] 76}, { 77 'arch': 78 'linux-arm', 79 'file_name': 80 'tracebox', 81 'file_size': 82 1288764, 83 'url': 84 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v34.0/linux-arm/tracebox', 85 'sha256': 86 'fa28950ce2b7a9345fbb9272f2dd04d3d4eb2a87f021df25e1e649840eae60b5', 87 'platform': 88 'linux', 89 'machine': ['armv6l', 'armv7l', 'armv8l'] 90}, { 91 'arch': 92 'linux-arm64', 93 'file_name': 94 'tracebox', 95 'file_size': 96 2082704, 97 'url': 98 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v34.0/linux-arm64/tracebox', 99 'sha256': 100 '85c371d79b8e23d22a293c29e6399dc311d891a6bd85d7eeaf2cb0179c69eb27', 101 'platform': 102 'linux', 103 'machine': ['aarch64'] 104}, { 105 'arch': 106 'android-arm', 107 'file_name': 108 'tracebox', 109 'file_size': 110 1169364, 111 'url': 112 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v34.0/android-arm/tracebox', 113 'sha256': 114 '40a3f31600f02dea10e290134d5c862e0e717f4f039756889a4e72c60f1591b6' 115}, { 116 'arch': 117 'android-arm64', 118 'file_name': 119 'tracebox', 120 'file_size': 121 1776296, 122 'url': 123 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v34.0/android-arm64/tracebox', 124 'sha256': 125 '562505fca18b34a97687dc002aeebcbf20acef68c8a8e48bed6d618c20e07c92' 126}, { 127 'arch': 128 'android-x86', 129 'file_name': 130 'tracebox', 131 'file_size': 132 1767340, 133 'url': 134 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v34.0/android-x86/tracebox', 135 'sha256': 136 'eb47eb43ba93403557dd15a61196799e945ec324d96109db2f155fb131f9996a' 137}, { 138 'arch': 139 'android-x64', 140 'file_name': 141 'tracebox', 142 'file_size': 143 2054824, 144 'url': 145 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v34.0/android-x64/tracebox', 146 'sha256': 147 'a3ae6d108e041ba368a9770f952772f111865d4eff7c8e4e4e2f653f45017948' 148}] 149 150# ----- Amalgamator: end of python/perfetto/prebuilts/manifests/tracebox.py 151 152# ----- Amalgamator: begin of python/perfetto/prebuilts/perfetto_prebuilts.py 153# Copyright (C) 2021 The Android Open Source Project 154# 155# Licensed under the Apache License, Version 2.0 (the "License"); 156# you may not use this file except in compliance with the License. 157# You may obtain a copy of the License at 158# 159# http://www.apache.org/licenses/LICENSE-2.0 160# 161# Unless required by applicable law or agreed to in writing, software 162# distributed under the License is distributed on an "AS IS" BASIS, 163# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 164# See the License for the specific language governing permissions and 165# limitations under the License. 166""" 167Functions to fetch pre-pinned Perfetto prebuilts. 168 169This function is used in different places: 170- Into the //tools/{trace_processor, traceconv} scripts, which are just plain 171 wrappers around executables. 172- Into the //tools/{heap_profiler, record_android_trace} scripts, which contain 173 some other hand-written python code. 174 175The manifest argument looks as follows: 176TRACECONV_MANIFEST = [ 177 { 178 'arch': 'mac-amd64', 179 'file_name': 'traceconv', 180 'file_size': 7087080, 181 'url': https://commondatastorage.googleapis.com/.../trace_to_text', 182 'sha256': 7d957c005b0dc130f5bd855d6cec27e060d38841b320d04840afc569f9087490', 183 'platform': 'darwin', 184 'machine': 'x86_64' 185 }, 186 ... 187] 188 189The intended usage is: 190 191 from perfetto.prebuilts.manifests.traceconv import TRACECONV_MANIFEST 192 bin_path = get_perfetto_prebuilt(TRACECONV_MANIFEST) 193 subprocess.call(bin_path, ...) 194""" 195 196import hashlib 197import os 198import platform 199import subprocess 200import sys 201 202 203def download_or_get_cached(file_name, url, sha256): 204 """ Downloads a prebuilt or returns a cached version 205 206 The first time this is invoked, it downloads the |url| and caches it into 207 ~/.local/share/perfetto/prebuilts/$tool_name. On subsequent invocations it 208 just runs the cached version. 209 """ 210 dir = os.path.join( 211 os.path.expanduser('~'), '.local', 'share', 'perfetto', 'prebuilts') 212 os.makedirs(dir, exist_ok=True) 213 bin_path = os.path.join(dir, file_name) 214 sha256_path = os.path.join(dir, file_name + '.sha256') 215 needs_download = True 216 217 # Avoid recomputing the SHA-256 on each invocation. The SHA-256 of the last 218 # download is cached into file_name.sha256, just check if that matches. 219 if os.path.exists(bin_path) and os.path.exists(sha256_path): 220 with open(sha256_path, 'rb') as f: 221 digest = f.read().decode() 222 if digest == sha256: 223 needs_download = False 224 225 if needs_download: 226 # Either the filed doesn't exist or the SHA256 doesn't match. 227 tmp_path = bin_path + '.tmp' 228 print('Downloading ' + url) 229 subprocess.check_call(['curl', '-f', '-L', '-#', '-o', tmp_path, url]) 230 with open(tmp_path, 'rb') as fd: 231 actual_sha256 = hashlib.sha256(fd.read()).hexdigest() 232 if actual_sha256 != sha256: 233 raise Exception('Checksum mismatch for %s (actual: %s, expected: %s)' % 234 (url, actual_sha256, sha256)) 235 os.chmod(tmp_path, 0o755) 236 os.replace(tmp_path, bin_path) 237 with open(sha256_path, 'w') as f: 238 f.write(sha256) 239 return bin_path 240 241 242def get_perfetto_prebuilt(manifest, soft_fail=False, arch=None): 243 """ Downloads the prebuilt, if necessary, and returns its path on disk. """ 244 plat = sys.platform.lower() 245 machine = platform.machine().lower() 246 manifest_entry = None 247 for entry in manifest: 248 # If the caller overrides the arch, just match that (for Android prebuilts). 249 if arch: 250 if entry.get('arch') == arch: 251 manifest_entry = entry 252 break 253 continue 254 # Otherwise guess the local machine arch. 255 if entry.get('platform') == plat and machine in entry.get('machine', []): 256 manifest_entry = entry 257 break 258 if manifest_entry is None: 259 if soft_fail: 260 return None 261 raise Exception( 262 ('No prebuilts available for %s-%s\n' % (plat, machine)) + 263 'See https://perfetto.dev/docs/contributing/build-instructions') 264 265 return download_or_get_cached( 266 file_name=manifest_entry['file_name'], 267 url=manifest_entry['url'], 268 sha256=manifest_entry['sha256']) 269 270 271def run_perfetto_prebuilt(manifest): 272 bin_path = get_perfetto_prebuilt(manifest) 273 if sys.platform.lower() == 'win32': 274 sys.exit(subprocess.check_call([bin_path, *sys.argv[1:]])) 275 os.execv(bin_path, [bin_path] + sys.argv[1:]) 276 277# ----- Amalgamator: end of python/perfetto/prebuilts/perfetto_prebuilts.py 278 279if __name__ == '__main__': 280 run_perfetto_prebuilt(TRACEBOX_MANIFEST) 281 282#EOF 283