Home | History | Annotate | Download | only in tools
      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=""" "
     24 exec python - "$@" <<'#'EOF
     25 #"""
     26 
     27 import hashlib
     28 import os
     29 import sys
     30 import tempfile
     31 import urllib
     32 
     33 TRACE_TO_TEXT_SHAS = {
     34   'linux': '91cbc8addb9adc4dcc0cbf398e7ff67a8ad14ece',
     35   'mac': '36a1313c8900b3b59b40e6f07488a6d2c7ffaa94',
     36 }
     37 TRACE_TO_TEXT_PATH = tempfile.gettempdir()
     38 TRACE_TO_TEXT_BASE_URL = (
     39     'https://storage.googleapis.com/perfetto/')
     40 
     41 def check_hash(file_name, sha_value):
     42   with open(file_name, 'rb') as fd:
     43     file_hash = hashlib.sha1(fd.read()).hexdigest()
     44     return file_hash == sha_value
     45 
     46 def load_trace_to_text(platform):
     47   sha_value = TRACE_TO_TEXT_SHAS[platform]
     48   file_name = 'trace_to_text-' + platform + '-' + sha_value
     49   local_file = os.path.join(TRACE_TO_TEXT_PATH, file_name)
     50 
     51   if os.path.exists(local_file):
     52     if not check_hash(local_file, sha_value):
     53       os.remove(local_file)
     54     else:
     55       return local_file
     56 
     57   url = TRACE_TO_TEXT_BASE_URL + file_name
     58   urllib.urlretrieve(url, local_file)
     59   if not check_hash(local_file, sha_value):
     60     os.remove(local_file)
     61     raise ValueError("Invalid signature.")
     62   os.chmod(local_file, 0o755)
     63   return local_file
     64 
     65 def main(argv):
     66   platform = None
     67   if sys.platform.startswith('linux'):
     68     platform = 'linux'
     69   elif sys.platform.startswith('darwin'):
     70     platform = 'mac'
     71   else:
     72     print("Invalid platform: {}".format(sys.platform))
     73     return 1
     74 
     75   trace_to_text_binary = load_trace_to_text(platform)
     76   os.execv(trace_to_text_binary, [trace_to_text_binary] + argv[1:])
     77 
     78 if __name__ == '__main__':
     79   sys.exit(main(sys.argv))
     80 
     81 #EOF
     82