blob: b33f9112bdb3ede19695c015b4bb029a3c15ebfa [file] [log] [blame]
Wei Li340ee8e2022-03-18 17:33:24 -07001#!/usr/bin/env python3
2#
3# Copyright (C) 2022 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17import argparse
18import hashlib
19import sys
20
21import google.protobuf.text_format as text_format
22import provenance_metadata_pb2
23
24def Log(*info):
25 if args.verbose:
26 for i in info:
27 print(i)
28
29def ParseArgs(argv):
30 parser = argparse.ArgumentParser(description='Create provenance metadata for a prebuilt artifact')
31 parser.add_argument('-v', '--verbose', action='store_true', help='Print more information in execution')
32 parser.add_argument('--module_name', help='Module name', required=True)
33 parser.add_argument('--artifact_path', help='Relative path of the prebuilt artifact in source tree', required=True)
34 parser.add_argument('--install_path', help='Absolute path of the artifact in the filesystem images', required=True)
35 parser.add_argument('--metadata_path', help='Path of the provenance metadata file created for the artifact', required=True)
36 return parser.parse_args(argv)
37
38def main(argv):
39 global args
40 args = ParseArgs(argv)
41 Log("Args:", vars(args))
42
43 provenance_metadata = provenance_metadata_pb2.ProvenanceMetadata()
44 provenance_metadata.module_name = args.module_name
45 provenance_metadata.artifact_path = args.artifact_path
46 provenance_metadata.artifact_install_path = args.install_path
47
48 Log("Generating SHA256 hash")
49 h = hashlib.sha256()
50 with open(args.artifact_path, "rb") as artifact_file:
51 h.update(artifact_file.read())
52 provenance_metadata.artifact_sha256 = h.hexdigest()
53
54 text_proto = [
55 "# proto-file: build/soong/provenance/proto/provenance_metadata.proto",
56 "# proto-message: ProvenanceMetaData",
57 "",
58 text_format.MessageToString(provenance_metadata)
59 ]
60 with open(args.metadata_path, "wt") as metadata_file:
61 file_content = "\n".join(text_proto)
62 Log("Writing provenance metadata in textproto:", file_content)
63 metadata_file.write(file_content)
64
65if __name__ == '__main__':
66 main(sys.argv[1:])