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 23BASH_FALLBACK = """ " 24exec python - "$@" <<'#'EOF 25#""" 26 27import hashlib 28import os 29import sys 30import subprocess 31import tempfile 32 33 34# Keep this in sync with the SHAs in catapult file 35# systrace/systrace/tracing_agents/atrace_from_file_agent.py. 36TRACE_TO_TEXT_SHAS = { 37 'linux': '2f4ee64e3f2135f698ee665d91c489e129fe7859', 38 'mac': 'd000e01827c439357a4d6e6c5b1076357e561a26', 39} 40TRACE_TO_TEXT_PATH = tempfile.gettempdir() 41TRACE_TO_TEXT_BASE_URL = ('https://storage.googleapis.com/perfetto/') 42 43 44def DownloadURL(url, out_file): 45 subprocess.check_call(['curl', '-L', '-#', '-o', out_file, url]) 46 47 48def check_hash(file_name, sha_value): 49 with open(file_name, 'rb') as fd: 50 file_hash = hashlib.sha1(fd.read()).hexdigest() 51 return file_hash == sha_value 52 53 54def load_trace_to_text(platform): 55 sha_value = TRACE_TO_TEXT_SHAS[platform] 56 file_name = 'trace_to_text-' + platform + '-' + sha_value 57 local_file = os.path.join(TRACE_TO_TEXT_PATH, file_name) 58 59 if os.path.exists(local_file): 60 if not check_hash(local_file, sha_value): 61 os.remove(local_file) 62 else: 63 return local_file 64 65 url = TRACE_TO_TEXT_BASE_URL + file_name 66 DownloadURL(url, local_file) 67 if not check_hash(local_file, sha_value): 68 os.remove(local_file) 69 raise ValueError("Invalid signature.") 70 os.chmod(local_file, 0o755) 71 return local_file 72 73 74def main(argv): 75 platform = None 76 if sys.platform.startswith('linux'): 77 platform = 'linux' 78 elif sys.platform.startswith('darwin'): 79 platform = 'mac' 80 else: 81 print("Invalid platform: {}".format(sys.platform)) 82 return 1 83 84 trace_to_text_binary = load_trace_to_text(platform) 85 os.execv(trace_to_text_binary, [trace_to_text_binary] + argv[1:]) 86 87 88if __name__ == '__main__': 89 sys.exit(main(sys.argv)) 90 91#EOF 92