Primiano Tucci | 34bc559 | 2021-02-19 17:53:36 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Hector Dearman | b7fa544 | 2018-11-08 18:39:32 +0000 | [diff] [blame] | 2 | # Copyright (C) 2018 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 | from __future__ import absolute_import |
| 17 | from __future__ import division |
| 18 | from __future__ import print_function |
| 19 | import os |
| 20 | import re |
Hector Dearman | b7fa544 | 2018-11-08 18:39:32 +0000 | [diff] [blame] | 21 | import argparse |
| 22 | import tempfile |
| 23 | import subprocess |
| 24 | import hashlib |
Matthew Clarkson | 9a5dfa5 | 2019-10-03 09:54:04 +0100 | [diff] [blame] | 25 | from compat import iteritems |
Hector Dearman | b7fa544 | 2018-11-08 18:39:32 +0000 | [diff] [blame] | 26 | |
Lalit Maganti | 38365dc | 2020-08-04 13:33:17 +0100 | [diff] [blame] | 27 | SOURCE_TARGET = [ |
| 28 | ( |
Lalit Maganti | 38365dc | 2020-08-04 13:33:17 +0100 | [diff] [blame] | 29 | 'protos/perfetto/trace_processor/trace_processor.proto', |
Anindita Ghosh | 456261f | 2020-09-07 11:31:24 +0100 | [diff] [blame] | 30 | 'src/trace_processor/python/perfetto/trace_processor/trace_processor.descriptor' |
Lalit Maganti | 38365dc | 2020-08-04 13:33:17 +0100 | [diff] [blame] | 31 | ), |
| 32 | ( |
| 33 | 'protos/perfetto/metrics/metrics.proto', |
Anindita Ghosh | 456261f | 2020-09-07 11:31:24 +0100 | [diff] [blame] | 34 | 'src/trace_processor/python/perfetto/trace_processor/metrics.descriptor' |
Lalit Maganti | 38365dc | 2020-08-04 13:33:17 +0100 | [diff] [blame] | 35 | ), |
| 36 | ] |
Hector Dearman | b7fa544 | 2018-11-08 18:39:32 +0000 | [diff] [blame] | 37 | |
| 38 | ROOT_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) |
| 39 | |
| 40 | SCRIPT_PATH = 'tools/gen_binary_descriptors' |
| 41 | |
Hector Dearman | 1e26914 | 2018-11-14 13:53:08 +0000 | [diff] [blame] | 42 | |
Hector Dearman | b7fa544 | 2018-11-08 18:39:32 +0000 | [diff] [blame] | 43 | def hash_path(path): |
| 44 | hash = hashlib.sha1() |
Matthew Clarkson | 9a5dfa5 | 2019-10-03 09:54:04 +0100 | [diff] [blame] | 45 | with open(os.path.join(ROOT_DIR, path), 'rb') as f: |
Hector Dearman | b7fa544 | 2018-11-08 18:39:32 +0000 | [diff] [blame] | 46 | hash.update(f.read()) |
| 47 | return hash.hexdigest() |
| 48 | |
| 49 | |
Hector Dearman | 1e26914 | 2018-11-14 13:53:08 +0000 | [diff] [blame] | 50 | def find_protoc(): |
Primiano Tucci | 561ba7e | 2019-10-10 21:23:11 +0100 | [diff] [blame] | 51 | for root, _, files in os.walk(os.path.join(ROOT_DIR, 'out')): |
Hector Dearman | 1e26914 | 2018-11-14 13:53:08 +0000 | [diff] [blame] | 52 | if 'protoc' in files: |
| 53 | return os.path.join(root, 'protoc') |
Hector Dearman | 1e26914 | 2018-11-14 13:53:08 +0000 | [diff] [blame] | 54 | return None |
| 55 | |
| 56 | |
Lalit Maganti | 2939c08 | 2021-03-11 17:25:44 +0000 | [diff] [blame^] | 57 | |
| 58 | def check(source, target): |
| 59 | assert os.path.exists(os.path.join(ROOT_DIR, target)), \ |
| 60 | 'Output file {} does not exist and so cannot be checked'.format(target) |
| 61 | |
| 62 | sha1_file = target + '.sha1' |
| 63 | assert os.path.exists(sha1_file), \ |
| 64 | 'SHA1 file {} does not exist and so cannot be checked'.format(sha1_file) |
| 65 | |
| 66 | with open(sha1_file, 'rb') as f: |
Hector Dearman | b7fa544 | 2018-11-08 18:39:32 +0000 | [diff] [blame] | 67 | s = f.read() |
| 68 | |
Matthew Clarkson | 9a5dfa5 | 2019-10-03 09:54:04 +0100 | [diff] [blame] | 69 | hashes = re.findall(r'// SHA1\((.*)\)\n// (.*)\n', s.decode()) |
Hector Dearman | b7fa544 | 2018-11-08 18:39:32 +0000 | [diff] [blame] | 70 | assert sorted([SCRIPT_PATH, source]) == sorted([key for key, _ in hashes]) |
| 71 | for path, expected_sha1 in hashes: |
| 72 | actual_sha1 = hash_path(os.path.join(ROOT_DIR, path)) |
| 73 | assert actual_sha1 == expected_sha1, \ |
Hector Dearman | 1e26914 | 2018-11-14 13:53:08 +0000 | [diff] [blame] | 74 | 'In {} hash given for {} did not match'.format(target, path) |
Hector Dearman | b7fa544 | 2018-11-08 18:39:32 +0000 | [diff] [blame] | 75 | |
| 76 | |
Lalit Maganti | 82a2c04 | 2020-07-06 13:50:33 +0100 | [diff] [blame] | 77 | def generate(source, target, protoc_path): |
| 78 | with tempfile.NamedTemporaryFile() as fdescriptor: |
| 79 | subprocess.check_call([ |
| 80 | protoc_path, |
| 81 | '--include_imports', |
| 82 | '--proto_path=.', |
| 83 | '--proto_path=' + \ |
| 84 | os.path.join(ROOT_DIR, "buildtools", "protobuf", "src"), |
| 85 | '--descriptor_set_out={}'.format(fdescriptor.name), |
| 86 | source, |
| 87 | ], |
| 88 | cwd=ROOT_DIR) |
| 89 | |
| 90 | s = fdescriptor.read() |
Lalit Maganti | 2939c08 | 2021-03-11 17:25:44 +0000 | [diff] [blame^] | 91 | with open(target, 'wb') as out: |
| 92 | out.write(s) |
| 93 | |
| 94 | sha1_path = target + '.sha1' |
| 95 | with open(sha1_path, 'wb') as c: |
| 96 | c.write(""" |
| 97 | // SHA1({script_path}) |
| 98 | // {script_hash} |
| 99 | // SHA1({source_path}) |
| 100 | // {source_hash} |
| 101 | """.format( |
| 102 | script_path=SCRIPT_PATH, |
| 103 | script_hash=hash_path(__file__), |
| 104 | source_path=source, |
| 105 | source_hash=hash_path(os.path.join(source)), |
| 106 | ).encode()) |
Lalit Maganti | 82a2c04 | 2020-07-06 13:50:33 +0100 | [diff] [blame] | 107 | |
Hector Dearman | b7fa544 | 2018-11-08 18:39:32 +0000 | [diff] [blame] | 108 | def main(): |
| 109 | parser = argparse.ArgumentParser() |
| 110 | parser.add_argument('--check-only', action='store_true') |
| 111 | parser.add_argument('--protoc') |
| 112 | args = parser.parse_args() |
| 113 | |
Hector Dearman | 1e26914 | 2018-11-14 13:53:08 +0000 | [diff] [blame] | 114 | try: |
Lalit Maganti | 38365dc | 2020-08-04 13:33:17 +0100 | [diff] [blame] | 115 | for source, target in SOURCE_TARGET: |
Hector Dearman | 1e26914 | 2018-11-14 13:53:08 +0000 | [diff] [blame] | 116 | if args.check_only: |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 117 | check(source, target) |
Hector Dearman | 1e26914 | 2018-11-14 13:53:08 +0000 | [diff] [blame] | 118 | else: |
| 119 | protoc = args.protoc or find_protoc() |
| 120 | assert protoc, 'protoc not found specific (--protoc PROTOC_PATH)' |
| 121 | assert os.path.exists(protoc), '{} does not exist'.format(protoc) |
| 122 | if protoc is not args.protoc: |
| 123 | print('Using protoc: {}'.format(protoc)) |
| 124 | generate(source, target, protoc) |
| 125 | except AssertionError as e: |
| 126 | if not str(e): |
| 127 | raise |
| 128 | print('Error: {}'.format(e)) |
Hector Dearman | 7e07977 | 2018-11-15 16:08:12 +0000 | [diff] [blame] | 129 | return 1 |
Hector Dearman | b7fa544 | 2018-11-08 18:39:32 +0000 | [diff] [blame] | 130 | |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 131 | |
Hector Dearman | b7fa544 | 2018-11-08 18:39:32 +0000 | [diff] [blame] | 132 | if __name__ == '__main__': |
| 133 | exit(main()) |