Wei Li | c134b76 | 2023-10-17 23:52:30 -0700 | [diff] [blame] | 1 | #!/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 | |
| 17 | import argparse |
| 18 | import hashlib |
| 19 | import json |
| 20 | import sbom_data |
| 21 | import sbom_writers |
| 22 | |
Wei Li | ee4ab53 | 2023-10-25 15:49:26 -0700 | [diff] [blame^] | 23 | ''' |
| 24 | This script generates SBOM of framework_res.jar of layoutlib shipped with Android Studio. |
| 25 | |
| 26 | The generated SBOM contains some placeholders which should be substituted by release_layoutlib.sh. |
| 27 | The placeholders include: document name, document namespace, organization, created timestamp and |
| 28 | the SHA1 checksum of framework_res.jar. |
| 29 | ''' |
Wei Li | c134b76 | 2023-10-17 23:52:30 -0700 | [diff] [blame] | 30 | |
| 31 | def get_args(): |
| 32 | parser = argparse.ArgumentParser() |
| 33 | parser.add_argument('-v', '--verbose', action='store_true', default=False, |
| 34 | help='Print more information.') |
| 35 | parser.add_argument('--output_file', required=True, |
| 36 | help='The generated SBOM file in SPDX format.') |
| 37 | parser.add_argument('--layoutlib_sbom', required=True, |
| 38 | help='The file path of the SBOM of layoutlib.') |
| 39 | |
| 40 | return parser.parse_args() |
| 41 | |
| 42 | |
| 43 | def main(): |
| 44 | global args |
| 45 | args = get_args() |
| 46 | |
| 47 | doc = sbom_data.Document(name='<name>', |
| 48 | namespace='<namespace>', |
| 49 | creators=['Organization: <organization>'], |
| 50 | created='<created>') |
| 51 | |
| 52 | filename = 'data/framework_res.jar' |
| 53 | file_id = f'SPDXRef-{sbom_data.encode_for_spdxid(filename)}' |
| 54 | file = sbom_data.File(id=file_id, name=filename, checksum='SHA1: <checksum>') |
| 55 | doc.files.append(file) |
| 56 | doc.describes = file_id |
| 57 | |
| 58 | with open(args.layoutlib_sbom, 'r', encoding='utf-8') as f: |
| 59 | layoutlib_sbom = json.load(f) |
| 60 | |
| 61 | with open(args.layoutlib_sbom, 'rb') as f: |
| 62 | sha1 = hashlib.file_digest(f, 'sha1') |
| 63 | |
| 64 | layoutlib_sbom_namespace = layoutlib_sbom[sbom_writers.PropNames.DOCUMENT_NAMESPACE] |
| 65 | external_doc_ref = 'DocumentRef-layoutlib' |
| 66 | doc.external_refs = [ |
| 67 | sbom_data.DocumentExternalReference(external_doc_ref, layoutlib_sbom_namespace, |
| 68 | f'SHA1: {sha1.hexdigest()}')] |
| 69 | |
| 70 | resource_file_spdxids = [] |
| 71 | for file in layoutlib_sbom[sbom_writers.PropNames.FILES]: |
| 72 | if file[sbom_writers.PropNames.FILE_NAME].startswith('data/res/'): |
| 73 | resource_file_spdxids.append(file[sbom_writers.PropNames.SPDXID]) |
| 74 | |
| 75 | doc.relationships = [] |
| 76 | for spdxid in resource_file_spdxids: |
| 77 | doc.relationships.append( |
| 78 | sbom_data.Relationship(file_id, sbom_data.RelationshipType.GENERATED_FROM, |
| 79 | f'{external_doc_ref}:{spdxid}')) |
| 80 | |
| 81 | # write sbom file |
| 82 | with open(args.output_file, 'w', encoding='utf-8') as f: |
| 83 | sbom_writers.JSONWriter.write(doc, f) |
| 84 | |
| 85 | |
| 86 | if __name__ == '__main__': |
| 87 | main() |