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