blob: ebc0273d2c5a44a8d41929974c45bd450281da16 [file] [log] [blame]
Primiano Tucci34bc5592021-02-19 17:53:36 +01001#!/usr/bin/env python3
Primiano Tuccid3e40bd2021-08-03 10:52:39 +01002# Copyright (C) 2021 The Android Open Source Project
Hector Dearman998741d2019-02-22 11:30:03 +00003#
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# This file should do the same thing when being invoked in any of these ways:
17# ./traceconv
18# python traceconv
19# bash traceconv
20# cat ./traceconv | bash
21# cat ./traceconv | python -
22
Matthew Clarkson63028d62019-10-10 15:48:23 +010023BASH_FALLBACK = """ "
Florian Mayer0367ce42021-04-19 15:01:32 +010024exec python3 - "$@" <<'#'EOF
Hector Dearman998741d2019-02-22 11:30:03 +000025#"""
26
Primiano Tuccid3e40bd2021-08-03 10:52:39 +010027TOOL_NAME = 'trace_to_text'
28
29# BEGIN_SECTION_GENERATED_BY(roll-prebuilts)
Primiano Tucci45646b62021-11-04 11:18:03 +000030# Revision: v21.0
Primiano Tuccid3e40bd2021-08-03 10:52:39 +010031PERFETTO_PREBUILT_MANIFEST = [{
32 'tool':
33 'trace_to_text',
34 'arch':
35 'mac-amd64',
36 'file_name':
37 'trace_to_text',
38 'file_size':
Primiano Tucci45646b62021-11-04 11:18:03 +000039 7136200,
Primiano Tuccid3e40bd2021-08-03 10:52:39 +010040 'url':
Primiano Tucci45646b62021-11-04 11:18:03 +000041 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v21.0/mac-amd64/trace_to_text',
Primiano Tuccid3e40bd2021-08-03 10:52:39 +010042 'sha256':
Primiano Tucci45646b62021-11-04 11:18:03 +000043 '17d93547b6c2d81905377e5eb5ac16dc5567df2d1898b84f617c0b723cd7d157',
Primiano Tuccid3e40bd2021-08-03 10:52:39 +010044 'platform':
45 'darwin',
46 'machine': ['x86_64']
47}, {
48 'tool':
49 'trace_to_text',
50 'arch':
51 'linux-amd64',
52 'file_name':
53 'trace_to_text',
54 'file_size':
Primiano Tucci45646b62021-11-04 11:18:03 +000055 7671280,
Primiano Tuccid3e40bd2021-08-03 10:52:39 +010056 'url':
Primiano Tucci45646b62021-11-04 11:18:03 +000057 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v21.0/linux-amd64/trace_to_text',
Primiano Tuccid3e40bd2021-08-03 10:52:39 +010058 'sha256':
Primiano Tucci45646b62021-11-04 11:18:03 +000059 '54858c0d97b13e510d3c62f144d0d8a9959280e4e89c0912b25905fe480a3493',
Primiano Tuccid3e40bd2021-08-03 10:52:39 +010060 'platform':
61 'linux',
62 'machine': ['x86_64']
63}, {
64 'tool':
65 'trace_to_text',
66 'arch':
67 'windows-amd64',
68 'file_name':
69 'trace_to_text.exe',
70 'file_size':
Primiano Tucci45646b62021-11-04 11:18:03 +000071 6591488,
Primiano Tuccid3e40bd2021-08-03 10:52:39 +010072 'url':
Primiano Tucci45646b62021-11-04 11:18:03 +000073 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v21.0/windows-amd64/trace_to_text.exe',
Primiano Tuccid3e40bd2021-08-03 10:52:39 +010074 'sha256':
Primiano Tucci45646b62021-11-04 11:18:03 +000075 'c93daee4fc305daa91aabcb7cad374e7f2870dc2c539259c4e40557901797f8b',
Primiano Tuccid3e40bd2021-08-03 10:52:39 +010076 'platform':
77 'win32',
78 'machine': ['amd64']
79}]
Lalit Maganti27832482020-05-18 12:10:50 +010080
Hector Dearman998741d2019-02-22 11:30:03 +000081
Primiano Tuccid3e40bd2021-08-03 10:52:39 +010082# DO NOT EDIT. If you wish to make edits to this code, you need to change only
83# //tools/get_perfetto_prebuilt.py and run /tools/roll-prebuilts to regenerate
84# all the others scripts this is embedded into.
85def get_perfetto_prebuilt(tool_name, soft_fail=False, arch=None):
86 """ Downloads the prebuilt, if necessary, and returns its path on disk. """
87
88 # The first time this is invoked, it downloads the |url| and caches it into
89 # ~/.perfetto/prebuilts/$tool_name. On subsequent invocations it just runs the
90 # cached version.
91 def download_or_get_cached(file_name, url, sha256):
92 import os, hashlib, subprocess
93 dir = os.path.join(
94 os.path.expanduser('~'), '.local', 'share', 'perfetto', 'prebuilts')
95 os.makedirs(dir, exist_ok=True)
96 bin_path = os.path.join(dir, file_name)
97 sha256_path = os.path.join(dir, file_name + '.sha256')
98 needs_download = True
99
100 # Avoid recomputing the SHA-256 on each invocation. The SHA-256 of the last
101 # download is cached into file_name.sha256, just check if that matches.
102 if os.path.exists(bin_path) and os.path.exists(sha256_path):
103 with open(sha256_path, 'rb') as f:
104 digest = f.read().decode()
105 if digest == sha256:
106 needs_download = False
107
108 if needs_download:
109 # Either the filed doesn't exist or the SHA256 doesn't match.
110 tmp_path = bin_path + '.tmp'
111 print('Downloading ' + url)
112 subprocess.check_call(['curl', '-f', '-L', '-#', '-o', tmp_path, url])
113 with open(tmp_path, 'rb') as fd:
114 actual_sha256 = hashlib.sha256(fd.read()).hexdigest()
115 if actual_sha256 != sha256:
116 raise Exception('Checksum mismatch for %s (actual: %s, expected: %s)' %
117 (url, actual_sha256, sha256))
118 os.chmod(tmp_path, 0o755)
119 os.rename(tmp_path, bin_path)
120 with open(sha256_path, 'w') as f:
121 f.write(sha256)
122 return bin_path
123 # --- end of download_or_get_cached() ---
124
125 # --- get_perfetto_prebuilt() function starts here. ---
126 import os, platform, sys
127 plat = sys.platform.lower()
128 machine = platform.machine().lower()
129 manifest_entry = None
130 for entry in PERFETTO_PREBUILT_MANIFEST:
131 # If the caller overrides the arch, just match that (for Android prebuilts).
132 if arch and entry.get('arch') == arch:
133 manifest_entry = entry
134 break
135 # Otherwise guess the local machine arch.
136 if entry.get('tool') == tool_name and entry.get(
137 'platform') == plat and machine in entry.get('machine', []):
138 manifest_entry = entry
139 break
140 if manifest_entry is None:
141 if soft_fail:
142 return None
143 raise Exception(
144 ('No prebuilts available for %s-%s\n' % (plat, machine)) +
145 'See https://perfetto.dev/docs/contributing/build-instructions')
146
147 return download_or_get_cached(
148 file_name=manifest_entry['file_name'],
149 url=manifest_entry['url'],
150 sha256=manifest_entry['sha256'])
Matthew Clarkson63028d62019-10-10 15:48:23 +0100151
Hector Dearman998741d2019-02-22 11:30:03 +0000152
Primiano Tuccid3e40bd2021-08-03 10:52:39 +0100153# END_SECTION_GENERATED_BY(roll-prebuilts)
Matthew Clarkson63028d62019-10-10 15:48:23 +0100154
Hector Dearman998741d2019-02-22 11:30:03 +0000155if __name__ == '__main__':
Primiano Tuccid3e40bd2021-08-03 10:52:39 +0100156 import sys, os
157 bin_path = get_perfetto_prebuilt(TOOL_NAME)
158 os.execv(bin_path, [bin_path] + sys.argv[1:])
Hector Dearman998741d2019-02-22 11:30:03 +0000159
160#EOF