blob: d6be734d51342548da01fefae025777ccf9c6b40 [file] [log] [blame]
Wei Lic134b762023-10-17 23:52:30 -07001#!/usr/bin/env python3
2#
3# Copyright (C) 2023 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 json
20import sbom_data
21import sbom_writers
22
23
24def get_args():
25 parser = argparse.ArgumentParser()
26 parser.add_argument('-v', '--verbose', action='store_true', default=False,
27 help='Print more information.')
28 parser.add_argument('--output_file', required=True,
29 help='The generated SBOM file in SPDX format.')
30 parser.add_argument('--layoutlib_sbom', required=True,
31 help='The file path of the SBOM of layoutlib.')
32
33 return parser.parse_args()
34
35
36def main():
37 global args
38 args = get_args()
39
40 doc = sbom_data.Document(name='<name>',
41 namespace='<namespace>',
42 creators=['Organization: <organization>'],
43 created='<created>')
44
45 filename = 'data/framework_res.jar'
46 file_id = f'SPDXRef-{sbom_data.encode_for_spdxid(filename)}'
47 file = sbom_data.File(id=file_id, name=filename, checksum='SHA1: <checksum>')
48 doc.files.append(file)
49 doc.describes = file_id
50
51 with open(args.layoutlib_sbom, 'r', encoding='utf-8') as f:
52 layoutlib_sbom = json.load(f)
53
54 with open(args.layoutlib_sbom, 'rb') as f:
55 sha1 = hashlib.file_digest(f, 'sha1')
56
57 layoutlib_sbom_namespace = layoutlib_sbom[sbom_writers.PropNames.DOCUMENT_NAMESPACE]
58 external_doc_ref = 'DocumentRef-layoutlib'
59 doc.external_refs = [
60 sbom_data.DocumentExternalReference(external_doc_ref, layoutlib_sbom_namespace,
61 f'SHA1: {sha1.hexdigest()}')]
62
63 resource_file_spdxids = []
64 for file in layoutlib_sbom[sbom_writers.PropNames.FILES]:
65 if file[sbom_writers.PropNames.FILE_NAME].startswith('data/res/'):
66 resource_file_spdxids.append(file[sbom_writers.PropNames.SPDXID])
67
68 doc.relationships = []
69 for spdxid in resource_file_spdxids:
70 doc.relationships.append(
71 sbom_data.Relationship(file_id, sbom_data.RelationshipType.GENERATED_FROM,
72 f'{external_doc_ref}:{spdxid}'))
73
74 # write sbom file
75 with open(args.output_file, 'w', encoding='utf-8') as f:
76 sbom_writers.JSONWriter.write(doc, f)
77
78
79if __name__ == '__main__':
80 main()