Skip to content

Commit 662d5f1

Browse files
committed
Read SBOM inside embed()
1 parent 33a66fd commit 662d5f1

1 file changed

Lines changed: 12 additions & 9 deletions

File tree

.github/embed-sbom.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def record_entry(path: str, data: bytes) -> str:
2222
return f"{path},sha256={digest.rstrip(b'=').decode()},{len(data)}"
2323

2424

25-
def embed(wheel: Path, sbom_name: str, sbom_bytes: bytes) -> None:
25+
def embed(wheel: Path, sbom: Path) -> None:
2626
with zipfile.ZipFile(wheel) as zf:
2727
infos = zf.infolist()
2828
contents = {info.filename: zf.read(info.filename) for info in infos}
@@ -33,7 +33,9 @@ def embed(wheel: Path, sbom_name: str, sbom_bytes: bytes) -> None:
3333
if name.endswith(".dist-info/RECORD") and name.count("/") == 1
3434
)
3535
dist_info = record_name.rsplit("/", 1)[0]
36-
sbom_path = f"{dist_info}/sboms/{sbom_name}"
36+
37+
sbom_bytes = sbom.read_bytes()
38+
sbom_path = f"{dist_info}/sboms/{sbom.name}"
3739

3840
# Append a matching RECORD line for the SBOM (RECORD's own line has no hash).
3941
lines = contents[record_name].decode("utf-8").splitlines()
@@ -49,16 +51,18 @@ def embed(wheel: Path, sbom_name: str, sbom_bytes: bytes) -> None:
4951
zf.writestr(sbom_path, sbom_bytes)
5052
tmp.replace(wheel)
5153

54+
print(f"Embedded {sbom.name} in {wheel.name}")
55+
5256

53-
def load_sbom(path: Path) -> tuple[str, bytes]:
54-
"""Read the SBOM; `path` may be the file or a directory containing one."""
57+
def scan_dir(path: Path) -> Path:
58+
"""If `path` is a directory, return the path of the SBOM within."""
5559
if path.is_dir():
5660
candidates = list(path.glob("*.cdx.json"))
5761
if len(candidates) != 1:
5862
msg = f"expected exactly one *.cdx.json in {path}, found {len(candidates)}"
5963
raise SystemExit(msg)
60-
path = candidates[0]
61-
return path.name, path.read_bytes()
64+
return candidates[0]
65+
return path
6266

6367

6468
def main() -> None:
@@ -75,16 +79,15 @@ def main() -> None:
7579
)
7680
args = parser.parse_args()
7781

78-
sbom_name, sbom_bytes = load_sbom(args.sbom)
82+
sbom = scan_dir(args.sbom)
7983

8084
wheels = sorted(args.wheelhouse.glob("*.whl"))
8185
if not wheels:
8286
print(f"error: no wheels found in {args.wheelhouse}", file=sys.stderr)
8387
raise SystemExit(1)
8488

8589
for wheel in wheels:
86-
embed(wheel, sbom_name, sbom_bytes)
87-
print(f"Embedded {sbom_name} in {wheel.name}")
90+
embed(wheel, sbom)
8891

8992

9093
if __name__ == "__main__":

0 commit comments

Comments
 (0)