blob: 71675edb170e21aaa2bfbf574fcdd6f1a1fa61be [file] [log] [blame]
Hector Dearman998741d2019-02-22 11:30:03 +00001#!/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=""" "
Hector Dearmanbd332762019-02-22 14:42:47 +000024exec python - "$@" <<'#'EOF
Hector Dearman998741d2019-02-22 11:30:03 +000025#"""
26
27import hashlib
28import os
29import sys
30import tempfile
31import urllib
32
Lalit Maganti1ba0d512019-05-21 19:23:38 +010033# Keep this in sync with the SHAs in catapult file
34# systrace/systrace/tracing_agents/atrace_from_file_agent.py.
Hector Dearman998741d2019-02-22 11:30:03 +000035TRACE_TO_TEXT_SHAS = {
Isabelle Taylor20d5fb62019-06-27 14:06:13 +010036 'linux': 'd67f9f3e3beb93664578c8b0300ab98e828ecb29',
37 'mac': '62b27f343640d0778f1324fa90940381561bef99',
Hector Dearman998741d2019-02-22 11:30:03 +000038}
39TRACE_TO_TEXT_PATH = tempfile.gettempdir()
40TRACE_TO_TEXT_BASE_URL = (
41 'https://storage.googleapis.com/perfetto/')
42
43def check_hash(file_name, sha_value):
44 with open(file_name, 'rb') as fd:
45 file_hash = hashlib.sha1(fd.read()).hexdigest()
46 return file_hash == sha_value
47
48def load_trace_to_text(platform):
49 sha_value = TRACE_TO_TEXT_SHAS[platform]
50 file_name = 'trace_to_text-' + platform + '-' + sha_value
51 local_file = os.path.join(TRACE_TO_TEXT_PATH, file_name)
52
53 if os.path.exists(local_file):
54 if not check_hash(local_file, sha_value):
55 os.remove(local_file)
56 else:
57 return local_file
58
59 url = TRACE_TO_TEXT_BASE_URL + file_name
60 urllib.urlretrieve(url, local_file)
61 if not check_hash(local_file, sha_value):
62 os.remove(local_file)
63 raise ValueError("Invalid signature.")
64 os.chmod(local_file, 0o755)
65 return local_file
66
67def main(argv):
68 platform = None
69 if sys.platform.startswith('linux'):
70 platform = 'linux'
71 elif sys.platform.startswith('darwin'):
72 platform = 'mac'
73 else:
74 print("Invalid platform: {}".format(sys.platform))
75 return 1
76
77 trace_to_text_binary = load_trace_to_text(platform)
78 os.execv(trace_to_text_binary, [trace_to_text_binary] + argv[1:])
79
80if __name__ == '__main__':
81 sys.exit(main(sys.argv))
82
83#EOF