sbom.py¶
clew/wiki/sbom.py
The SBOM page: the repository's own SBOM rendered, beside its files' licences (#70, P5.8).
read_sbom
tuple[tuple[str, str], list[Component]] clew.wiki.sbom.read_sbom(Path path)
Read one SBOM file, SPDX 2.3 or CycloneDX JSON, by its content.
Source · 16 lines
def read_sbom(path: Path) -> tuple[tuple[str, str], list[Component]]:
"""FORMAT BY CONTENT, not by file name: `spdxVersion` is SPDX, `bomFormat: CycloneDX` is
CycloneDX; anything else raises ValueError naming the file, so a wrong file is a build fault
the page reports, never a silently empty table.
@brief Read an SBOM.
@return Package and components.
"""
doc = json.loads(Path(path).read_text(encoding="utf-8"))
if str(doc.get("spdxVersion", "")).startswith("SPDX-"):
return _spdx(doc)
if doc.get("bomFormat") == "CycloneDX":
return _cyclonedx(doc)
raise ValueError(f"{path} is neither SPDX JSON nor CycloneDX JSON")
Activity · 6 steps
Requirements
sbom_html
str clew.wiki.sbom.sbom_html(list sboms, tuple[str,...] accepted, dict file_licences, int unlicensed)
The SBOM page's HTML.
Source · 25 lines
def sbom_html(sboms: list, accepted: tuple[str, ...], file_licences: dict, unlicensed: int) -> str:
"""@brief SBOMs, then the files' licences. @return HTML."""
parts = [_policy(accepted)]
if not sboms:
parts.append(
'<p class="none">No SBOM was found: none is declared under <code>wiki.sbom.files</code> '
"and none is at the repository root (<code>sbom.spdx.json</code>, "
"<code>bom.cdx.json</code>). The wiki renders an SBOM; it never makes one.</p>"
)
for rel, (name, licence), comps in sboms:
## E-25: a column empty in every row is not drawn.
cols = [c for c in _COLUMNS if any(getattr(comp, c) for comp in comps)]
rows = "".join(_row(c, accepted, cols) for c in comps)
heads = "".join(f"<th>{_HEADS.get(c, c)}</th>" for c in cols)
parts.append(
f'<section class="sbom"><h2><code>{escape(rel)}</code></h2>'
f"<p>Package <strong>{escape(name)}</strong>, licence <code>{escape(licence)}</code>; "
f"{len(comps)} components.</p>"
'<div class="table-wrap"><table class="clew sbom"><thead><tr><th>component</th>'
f"{heads}</tr></thead><tbody>{rows}</tbody></table></div></section>"
)
parts.append(_files(file_licences, unlicensed))
return "".join(parts)
Activity · 10 steps
Requirements
sbom_page
str clew.wiki.sbom.sbom_page(Path repo_root, Path db, config, Path source)
Write the SBOM page from the repository's SBOMs and the index's file licences.
Source · 35 lines
def sbom_page(repo_root: Path, db: Path, config, source: Path) -> str: # noqa: ANN001
"""AN SBOM THAT WILL NOT READ IS SAID, by file and reason, not dropped: the page lists it under
the table rather than failing the whole site.
@brief Write sbom.md.
@return Page name.
"""
root = Path(repo_root)
sboms, refused = [], []
for path in sorted({p for g in config.sbom_files for p in root.glob(g) if p.is_file()}):
rel = path.relative_to(root).as_posix()
try:
sboms.append((rel, *read_sbom(path)))
except (OSError, ValueError) as exc:
refused.append(f"<li><code>{escape(rel)}</code>: {escape(str(exc))}</li>")
accepted = config.sbom_accepted or _toml_accepted(root / "docs" / "sbom.toml")
counts, unlicensed = _file_licences(db)
html = sbom_html(sboms, accepted, counts, unlicensed)
if refused:
html += f'<p class="bad">Not read:</p><ul>{"".join(refused)}</ul>'
lines = [
"# SBOM",
"",
"The software bill of materials this repository carries, rendered as it is (the wiki never "
"writes one), and the licence each of its own files declares.",
"",
"```{raw} html",
html,
"```",
"",
]
(Path(source) / "sbom.md").write_text("\n".join(lines), encoding="utf-8")
return "sbom"