xref: /aosp_15_r20/build/make/tools/sbom/generate-sbom-framework_res.py (revision 9e94795a3d4ef5c1d47486f9a02bb378756cea8a)
1*9e94795aSAndroid Build Coastguard Worker#!/usr/bin/env python3
2*9e94795aSAndroid Build Coastguard Worker#
3*9e94795aSAndroid Build Coastguard Worker# Copyright (C) 2023 The Android Open Source Project
4*9e94795aSAndroid Build Coastguard Worker#
5*9e94795aSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
6*9e94795aSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
7*9e94795aSAndroid Build Coastguard Worker# You may obtain a copy of the License at
8*9e94795aSAndroid Build Coastguard Worker#
9*9e94795aSAndroid Build Coastguard Worker#      http://www.apache.org/licenses/LICENSE-2.0
10*9e94795aSAndroid Build Coastguard Worker#
11*9e94795aSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
12*9e94795aSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
13*9e94795aSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*9e94795aSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
15*9e94795aSAndroid Build Coastguard Worker# limitations under the License.
16*9e94795aSAndroid Build Coastguard Worker
17*9e94795aSAndroid Build Coastguard Workerimport argparse
18*9e94795aSAndroid Build Coastguard Workerimport hashlib
19*9e94795aSAndroid Build Coastguard Workerimport json
20*9e94795aSAndroid Build Coastguard Workerimport sbom_data
21*9e94795aSAndroid Build Coastguard Workerimport sbom_writers
22*9e94795aSAndroid Build Coastguard Worker
23*9e94795aSAndroid Build Coastguard Worker'''
24*9e94795aSAndroid Build Coastguard WorkerThis script generates SBOM of framework_res.jar of layoutlib shipped with Android Studio.
25*9e94795aSAndroid Build Coastguard Worker
26*9e94795aSAndroid Build Coastguard WorkerThe generated SBOM contains some placeholders which should be substituted by release_layoutlib.sh.
27*9e94795aSAndroid Build Coastguard WorkerThe placeholders include: document name, document namespace, organization, created timestamp and
28*9e94795aSAndroid Build Coastguard Workerthe SHA1 checksum of framework_res.jar.
29*9e94795aSAndroid Build Coastguard Worker'''
30*9e94795aSAndroid Build Coastguard Worker
31*9e94795aSAndroid Build Coastguard Workerdef get_args():
32*9e94795aSAndroid Build Coastguard Worker  parser = argparse.ArgumentParser()
33*9e94795aSAndroid Build Coastguard Worker  parser.add_argument('-v', '--verbose', action='store_true', default=False,
34*9e94795aSAndroid Build Coastguard Worker                      help='Print more information.')
35*9e94795aSAndroid Build Coastguard Worker  parser.add_argument('--output_file', required=True,
36*9e94795aSAndroid Build Coastguard Worker                      help='The generated SBOM file in SPDX format.')
37*9e94795aSAndroid Build Coastguard Worker  parser.add_argument('--layoutlib_sbom', required=True,
38*9e94795aSAndroid Build Coastguard Worker                      help='The file path of the SBOM of layoutlib.')
39*9e94795aSAndroid Build Coastguard Worker
40*9e94795aSAndroid Build Coastguard Worker  return parser.parse_args()
41*9e94795aSAndroid Build Coastguard Worker
42*9e94795aSAndroid Build Coastguard Worker
43*9e94795aSAndroid Build Coastguard Workerdef main():
44*9e94795aSAndroid Build Coastguard Worker  global args
45*9e94795aSAndroid Build Coastguard Worker  args = get_args()
46*9e94795aSAndroid Build Coastguard Worker
47*9e94795aSAndroid Build Coastguard Worker  doc = sbom_data.Document(name='<name>',
48*9e94795aSAndroid Build Coastguard Worker                           namespace='<namespace>',
49*9e94795aSAndroid Build Coastguard Worker                           creators=['Organization: <organization>'],
50*9e94795aSAndroid Build Coastguard Worker                           created='<created>')
51*9e94795aSAndroid Build Coastguard Worker
52*9e94795aSAndroid Build Coastguard Worker  filename = 'data/framework_res.jar'
53*9e94795aSAndroid Build Coastguard Worker  file_id = f'SPDXRef-{sbom_data.encode_for_spdxid(filename)}'
54*9e94795aSAndroid Build Coastguard Worker  file = sbom_data.File(id=file_id, name=filename, checksum='SHA1: <checksum>')
55*9e94795aSAndroid Build Coastguard Worker
56*9e94795aSAndroid Build Coastguard Worker  package_name = 'framework_res'
57*9e94795aSAndroid Build Coastguard Worker  package_id = f'SPDXRef-PREBUILT-{sbom_data.encode_for_spdxid(package_name)}'
58*9e94795aSAndroid Build Coastguard Worker  package = sbom_data.Package(id=package_id, name=package_name, version='<package_version>',
59*9e94795aSAndroid Build Coastguard Worker                    download_location=sbom_data.VALUE_NONE,
60*9e94795aSAndroid Build Coastguard Worker                    supplier='Organization: <organization>',
61*9e94795aSAndroid Build Coastguard Worker                    files_analyzed=True,
62*9e94795aSAndroid Build Coastguard Worker                    verification_code='<package_verification_code>')
63*9e94795aSAndroid Build Coastguard Worker  package.file_ids.append(file_id)
64*9e94795aSAndroid Build Coastguard Worker
65*9e94795aSAndroid Build Coastguard Worker  doc.packages.append(package)
66*9e94795aSAndroid Build Coastguard Worker  doc.files.append(file)
67*9e94795aSAndroid Build Coastguard Worker  doc.describes = package_id
68*9e94795aSAndroid Build Coastguard Worker
69*9e94795aSAndroid Build Coastguard Worker  with open(args.layoutlib_sbom, 'r', encoding='utf-8') as f:
70*9e94795aSAndroid Build Coastguard Worker    layoutlib_sbom = json.load(f)
71*9e94795aSAndroid Build Coastguard Worker
72*9e94795aSAndroid Build Coastguard Worker  with open(args.layoutlib_sbom, 'rb') as f:
73*9e94795aSAndroid Build Coastguard Worker    sha1 = hashlib.file_digest(f, 'sha1')
74*9e94795aSAndroid Build Coastguard Worker
75*9e94795aSAndroid Build Coastguard Worker  layoutlib_sbom_namespace = layoutlib_sbom[sbom_writers.PropNames.DOCUMENT_NAMESPACE]
76*9e94795aSAndroid Build Coastguard Worker  external_doc_ref = 'DocumentRef-layoutlib'
77*9e94795aSAndroid Build Coastguard Worker  doc.external_refs = [
78*9e94795aSAndroid Build Coastguard Worker    sbom_data.DocumentExternalReference(external_doc_ref, layoutlib_sbom_namespace,
79*9e94795aSAndroid Build Coastguard Worker                                        f'SHA1: {sha1.hexdigest()}')]
80*9e94795aSAndroid Build Coastguard Worker
81*9e94795aSAndroid Build Coastguard Worker  resource_file_spdxids = []
82*9e94795aSAndroid Build Coastguard Worker  for file in layoutlib_sbom[sbom_writers.PropNames.FILES]:
83*9e94795aSAndroid Build Coastguard Worker    file_path = file[sbom_writers.PropNames.FILE_NAME]
84*9e94795aSAndroid Build Coastguard Worker    if file_path.startswith('data/res/') or file_path.startswith('data/overlays/'):
85*9e94795aSAndroid Build Coastguard Worker      resource_file_spdxids.append(file[sbom_writers.PropNames.SPDXID])
86*9e94795aSAndroid Build Coastguard Worker
87*9e94795aSAndroid Build Coastguard Worker  doc.relationships = [
88*9e94795aSAndroid Build Coastguard Worker    sbom_data.Relationship(package_id, sbom_data.RelationshipType.CONTAINS, file_id)
89*9e94795aSAndroid Build Coastguard Worker  ]
90*9e94795aSAndroid Build Coastguard Worker  for spdxid in resource_file_spdxids:
91*9e94795aSAndroid Build Coastguard Worker    doc.relationships.append(
92*9e94795aSAndroid Build Coastguard Worker      sbom_data.Relationship(file_id, sbom_data.RelationshipType.GENERATED_FROM,
93*9e94795aSAndroid Build Coastguard Worker                             f'{external_doc_ref}:{spdxid}'))
94*9e94795aSAndroid Build Coastguard Worker
95*9e94795aSAndroid Build Coastguard Worker  # write sbom file
96*9e94795aSAndroid Build Coastguard Worker  with open(args.output_file, 'w', encoding='utf-8') as f:
97*9e94795aSAndroid Build Coastguard Worker    sbom_writers.JSONWriter.write(doc, f)
98*9e94795aSAndroid Build Coastguard Worker
99*9e94795aSAndroid Build Coastguard Worker
100*9e94795aSAndroid Build Coastguard Workerif __name__ == '__main__':
101*9e94795aSAndroid Build Coastguard Worker  main()
102