Hector Dearman | 998741d | 2019-02-22 11:30:03 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright (C) 2019 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 | # 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 | |
| 23 | BASH_FALLBACK=""" " |
Hector Dearman | bd33276 | 2019-02-22 14:42:47 +0000 | [diff] [blame] | 24 | exec python - "$@" <<'#'EOF |
Hector Dearman | 998741d | 2019-02-22 11:30:03 +0000 | [diff] [blame] | 25 | #""" |
| 26 | |
| 27 | import hashlib |
| 28 | import os |
| 29 | import sys |
| 30 | import tempfile |
| 31 | import urllib |
| 32 | |
Lalit Maganti | 1ba0d51 | 2019-05-21 19:23:38 +0100 | [diff] [blame] | 33 | # Keep this in sync with the SHAs in catapult file |
| 34 | # systrace/systrace/tracing_agents/atrace_from_file_agent.py. |
Hector Dearman | 998741d | 2019-02-22 11:30:03 +0000 | [diff] [blame] | 35 | TRACE_TO_TEXT_SHAS = { |
Isabelle Taylor | 20d5fb6 | 2019-06-27 14:06:13 +0100 | [diff] [blame] | 36 | 'linux': 'd67f9f3e3beb93664578c8b0300ab98e828ecb29', |
| 37 | 'mac': '62b27f343640d0778f1324fa90940381561bef99', |
Hector Dearman | 998741d | 2019-02-22 11:30:03 +0000 | [diff] [blame] | 38 | } |
| 39 | TRACE_TO_TEXT_PATH = tempfile.gettempdir() |
| 40 | TRACE_TO_TEXT_BASE_URL = ( |
| 41 | 'https://storage.googleapis.com/perfetto/') |
| 42 | |
| 43 | def check_hash(file_name, sha_value): |
| 44 | with open(file_name, 'rb') as fd: |
| 45 | file_hash = hashlib.sha1(fd.read()).hexdigest() |
| 46 | return file_hash == sha_value |
| 47 | |
| 48 | def load_trace_to_text(platform): |
| 49 | sha_value = TRACE_TO_TEXT_SHAS[platform] |
| 50 | file_name = 'trace_to_text-' + platform + '-' + sha_value |
| 51 | local_file = os.path.join(TRACE_TO_TEXT_PATH, file_name) |
| 52 | |
| 53 | if os.path.exists(local_file): |
| 54 | if not check_hash(local_file, sha_value): |
| 55 | os.remove(local_file) |
| 56 | else: |
| 57 | return local_file |
| 58 | |
| 59 | url = TRACE_TO_TEXT_BASE_URL + file_name |
| 60 | urllib.urlretrieve(url, local_file) |
| 61 | if not check_hash(local_file, sha_value): |
| 62 | os.remove(local_file) |
| 63 | raise ValueError("Invalid signature.") |
| 64 | os.chmod(local_file, 0o755) |
| 65 | return local_file |
| 66 | |
| 67 | def main(argv): |
| 68 | platform = None |
| 69 | if sys.platform.startswith('linux'): |
| 70 | platform = 'linux' |
| 71 | elif sys.platform.startswith('darwin'): |
| 72 | platform = 'mac' |
| 73 | else: |
| 74 | print("Invalid platform: {}".format(sys.platform)) |
| 75 | return 1 |
| 76 | |
| 77 | trace_to_text_binary = load_trace_to_text(platform) |
| 78 | os.execv(trace_to_text_binary, [trace_to_text_binary] + argv[1:]) |
| 79 | |
| 80 | if __name__ == '__main__': |
| 81 | sys.exit(main(sys.argv)) |
| 82 | |
| 83 | #EOF |