aboutsummaryrefslogtreecommitdiff
path: root/tools/trace_processor
blob: 22aaab63b4f5c600f57f1fd4c44be47367e881a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python3
# 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:
# ./trace_processor
# python trace_processor
# bash trace_processor
# cat ./trace_processor | bash
# cat ./trace_processor | python -

BASH_FALLBACK = """ "
exec python3 - "$@" <<'#'EOF
#"""

import hashlib
import os
import sys
import tempfile
import subprocess
import platform

TRACE_PROCESSOR_SHELL_SHAS = {
    'linux': 'a3ce2cbf4cbe4f86cc10b02957db727cecfafae8',
    'mac': 'c39a5be9a3831911ef2e50d66d11c12d877688f3',
}
TRACE_PROCESSOR_SHELL_PATH = tempfile.gettempdir()
TRACE_PROCESSOR_SHELL_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_processor_shell(platform):
  sha_value = TRACE_PROCESSOR_SHELL_SHAS[platform]
  file_name = 'trace_processor_shell-' + platform + '-' + sha_value
  local_file = os.path.join(TRACE_PROCESSOR_SHELL_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_PROCESSOR_SHELL_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):
  os_name = None
  if sys.platform.startswith('linux'):
    os_name = 'linux'
  elif sys.platform.startswith('darwin'):
    os_name = 'mac'
  else:
    print("Invalid platform: {}".format(sys.platform))
    return 1

  arch = platform.machine()
  if arch not in ['x86_64', 'amd64']:
    print("Prebuilts are only available for x86_64 (found '{}' instead).".format(arch))
    print("Follow https://perfetto.dev/docs/contributing/build-instructions to build locally.")
    return 1

  trace_processor_shell_binary = load_trace_processor_shell(os_name)
  os.execv(trace_processor_shell_binary,
           [trace_processor_shell_binary] + argv[1:])


if __name__ == '__main__':
  sys.exit(main(sys.argv))

#EOF