1#!/usr/bin/env python3 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 23BASH_FALLBACK = """ " 24exec python3 - "$@" <<'#'EOF 25#""" 26 27import hashlib 28import os 29import sys 30import subprocess 31import tempfile 32import platform 33 34 35TRACE_TO_TEXT_SHAS = { 36 'linux': '7e3e10dfb324e31723efd63ac25037856e06eba0', 37 'mac': '21f0f42dd019b4f09addd404a114fbf2322ca8a4', 38} 39TRACE_TO_TEXT_PATH = tempfile.gettempdir() 40TRACE_TO_TEXT_BASE_URL = ('https://storage.googleapis.com/perfetto/') 41 42 43def DownloadURL(url, out_file): 44 subprocess.check_call(['curl', '-L', '-#', '-o', out_file, url]) 45 46 47def check_hash(file_name, sha_value): 48 with open(file_name, 'rb') as fd: 49 file_hash = hashlib.sha1(fd.read()).hexdigest() 50 return file_hash == sha_value 51 52 53def load_trace_to_text(platform): 54 sha_value = TRACE_TO_TEXT_SHAS[platform] 55 file_name = 'trace_to_text-' + platform + '-' + sha_value 56 local_file = os.path.join(TRACE_TO_TEXT_PATH, file_name) 57 58 if os.path.exists(local_file): 59 if not check_hash(local_file, sha_value): 60 os.remove(local_file) 61 else: 62 return local_file 63 64 url = TRACE_TO_TEXT_BASE_URL + file_name 65 DownloadURL(url, local_file) 66 if not check_hash(local_file, sha_value): 67 os.remove(local_file) 68 raise ValueError("Invalid signature.") 69 os.chmod(local_file, 0o755) 70 return local_file 71 72 73def main(argv): 74 os_name = None 75 if sys.platform.startswith('linux'): 76 os_name = 'linux' 77 elif sys.platform.startswith('darwin'): 78 os_name = 'mac' 79 else: 80 print("Invalid platform: {}".format(sys.platform)) 81 return 1 82 83 arch = platform.machine() 84 if arch not in ['x86_64', 'amd64']: 85 print("Prebuilts are only available for x86_64 (found '{}' instead).".format(arch)) 86 print("Follow https://perfetto.dev/docs/contributing/build-instructions to build locally.") 87 return 1 88 89 trace_to_text_binary = load_trace_to_text(os_name) 90 os.execv(trace_to_text_binary, [trace_to_text_binary] + argv[1:]) 91 92 93if __name__ == '__main__': 94 sys.exit(main(sys.argv)) 95 96#EOF 97