Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 1 | # |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 2 | # Copyright OpenEmbedded Contributors |
| 3 | # |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 4 | # SPDX-License-Identifier: GPL-2.0-only |
| 5 | # |
| 6 | |
| 7 | import collections |
| 8 | |
| 9 | DepRecipe = collections.namedtuple("DepRecipe", ("doc", "doc_sha1", "recipe")) |
| 10 | DepSource = collections.namedtuple("DepSource", ("doc", "doc_sha1", "recipe", "file")) |
| 11 | |
| 12 | |
| 13 | def get_recipe_spdxid(d): |
| 14 | return "SPDXRef-%s-%s" % ("Recipe", d.getVar("PN")) |
| 15 | |
| 16 | |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 17 | def get_download_spdxid(d, idx): |
| 18 | return "SPDXRef-Download-%s-%d" % (d.getVar("PN"), idx) |
| 19 | |
| 20 | |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 21 | def get_package_spdxid(pkg): |
| 22 | return "SPDXRef-Package-%s" % pkg |
| 23 | |
| 24 | |
| 25 | def get_source_file_spdxid(d, idx): |
| 26 | return "SPDXRef-SourceFile-%s-%d" % (d.getVar("PN"), idx) |
| 27 | |
| 28 | |
| 29 | def get_packaged_file_spdxid(pkg, idx): |
| 30 | return "SPDXRef-PackagedFile-%s-%d" % (pkg, idx) |
| 31 | |
| 32 | |
| 33 | def get_image_spdxid(img): |
| 34 | return "SPDXRef-Image-%s" % img |
| 35 | |
| 36 | |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 37 | def get_sdk_spdxid(sdk): |
| 38 | return "SPDXRef-SDK-%s" % sdk |
| 39 | |
| 40 | |
Patrick Williams | b542dec | 2023-06-09 01:26:37 -0500 | [diff] [blame] | 41 | def doc_path_by_namespace(spdx_deploy, doc_namespace): |
| 42 | return spdx_deploy / "by-namespace" / doc_namespace.replace("/", "_") |
| 43 | |
| 44 | |
| 45 | def doc_path_by_hashfn(spdx_deploy, doc_name, hashfn): |
| 46 | return spdx_deploy / "by-hash" / hashfn.split()[1] / (doc_name + ".spdx.json") |
| 47 | |
| 48 | |
| 49 | def doc_path(spdx_deploy, doc_name, arch, subdir): |
| 50 | return spdx_deploy / arch/ subdir / (doc_name + ".spdx.json") |
| 51 | |
| 52 | |
| 53 | def write_doc(d, spdx_doc, arch, subdir, spdx_deploy=None, indent=None): |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 54 | from pathlib import Path |
| 55 | |
| 56 | if spdx_deploy is None: |
| 57 | spdx_deploy = Path(d.getVar("SPDXDEPLOY")) |
| 58 | |
Patrick Williams | b542dec | 2023-06-09 01:26:37 -0500 | [diff] [blame] | 59 | dest = doc_path(spdx_deploy, spdx_doc.name, arch, subdir) |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 60 | dest.parent.mkdir(exist_ok=True, parents=True) |
| 61 | with dest.open("wb") as f: |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 62 | doc_sha1 = spdx_doc.to_json(f, sort_keys=True, indent=indent) |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 63 | |
Patrick Williams | b542dec | 2023-06-09 01:26:37 -0500 | [diff] [blame] | 64 | l = doc_path_by_namespace(spdx_deploy, spdx_doc.documentNamespace) |
| 65 | l.parent.mkdir(exist_ok=True, parents=True) |
| 66 | l.symlink_to(os.path.relpath(dest, l.parent)) |
| 67 | |
| 68 | l = doc_path_by_hashfn(spdx_deploy, spdx_doc.name, d.getVar("BB_HASHFILENAME")) |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 69 | l.parent.mkdir(exist_ok=True, parents=True) |
| 70 | l.symlink_to(os.path.relpath(dest, l.parent)) |
| 71 | |
| 72 | return doc_sha1 |
| 73 | |
| 74 | |
| 75 | def read_doc(fn): |
| 76 | import hashlib |
| 77 | import oe.spdx |
| 78 | import io |
| 79 | import contextlib |
| 80 | |
| 81 | @contextlib.contextmanager |
| 82 | def get_file(): |
| 83 | if isinstance(fn, io.IOBase): |
| 84 | yield fn |
| 85 | else: |
| 86 | with fn.open("rb") as f: |
| 87 | yield f |
| 88 | |
| 89 | with get_file() as f: |
| 90 | sha1 = hashlib.sha1() |
| 91 | while True: |
| 92 | chunk = f.read(4096) |
| 93 | if not chunk: |
| 94 | break |
| 95 | sha1.update(chunk) |
| 96 | |
| 97 | f.seek(0) |
| 98 | doc = oe.spdx.SPDXDocument.from_json(f) |
| 99 | |
| 100 | return (doc, sha1.hexdigest()) |