| #!/usr/bin/env python | 
 | # Copyright (C) 2019 The Android Open Source Project | 
 | # | 
 | # Licensed under the Apache License, Version 2.0 (the "License"); | 
 | # you may not use this file except in compliance with the License. | 
 | # You may obtain a copy of the License at | 
 | # | 
 | #      http://www.apache.org/licenses/LICENSE-2.0 | 
 | # | 
 | # Unless required by applicable law or agreed to in writing, software | 
 | # distributed under the License is distributed on an "AS IS" BASIS, | 
 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
 | # See the License for the specific language governing permissions and | 
 | # limitations under the License. | 
 |  | 
 | # This file should do the same thing when being invoked in any of these ways: | 
 | # ./traceconv | 
 | # python traceconv | 
 | # bash traceconv | 
 | # cat ./traceconv | bash | 
 | # cat ./traceconv | python - | 
 |  | 
 | BASH_FALLBACK = """ " | 
 | exec python - "$@" <<'#'EOF | 
 | #""" | 
 |  | 
 | import hashlib | 
 | import os | 
 | import sys | 
 | import subprocess | 
 | import tempfile | 
 |  | 
 |  | 
 | # Keep this in sync with the SHAs in catapult file | 
 | # systrace/systrace/tracing_agents/atrace_from_file_agent.py. | 
 | TRACE_TO_TEXT_SHAS = { | 
 |     'linux': '2f4ee64e3f2135f698ee665d91c489e129fe7859', | 
 |     'mac': 'd000e01827c439357a4d6e6c5b1076357e561a26', | 
 | } | 
 | TRACE_TO_TEXT_PATH = tempfile.gettempdir() | 
 | TRACE_TO_TEXT_BASE_URL = ('https://storage.googleapis.com/perfetto/') | 
 |  | 
 |  | 
 | def DownloadURL(url, out_file): | 
 |   subprocess.check_call(['curl', '-L', '-#', '-o', out_file, url]) | 
 |  | 
 |  | 
 | def check_hash(file_name, sha_value): | 
 |   with open(file_name, 'rb') as fd: | 
 |     file_hash = hashlib.sha1(fd.read()).hexdigest() | 
 |     return file_hash == sha_value | 
 |  | 
 |  | 
 | def load_trace_to_text(platform): | 
 |   sha_value = TRACE_TO_TEXT_SHAS[platform] | 
 |   file_name = 'trace_to_text-' + platform + '-' + sha_value | 
 |   local_file = os.path.join(TRACE_TO_TEXT_PATH, file_name) | 
 |  | 
 |   if os.path.exists(local_file): | 
 |     if not check_hash(local_file, sha_value): | 
 |       os.remove(local_file) | 
 |     else: | 
 |       return local_file | 
 |  | 
 |   url = TRACE_TO_TEXT_BASE_URL + file_name | 
 |   DownloadURL(url, local_file) | 
 |   if not check_hash(local_file, sha_value): | 
 |     os.remove(local_file) | 
 |     raise ValueError("Invalid signature.") | 
 |   os.chmod(local_file, 0o755) | 
 |   return local_file | 
 |  | 
 |  | 
 | def main(argv): | 
 |   platform = None | 
 |   if sys.platform.startswith('linux'): | 
 |     platform = 'linux' | 
 |   elif sys.platform.startswith('darwin'): | 
 |     platform = 'mac' | 
 |   else: | 
 |     print("Invalid platform: {}".format(sys.platform)) | 
 |     return 1 | 
 |  | 
 |   trace_to_text_binary = load_trace_to_text(platform) | 
 |   os.execv(trace_to_text_binary, [trace_to_text_binary] + argv[1:]) | 
 |  | 
 |  | 
 | if __name__ == '__main__': | 
 |   sys.exit(main(sys.argv)) | 
 |  | 
 | #EOF |