Lalit Maganti | 5eacf42 | 2020-06-30 13:51:23 +0100 | [diff] [blame] | 1 | # Copyright (C) 2020 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | from __future__ import absolute_import |
| 16 | |
| 17 | import os |
| 18 | import subprocess |
| 19 | import tempfile |
| 20 | |
Andrew Shulaev | 94d1bf2 | 2021-01-15 15:30:42 +0000 | [diff] [blame] | 21 | from google.protobuf import descriptor, descriptor_pb2, message_factory, descriptor_pool |
Lalit Maganti | 5eacf42 | 2020-06-30 13:51:23 +0100 | [diff] [blame] | 22 | from google.protobuf import reflection, text_format |
| 23 | |
| 24 | |
Lalit Maganti | df50009 | 2020-07-16 23:56:24 +0100 | [diff] [blame] | 25 | ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 26 | |
| 27 | |
Deepanjan Roy | 5d56d4c | 2021-02-10 10:53:36 -0500 | [diff] [blame] | 28 | def create_message_factory(descriptor_file_paths, proto_type): |
Lalit Maganti | 907f812 | 2021-02-03 18:32:14 +0000 | [diff] [blame] | 29 | pool = descriptor_pool.DescriptorPool() |
Deepanjan Roy | 5d56d4c | 2021-02-10 10:53:36 -0500 | [diff] [blame] | 30 | for file_path in descriptor_file_paths: |
| 31 | descriptor = read_descriptor(file_path) |
| 32 | for file in descriptor.file: |
| 33 | pool.Add(file) |
Lalit Maganti | 5eacf42 | 2020-06-30 13:51:23 +0100 | [diff] [blame] | 34 | |
Lalit Maganti | 907f812 | 2021-02-03 18:32:14 +0000 | [diff] [blame] | 35 | return message_factory.MessageFactory().GetPrototype( |
| 36 | pool.FindMessageTypeByName(proto_type)) |
Lalit Maganti | 5eacf42 | 2020-06-30 13:51:23 +0100 | [diff] [blame] | 37 | |
| 38 | |
Andrew Shulaev | 94d1bf2 | 2021-01-15 15:30:42 +0000 | [diff] [blame] | 39 | def read_descriptor(file_name): |
| 40 | with open(file_name, 'rb') as f: |
| 41 | contents = f.read() |
| 42 | |
| 43 | descriptor = descriptor_pb2.FileDescriptorSet() |
| 44 | descriptor.MergeFromString(contents) |
| 45 | |
| 46 | return descriptor |
| 47 | |
| 48 | |
| 49 | def serialize_textproto_trace(trace_descriptor_path, extension_descriptor_paths, |
| 50 | text_proto_path, out_stream): |
Deepanjan Roy | 5d56d4c | 2021-02-10 10:53:36 -0500 | [diff] [blame] | 51 | proto = create_message_factory([trace_descriptor_path] + |
| 52 | extension_descriptor_paths, |
| 53 | 'perfetto.protos.Trace')() |
Andrew Shulaev | 94d1bf2 | 2021-01-15 15:30:42 +0000 | [diff] [blame] | 54 | |
Lalit Maganti | 5eacf42 | 2020-06-30 13:51:23 +0100 | [diff] [blame] | 55 | with open(text_proto_path, 'r') as text_proto_file: |
| 56 | text_format.Merge(text_proto_file.read(), proto) |
| 57 | out_stream.write(proto.SerializeToString()) |
| 58 | out_stream.flush() |
| 59 | |
| 60 | |
| 61 | def serialize_python_trace(trace_descriptor_path, python_trace_path, |
| 62 | out_stream): |
Andrew Shulaev | 94d1bf2 | 2021-01-15 15:30:42 +0000 | [diff] [blame] | 63 | python_cmd = [ |
| 64 | 'python3', |
| 65 | python_trace_path, |
| 66 | trace_descriptor_path, |
| 67 | ] |
Lalit Maganti | df50009 | 2020-07-16 23:56:24 +0100 | [diff] [blame] | 68 | |
| 69 | # Add the test dir to the PYTHONPATH to allow synth_common to be found. |
| 70 | env = os.environ.copy() |
| 71 | if 'PYTHONPATH' in env: |
| 72 | env['PYTHONPATH'] = "{}:{}".format( |
| 73 | os.path.join(ROOT_DIR, 'test'), env['PYTHONPATH']) |
| 74 | else: |
| 75 | env['PYTHONPATH'] = os.path.join(ROOT_DIR, 'test') |
| 76 | subprocess.check_call(python_cmd, env=env, stdout=out_stream) |