-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsbom_json_to_csv.py
More file actions
78 lines (65 loc) · 2.15 KB
/
sbom_json_to_csv.py
File metadata and controls
78 lines (65 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import json
import csv
import sys
# from pathlib import Path
from tabulate import tabulate
input_file = sys.argv[1] if len(sys.argv) > 1 else "sbom.json"
output_file = sys.argv[2] if len(sys.argv) > 2 else "sbom.csv"
with open(input_file, "r", encoding="utf-8") as f:
sbom = json.load(f)
packages = sbom.get("packages", [])
columns = [
"name",
"versionInfo",
"type",
"supplier",
"downloadLocation",
"licenseConcluded",
"licenseDeclared",
"externalRefs"
]
def get_type(pkg):
spdxid = pkg.get("SPDXID", "")
if "-" in spdxid:
parts = spdxid.split("-")
if len(parts) > 2:
return parts[2]
refs = pkg.get("externalRefs", [])
for ref in refs:
if ref.get("referenceType") == "purl":
return ref.get("referenceLocator", "").split("/")[0]
return ""
def get_external_refs(pkg):
refs = pkg.get("externalRefs", [])
return ";".join([ref.get("referenceLocator", "") for ref in refs])
with open(output_file, "w", newline="", encoding="utf-8") as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=columns)
writer.writeheader()
for pkg in packages:
row = {
"name": pkg.get("name", ""),
"versionInfo": pkg.get("versionInfo", ""),
"type": get_type(pkg),
"supplier": pkg.get("supplier", ""),
"downloadLocation": pkg.get("downloadLocation", ""),
"licenseConcluded": pkg.get("licenseConcluded", ""),
"licenseDeclared": pkg.get("licenseDeclared", ""),
"externalRefs": get_external_refs(pkg)
}
writer.writerow(row)
print(f"CSV export complete: {output_file}")
with open("sbom_table.txt", "w", encoding="utf-8") as f:
table = []
for pkg in packages:
row = [
pkg.get("name", ""),
pkg.get("versionInfo", ""),
get_type(pkg),
pkg.get("supplier", ""),
pkg.get("downloadLocation", ""),
pkg.get("licenseConcluded", ""),
pkg.get("licenseDeclared", ""),
get_external_refs(pkg)
]
table.append(row)
f.write(tabulate(table, columns, tablefmt="grid"))