-
Notifications
You must be signed in to change notification settings - Fork 218
/
generate-manifest.py
241 lines (208 loc) · 7.37 KB
/
generate-manifest.py
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# SPDX-License-Identifier: Apache-2.0
import glob
import hashlib
import json
import os
import re
import subprocess
from fontTools.ttLib import TTFont
from lib4sbom.data.file import SBOMFile
from lib4sbom.data.package import SBOMPackage
from lib4sbom.generator import SBOMGenerator
from lib4sbom.sbom import SBOM
from lib4sbom.output import SBOMOutput
from sbom4python.scanner import SBOMScanner
__version__ = "0.0.1"
SOURCESANS_RELEASE_URL = (
"https://github.com/adobe-fonts/source-han-sans/releases/tag/2.004R"
)
SOURCESERIF_RELEASE_URL = (
"https://github.com/adobe-fonts/source-han-serif/releases/tag/2.003"
)
DRAFT = False
# Most of this is written in as general a way as possible, so it can be reused
# as a basis for other font projects. The noto-cjk specific stuff is at the end.
def file_id(s):
return re.sub(r"[^a-zA-Z0-9\.]", "-", s)
def add_checksums(file):
filename = file.get_name()
file.set_checksum(
"SHA1", hashlib.file_digest(open(filename, "rb"), "sha1").hexdigest()
)
file.set_checksum(
"SHA256", hashlib.file_digest(open(filename, "rb"), "sha256").hexdigest()
)
def font_to_file(filename, upstream_binary=False):
file = SBOMFile()
file.initialise()
file.set_id(file_id(filename))
file.set_name(filename)
file.set_filetype("OTHER")
add_checksums(file)
name = TTFont(filename, fontNumber=0)["name"]
license = name.getDebugName(13)
copyright = name.getDebugName(0)
rfn = (
"-RFN"
if (
"reserved font name" in license.lower()
or "reserved font name" in copyright.lower()
)
else ""
)
if "Open Font License, Version 1.0" in license:
file.set_licenseinfoinfile("OFL-1.0" + rfn)
file.set_licenseconcluded("OFL-1.0" + rfn)
if "Open Font License, Version 1.1" in license:
file.set_licenseinfoinfile("OFL-1.1" + rfn)
file.set_licenseconcluded("OFL-1.1" + rfn)
file.set_copyrighttext(copyright)
if upstream_binary:
manufacturer = name.getDebugName(8)
file.set_contributor(manufacturer)
designer = name.getDebugName(9)
file.set_contributor(designer)
file.set_comment("Binary file contributed by " + manufacturer)
return file.get_file()
def homebrew_package(package_name):
package = SBOMPackage()
package.initialise()
package.set_name(package_name)
info = subprocess.run(
f"brew info --json=v1 {package_name}", shell=True, capture_output=True
).stdout
info = json.loads(info)[0]
package.set_version(info["linked_keg"])
package.set_homepage(info["homepage"])
package.set_licenseinfoinfiles(info["license"])
# Extend this to handle more licenses if needed
if "MIT" in info["license"]:
package.set_licenseconcluded("MIT")
return package.get_package()
def source_to_file(filename):
file = SBOMFile()
file.initialise()
file.set_id(file_id(filename))
file.set_name(filename)
file.set_filetype("SOURCE")
add_checksums(file)
file.set_licenseinfoinfile("Apache-2.0")
file.set_licenseconcluded("Apache-2.0")
file.set_comment(
"Taken from "
+ subprocess.run(
"git config --get remote.origin.url", shell=True, capture_output=True
).stdout.decode("utf8")
+ " git rev "
+ subprocess.run(
"git rev-parse main:" + filename, shell=True, capture_output=True
)
.stdout.decode("utf8")
.strip()
)
return file.get_file()
sbom_scan = SBOMScanner(False, False, False, False)
sbom_gen = SBOMGenerator(
sbom_type="spdx", format="json", application=__file__, version=__version__
)
sbom_gen.bom.spdx_version = "SPDX-2.2" # All ms sbom-tool can cope with
sbom = SBOM()
document = sbom_scan.get_document()
sbom.add_document(document)
files = {}
# Input files from Adobe
adobe = {
font: font_to_file(font, upstream_binary=True)
for font in glob.glob("S*/**/*.[o,t]t[f,c]", recursive=True)
}
files.update(adobe)
# Files we generated
ours = {
font: font_to_file(font)
for font in glob.glob("google-fonts/*.[o,t]t[f,c]", recursive=True)
}
for our in ours.values():
our["comment"] = "Generated as described in relationships section"
files.update(ours)
sbom.add_files(files)
# Let's consider Source Han Serif and Source Han Sans as packages, and
# the Adobe dropped files as FILE_MODIFIED from the package
source_sans = SBOMPackage()
source_sans.initialise()
source_sans.set_name("Source Han Sans")
source_sans.set_type("ARCHIVE")
source_sans.set_supplier("organization", "Adobe")
source_sans.set_downloadlocation(SOURCESANS_RELEASE_URL)
source_sans.set_homepage("https://github.com/adobe-fonts/source-han-sans")
source_sans.set_licenseconcluded("OFL-1.1-RFN")
sans_version = str(
TTFont("Sans/OTF/Japanese/NotoSansCJKjp-Regular.otf")["head"].fontRevision
)
source_sans.set_version(sans_version)
source_serif = SBOMPackage()
source_serif.initialise()
source_serif.set_type("ARCHIVE")
source_serif.set_name("Source Han Serif")
source_serif.set_supplier("organization", "Adobe")
source_serif.set_downloadlocation(SOURCESERIF_RELEASE_URL)
source_serif.set_homepage("https://github.com/adobe-fonts/source-han-serif")
source_serif.set_licenseconcluded("OFL-1.1-RFN")
serif_version = str(
TTFont("Serif/OTF/Japanese/NotoSerifCJKjp-Regular.otf")["head"].fontRevision
)
source_serif.set_version(serif_version)
sbom.add_packages(
{
"Source Han Sans": source_sans.get_package(),
"Source Han Serif": source_serif.get_package(),
}
)
sbom_gen.generate(
project_name="noto-cjk" + ("-DRAFT" if DRAFT else ""),
sbom_data=sbom.get_sbom(),
send_to_output=False,
)
# lib4sbom does not support file->file relationships, so we have to
# add them manually
sbom_gen.sbom_complete = False
def relates(sbom, source, target, relationship):
if os.path.isfile(source):
source_ident = sbom.bom.file_ident(sbom._get_element(source, file_id(source)))
elif source == "-": # This document
source_ident = sbom.bom.SPDX_PROJECT_ID
else:
source_ident = sbom.bom.package_ident(
sbom._get_element(source, file_id(source))
)
if os.path.isfile(target):
target_ident = sbom.bom.file_ident(sbom._get_element(target, file_id(target)))
else:
target_ident = sbom.bom.package_ident(
sbom._get_element(target, file_id(target))
)
sbom_gen.bom.generateRelationship(
source_ident,
target_ident,
" " + relationship + " ",
)
for original in adobe.keys():
relates(sbom_gen, "-", original, "DESCRIBES")
if "Sans" in original:
relates(sbom_gen, "Source Han Sans", original, "FILE_MODIFIED")
else:
relates(sbom_gen, "Source Han Serif", original, "FILE_MODIFIED")
for original in glob.glob("S*/Variable/*/Subset/*.ttf"):
modified = os.path.basename(original).replace("-VF.ttf", "[wght].ttf")
modified = "google-fonts/" + re.sub(
"CJK(..)", lambda x: x[0][-2:].upper(), modified
)
relates(sbom_gen, modified, original, "FILE_MODIFIED")
relates(sbom_gen, "-", modified, "DESCRIBES")
sbom_gen.bom.showRelationship()
manifest_dir = os.path.join(
"_manifest", sbom_gen.bom.spdx_version.lower().replace("-", "_")
)
os.makedirs(manifest_dir, exist_ok=True)
sbom_path = os.path.join(manifest_dir, "manifest.spdx.json")
sbom_out = SBOMOutput(sbom_path, output_format="json")
sbom_out.generate_output(sbom_gen.sbom)