Lalit Maganti | 3147909 | 2019-05-30 20:29:24 +0100 | [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 | # ./trace_processor |
| 18 | # python trace_processor |
| 19 | # bash trace_processor |
| 20 | # cat ./trace_processor | bash |
| 21 | # cat ./trace_processor | python - |
| 22 | |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame] | 23 | BASH_FALLBACK = """ " |
Lalit Maganti | 3147909 | 2019-05-30 20:29:24 +0100 | [diff] [blame] | 24 | exec python - "$@" <<'#'EOF |
| 25 | #""" |
| 26 | |
| 27 | import hashlib |
| 28 | import os |
| 29 | import sys |
| 30 | import tempfile |
Lalit Maganti | 2783248 | 2020-05-18 12:10:50 +0100 | [diff] [blame^] | 31 | import subprocess |
Lalit Maganti | 3147909 | 2019-05-30 20:29:24 +0100 | [diff] [blame] | 32 | |
| 33 | TRACE_PROCESSOR_SHELL_SHAS = { |
Lalit Maganti | 8f1b35e | 2020-05-11 16:46:40 +0100 | [diff] [blame] | 34 | 'linux': '3a28532ba5cb1b94cea8a563bbfa7d16175f208d', |
| 35 | 'mac': '637fe7ac61f1700edd2fbaa76e0775ea97f4c394', |
Lalit Maganti | 3147909 | 2019-05-30 20:29:24 +0100 | [diff] [blame] | 36 | } |
| 37 | TRACE_PROCESSOR_SHELL_PATH = tempfile.gettempdir() |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame] | 38 | TRACE_PROCESSOR_SHELL_BASE_URL = ('https://storage.googleapis.com/perfetto/') |
| 39 | |
Lalit Maganti | 3147909 | 2019-05-30 20:29:24 +0100 | [diff] [blame] | 40 | |
Lalit Maganti | 2783248 | 2020-05-18 12:10:50 +0100 | [diff] [blame^] | 41 | def DownloadURL(url, out_file): |
| 42 | subprocess.check_call(['curl', '-L', '-#', '-o', out_file, url]) |
| 43 | |
| 44 | |
Lalit Maganti | 3147909 | 2019-05-30 20:29:24 +0100 | [diff] [blame] | 45 | def check_hash(file_name, sha_value): |
| 46 | with open(file_name, 'rb') as fd: |
| 47 | file_hash = hashlib.sha1(fd.read()).hexdigest() |
| 48 | return file_hash == sha_value |
| 49 | |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame] | 50 | |
Lalit Maganti | 3147909 | 2019-05-30 20:29:24 +0100 | [diff] [blame] | 51 | def load_trace_processor_shell(platform): |
| 52 | sha_value = TRACE_PROCESSOR_SHELL_SHAS[platform] |
| 53 | file_name = 'trace_processor_shell-' + platform + '-' + sha_value |
| 54 | local_file = os.path.join(TRACE_PROCESSOR_SHELL_PATH, file_name) |
| 55 | |
| 56 | if os.path.exists(local_file): |
| 57 | if not check_hash(local_file, sha_value): |
| 58 | os.remove(local_file) |
| 59 | else: |
| 60 | return local_file |
| 61 | |
| 62 | url = TRACE_PROCESSOR_SHELL_BASE_URL + file_name |
Lalit Maganti | 2783248 | 2020-05-18 12:10:50 +0100 | [diff] [blame^] | 63 | DownloadURL(url, local_file) |
Lalit Maganti | 3147909 | 2019-05-30 20:29:24 +0100 | [diff] [blame] | 64 | if not check_hash(local_file, sha_value): |
| 65 | os.remove(local_file) |
| 66 | raise ValueError("Invalid signature.") |
| 67 | os.chmod(local_file, 0o755) |
| 68 | return local_file |
| 69 | |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame] | 70 | |
Lalit Maganti | 3147909 | 2019-05-30 20:29:24 +0100 | [diff] [blame] | 71 | def main(argv): |
| 72 | platform = None |
| 73 | if sys.platform.startswith('linux'): |
| 74 | platform = 'linux' |
| 75 | elif sys.platform.startswith('darwin'): |
| 76 | platform = 'mac' |
| 77 | else: |
| 78 | print("Invalid platform: {}".format(sys.platform)) |
| 79 | return 1 |
| 80 | |
| 81 | trace_processor_shell_binary = load_trace_processor_shell(platform) |
| 82 | os.execv(trace_processor_shell_binary, |
| 83 | [trace_processor_shell_binary] + argv[1:]) |
| 84 | |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame] | 85 | |
Lalit Maganti | 3147909 | 2019-05-30 20:29:24 +0100 | [diff] [blame] | 86 | if __name__ == '__main__': |
| 87 | sys.exit(main(sys.argv)) |
| 88 | |
| 89 | #EOF |