8000 Add news, refactor our SPDX ID normalizer · python/cpython@3aaa38b · GitHub
[go: up one dir, main page]

Skip to content

Commit 3aaa38b

Browse files
committed
Add news, refactor our SPDX ID normalizer
1 parent c76ab24 commit 3aaa38b

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Created a Software Bill-of-Materials document and tooling for tracking
2+
dependencies.

Tools/build/generate_sbom.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,23 @@ class PackageFiles(typing.NamedTuple):
7373
}
7474

7575

76+
def spdx_id(value: str) -> str:
77+
"""Encode a value into characters that are valid in an SPDX ID"""
78+
return re.sub(r"[^a-zA-Z0-9.\-]+", "-", value)
79+
80+
7681
def main():
7782
root_dir = pathlib.Path(__file__).parent.parent.parent
78-
sbom_path = (root_dir / "Misc/sbom.spdx.json")
83+
sbom_path = root_dir / "Misc/sbom.spdx.json"
7984
sbom_data = json.loads(sbom_path.read_bytes())
8085

81-
# Make a bunch of assertions about the SBOM data to ensure its consistent.
86+
# Make a bunch of assertions about the SBOM data to ensure it's consistent.
8287
assert {package["name"] for package in sbom_data["packages"]} == set(PACKAGE_TO_FILES)
8388
for package in sbom_data["packages"]:
8489

8590
# Properties and ID must be properly formed.
8691
assert set(package.keys()) == REQUIRED_PROPERTIES_PACKAGE
87-
assert package["SPDXID"] == f"SPDXRef-PACKAGE-{package['name']}"
92+
assert package["SPDXID"] == spdx_id(f"SPDXRef-PACKAGE-{package['name']}")
8893

8994
# Version must be in the download and external references.
9095
version = package["versionInfo"]
@@ -100,7 +105,7 @@ def main():
100105

101106
# We call 'sorted()' here a lot to avoid filesystem scan order issues.
102107
for name, files in sorted(PACKAGE_TO_FILES.items()):
103-
package_spdx_id = f"SPDXRef-PACKAGE-{name}"
108+
package_spdx_id = spdx_id(f"SPDXRef-PACKAGE-{name}")
104109
exclude = files.exclude or ()
105110
for include in sorted(files.include):
106111
paths = sorted(glob.glob(include, root_dir=root_dir, recursive=True))
@@ -116,7 +121,7 @@ def main():
116121
checksum_sha1 = hashlib.sha1(data).hexdigest()
117122
checksum_sha256 = hashlib.sha256(data).hexdigest()
118123

119-
file_spdx_id = re.sub(r"[^a-zA-Z0-9.\-]+", "-", f"SPDXRef-FILE-{path}")
124+
file_spdx_id = spdx_id(f"SPDXRef-FILE-{path}")
120125
sbom_files.append({
121126
"SPDXID": file_spdx_id,
122127
"fileName": path,

0 commit comments

Comments
 (0)
0