Primiano Tucci | 34bc559 | 2021-02-19 17:53:36 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 2 | # Copyright (C) 2021 The Android Open Source Project |
Hector Dearman | 998741d | 2019-02-22 11:30:03 +0000 | [diff] [blame] | 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 | # 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 Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame] | 23 | BASH_FALLBACK = """ " |
Florian Mayer | 0367ce4 | 2021-04-19 15:01:32 +0100 | [diff] [blame] | 24 | exec python3 - "$@" <<'#'EOF |
Hector Dearman | 998741d | 2019-02-22 11:30:03 +0000 | [diff] [blame] | 25 | #""" |
| 26 | |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 27 | TOOL_NAME = 'trace_to_text' |
| 28 | |
| 29 | # BEGIN_SECTION_GENERATED_BY(roll-prebuilts) |
Primiano Tucci | 45646b6 | 2021-11-04 11:18:03 +0000 | [diff] [blame] | 30 | # Revision: v21.0 |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 31 | PERFETTO_PREBUILT_MANIFEST = [{ |
| 32 | 'tool': |
| 33 | 'trace_to_text', |
| 34 | 'arch': |
| 35 | 'mac-amd64', |
| 36 | 'file_name': |
| 37 | 'trace_to_text', |
| 38 | 'file_size': |
Primiano Tucci | 45646b6 | 2021-11-04 11:18:03 +0000 | [diff] [blame] | 39 | 7136200, |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 40 | 'url': |
Primiano Tucci | 45646b6 | 2021-11-04 11:18:03 +0000 | [diff] [blame] | 41 | 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v21.0/mac-amd64/trace_to_text', |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 42 | 'sha256': |
Primiano Tucci | 45646b6 | 2021-11-04 11:18:03 +0000 | [diff] [blame] | 43 | '17d93547b6c2d81905377e5eb5ac16dc5567df2d1898b84f617c0b723cd7d157', |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 44 | '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 Tucci | 45646b6 | 2021-11-04 11:18:03 +0000 | [diff] [blame] | 55 | 7671280, |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 56 | 'url': |
Primiano Tucci | 45646b6 | 2021-11-04 11:18:03 +0000 | [diff] [blame] | 57 | 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v21.0/linux-amd64/trace_to_text', |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 58 | 'sha256': |
Primiano Tucci | 45646b6 | 2021-11-04 11:18:03 +0000 | [diff] [blame] | 59 | '54858c0d97b13e510d3c62f144d0d8a9959280e4e89c0912b25905fe480a3493', |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 60 | '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 Tucci | 45646b6 | 2021-11-04 11:18:03 +0000 | [diff] [blame] | 71 | 6591488, |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 72 | 'url': |
Primiano Tucci | 45646b6 | 2021-11-04 11:18:03 +0000 | [diff] [blame] | 73 | 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v21.0/windows-amd64/trace_to_text.exe', |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 74 | 'sha256': |
Primiano Tucci | 45646b6 | 2021-11-04 11:18:03 +0000 | [diff] [blame] | 75 | 'c93daee4fc305daa91aabcb7cad374e7f2870dc2c539259c4e40557901797f8b', |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 76 | 'platform': |
| 77 | 'win32', |
| 78 | 'machine': ['amd64'] |
| 79 | }] |
Lalit Maganti | 2783248 | 2020-05-18 12:10:50 +0100 | [diff] [blame] | 80 | |
Hector Dearman | 998741d | 2019-02-22 11:30:03 +0000 | [diff] [blame] | 81 | |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 82 | # 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. |
| 85 | def 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 Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame] | 151 | |
Hector Dearman | 998741d | 2019-02-22 11:30:03 +0000 | [diff] [blame] | 152 | |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 153 | # END_SECTION_GENERATED_BY(roll-prebuilts) |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame] | 154 | |
Hector Dearman | 998741d | 2019-02-22 11:30:03 +0000 | [diff] [blame] | 155 | if __name__ == '__main__': |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 156 | import sys, os |
| 157 | bin_path = get_perfetto_prebuilt(TOOL_NAME) |
| 158 | os.execv(bin_path, [bin_path] + sys.argv[1:]) |
Hector Dearman | 998741d | 2019-02-22 11:30:03 +0000 | [diff] [blame] | 159 | |
| 160 | #EOF |