blob: 533d24d1d3bfba3ea18ea3f716e482dbe7625e68 [file] [log] [blame]
Lalit Maganti31479092019-05-30 20:29:24 +01001#!/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# ./trace_processor
18# python trace_processor
19# bash trace_processor
20# cat ./trace_processor | bash
21# cat ./trace_processor | python -
22
Matthew Clarkson63028d62019-10-10 15:48:23 +010023BASH_FALLBACK = """ "
Lalit Maganti31479092019-05-30 20:29:24 +010024exec python - "$@" <<'#'EOF
25#"""
26
27import hashlib
28import os
29import sys
30import tempfile
Lalit Maganti27832482020-05-18 12:10:50 +010031import subprocess
Lalit Maganti31479092019-05-30 20:29:24 +010032
33TRACE_PROCESSOR_SHELL_SHAS = {
Lalit Maganti8f1b35e2020-05-11 16:46:40 +010034 'linux': '3a28532ba5cb1b94cea8a563bbfa7d16175f208d',
35 'mac': '637fe7ac61f1700edd2fbaa76e0775ea97f4c394',
Lalit Maganti31479092019-05-30 20:29:24 +010036}
37TRACE_PROCESSOR_SHELL_PATH = tempfile.gettempdir()
Matthew Clarkson63028d62019-10-10 15:48:23 +010038TRACE_PROCESSOR_SHELL_BASE_URL = ('https://storage.googleapis.com/perfetto/')
39
Lalit Maganti31479092019-05-30 20:29:24 +010040
Lalit Maganti27832482020-05-18 12:10:50 +010041def DownloadURL(url, out_file):
42 subprocess.check_call(['curl', '-L', '-#', '-o', out_file, url])
43
44
Lalit Maganti31479092019-05-30 20:29:24 +010045def check_hash(file_name, sha_value):
46 with open(file_name, 'rb') as fd:
47 file_hash = hashlib.sha1(fd.read()).hexdigest()
48 return file_hash == sha_value
49
Matthew Clarkson63028d62019-10-10 15:48:23 +010050
Lalit Maganti31479092019-05-30 20:29:24 +010051def load_trace_processor_shell(platform):
52 sha_value = TRACE_PROCESSOR_SHELL_SHAS[platform]
53 file_name = 'trace_processor_shell-' + platform + '-' + sha_value
54 local_file = os.path.join(TRACE_PROCESSOR_SHELL_PATH, file_name)
55
56 if os.path.exists(local_file):
57 if not check_hash(local_file, sha_value):
58 os.remove(local_file)
59 else:
60 return local_file
61
62 url = TRACE_PROCESSOR_SHELL_BASE_URL + file_name
Lalit Maganti27832482020-05-18 12:10:50 +010063 DownloadURL(url, local_file)
Lalit Maganti31479092019-05-30 20:29:24 +010064 if not check_hash(local_file, sha_value):
65 os.remove(local_file)
66 raise ValueError("Invalid signature.")
67 os.chmod(local_file, 0o755)
68 return local_file
69
Matthew Clarkson63028d62019-10-10 15:48:23 +010070
Lalit Maganti31479092019-05-30 20:29:24 +010071def main(argv):
72 platform = None
73 if sys.platform.startswith('linux'):
74 platform = 'linux'
75 elif sys.platform.startswith('darwin'):
76 platform = 'mac'
77 else:
78 print("Invalid platform: {}".format(sys.platform))
79 return 1
80
81 trace_processor_shell_binary = load_trace_processor_shell(platform)
82 os.execv(trace_processor_shell_binary,
83 [trace_processor_shell_binary] + argv[1:])
84
Matthew Clarkson63028d62019-10-10 15:48:23 +010085
Lalit Maganti31479092019-05-30 20:29:24 +010086if __name__ == '__main__':
87 sys.exit(main(sys.argv))
88
89#EOF