Read a CycloneDX JSON SBOM and write a CSV beside it.
(json_path: Path)
| 32 | |
| 33 | |
| 34 | def sbom_to_csv(json_path: Path) -> Path: |
| 35 | """Read a CycloneDX JSON SBOM and write a CSV beside it.""" |
| 36 | with json_path.open() as f: |
| 37 | sbom = json.load(f) |
| 38 | |
| 39 | csv_path = json_path.with_suffix(".csv") |
| 40 | components = sbom.get("components", []) |
| 41 | |
| 42 | with csv_path.open("w", newline="") as f: |
| 43 | writer = csv.writer(f) |
| 44 | writer.writerow(["name", "version", "type", "purl", "licenses", "bom-ref"]) |
| 45 | |
| 46 | for comp in components: |
| 47 | writer.writerow( |
| 48 | [ |
| 49 | comp.get("name", ""), |
| 50 | comp.get("version", ""), |
| 51 | comp.get("type", ""), |
| 52 | comp.get("purl", ""), |
| 53 | extract_licenses(comp), |
| 54 | comp.get("bom-ref", ""), |
| 55 | ] |
| 56 | ) |
| 57 | |
| 58 | return csv_path |
| 59 | |
| 60 | |
| 61 | def _find_sbom_files() -> list[Path]: |
no test coverage detected