blob: 0c9276584c2cfef77b4704e51d2c094c053b7ef7 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001# This class integrates real-time license scanning, generation of SPDX standard
2# output and verifiying license info during the building process.
3# It is a combination of efforts from the OE-Core, SPDX and Fossology projects.
4#
5# For more information on FOSSology:
6# http://www.fossology.org
7#
8# For more information on FOSSologySPDX commandline:
9# https://github.com/spdx-tools/fossology-spdx/wiki/Fossology-SPDX-Web-API
10#
11# For more information on SPDX:
12# http://www.spdx.org
13#
14
15# SPDX file will be output to the path which is defined as[SPDX_MANIFEST_DIR]
16# in ./meta/conf/licenses.conf.
17
18SPDXSSTATEDIR = "${WORKDIR}/spdx_sstate_dir"
19
20# If ${S} isn't actually the top-level source directory, set SPDX_S to point at
21# the real top-level directory.
22SPDX_S ?= "${S}"
23
24python do_spdx () {
25 import os, sys
26 import json, shutil
27
28 info = {}
29 info['workdir'] = d.getVar('WORKDIR', True)
30 info['sourcedir'] = d.getVar('SPDX_S', True)
31 info['pn'] = d.getVar('PN', True)
32 info['pv'] = d.getVar('PV', True)
33 info['spdx_version'] = d.getVar('SPDX_VERSION', True)
34 info['data_license'] = d.getVar('DATA_LICENSE', True)
35
36 sstatedir = d.getVar('SPDXSSTATEDIR', True)
37 sstatefile = os.path.join(sstatedir, info['pn'] + info['pv'] + ".spdx")
38
39 manifest_dir = d.getVar('SPDX_MANIFEST_DIR', True)
40 info['outfile'] = os.path.join(manifest_dir, info['pn'] + ".spdx" )
41
42 info['spdx_temp_dir'] = d.getVar('SPDX_TEMP_DIR', True)
43 info['tar_file'] = os.path.join(info['workdir'], info['pn'] + ".tar.gz" )
44
45 # Make sure important dirs exist
46 try:
47 bb.utils.mkdirhier(manifest_dir)
48 bb.utils.mkdirhier(sstatedir)
49 bb.utils.mkdirhier(info['spdx_temp_dir'])
50 except OSError as e:
51 bb.error("SPDX: Could not set up required directories: " + str(e))
52 return
53
54 ## get everything from cache. use it to decide if
55 ## something needs to be rerun
56 cur_ver_code = get_ver_code(info['sourcedir'])
57 cache_cur = False
58 if os.path.exists(sstatefile):
59 ## cache for this package exists. read it in
60 cached_spdx = get_cached_spdx(sstatefile)
61
62 if cached_spdx['PackageVerificationCode'] == cur_ver_code:
63 bb.warn("SPDX: Verification code for " + info['pn']
64 + "is same as cache's. do nothing")
65 cache_cur = True
66 else:
67 local_file_info = setup_foss_scan(info, True, cached_spdx['Files'])
68 else:
69 local_file_info = setup_foss_scan(info, False, None)
70
71 if cache_cur:
72 spdx_file_info = cached_spdx['Files']
73 foss_package_info = cached_spdx['Package']
74 foss_license_info = cached_spdx['Licenses']
75 else:
76 ## setup fossology command
77 foss_server = d.getVar('FOSS_SERVER', True)
78 foss_flags = d.getVar('FOSS_WGET_FLAGS', True)
79 foss_full_spdx = d.getVar('FOSS_FULL_SPDX', True) == "true" or False
80 foss_command = "wget %s --post-file=%s %s"\
81 % (foss_flags, info['tar_file'], foss_server)
82
83 foss_result = run_fossology(foss_command, foss_full_spdx)
84 if foss_result is not None:
85 (foss_package_info, foss_file_info, foss_license_info) = foss_result
86 spdx_file_info = create_spdx_doc(local_file_info, foss_file_info)
87 ## write to cache
88 write_cached_spdx(sstatefile, cur_ver_code, foss_package_info,
89 spdx_file_info, foss_license_info)
90 else:
91 bb.error("SPDX: Could not communicate with FOSSology server. Command was: " + foss_command)
92 return
93
94 ## Get document and package level information
95 spdx_header_info = get_header_info(info, cur_ver_code, foss_package_info)
96
97 ## CREATE MANIFEST
98 create_manifest(info, spdx_header_info, spdx_file_info, foss_license_info)
99
100 ## clean up the temp stuff
101 shutil.rmtree(info['spdx_temp_dir'], ignore_errors=True)
102 if os.path.exists(info['tar_file']):
103 remove_file(info['tar_file'])
104}
105addtask spdx after do_patch before do_configure
106
107def create_manifest(info, header, files, licenses):
108 import codecs
109 with codecs.open(info['outfile'], mode='w', encoding='utf-8') as f:
110 # Write header
111 f.write(header + '\n')
112
113 # Write file data
114 for chksum, block in files.iteritems():
115 f.write("FileName: " + block['FileName'] + '\n')
116 for key, value in block.iteritems():
117 if not key == 'FileName':
118 f.write(key + ": " + value + '\n')
119 f.write('\n')
120
121 # Write license data
122 for id, block in licenses.iteritems():
123 f.write("LicenseID: " + id + '\n')
124 for key, value in block.iteritems():
125 f.write(key + ": " + value + '\n')
126 f.write('\n')
127
128def get_cached_spdx(sstatefile):
129 import json
130 import codecs
131 cached_spdx_info = {}
132 with codecs.open(sstatefile, mode='r', encoding='utf-8') as f:
133 try:
134 cached_spdx_info = json.load(f)
135 except ValueError as e:
136 cached_spdx_info = None
137 return cached_spdx_info
138
139def write_cached_spdx(sstatefile, ver_code, package_info, files, license_info):
140 import json
141 import codecs
142 spdx_doc = {}
143 spdx_doc['PackageVerificationCode'] = ver_code
144 spdx_doc['Files'] = {}
145 spdx_doc['Files'] = files
146 spdx_doc['Package'] = {}
147 spdx_doc['Package'] = package_info
148 spdx_doc['Licenses'] = {}
149 spdx_doc['Licenses'] = license_info
150 with codecs.open(sstatefile, mode='w', encoding='utf-8') as f:
151 f.write(json.dumps(spdx_doc))
152
153def setup_foss_scan(info, cache, cached_files):
154 import errno, shutil
155 import tarfile
156 file_info = {}
157 cache_dict = {}
158
159 for f_dir, f in list_files(info['sourcedir']):
160 full_path = os.path.join(f_dir, f)
161 abs_path = os.path.join(info['sourcedir'], full_path)
162 dest_dir = os.path.join(info['spdx_temp_dir'], f_dir)
163 dest_path = os.path.join(info['spdx_temp_dir'], full_path)
164
165 checksum = hash_file(abs_path)
166 if not checksum is None:
167 file_info[checksum] = {}
168 ## retain cache information if it exists
169 if cache and checksum in cached_files:
170 file_info[checksum] = cached_files[checksum]
171 ## have the file included in what's sent to the FOSSology server
172 else:
173 file_info[checksum]['FileName'] = full_path
174 try:
175 bb.utils.mkdirhier(dest_dir)
176 shutil.copyfile(abs_path, dest_path)
177 except OSError as e:
178 bb.warn("SPDX: mkdirhier failed: " + str(e))
179 except shutil.Error as e:
180 bb.warn("SPDX: copyfile failed: " + str(e))
181 except IOError as e:
182 bb.warn("SPDX: copyfile failed: " + str(e))
183 else:
184 bb.warn("SPDX: Could not get checksum for file: " + f)
185
186 with tarfile.open(info['tar_file'], "w:gz") as tar:
187 tar.add(info['spdx_temp_dir'], arcname=os.path.basename(info['spdx_temp_dir']))
188
189 return file_info
190
191def remove_file(file_name):
192 try:
193 os.remove(file_name)
194 except OSError as e:
195 pass
196
197def list_files(dir):
198 for root, subFolders, files in os.walk(dir):
199 for f in files:
200 rel_root = os.path.relpath(root, dir)
201 yield rel_root, f
202 return
203
204def hash_file(file_name):
205 try:
206 with open(file_name, 'rb') as f:
207 data_string = f.read()
208 sha1 = hash_string(data_string)
209 return sha1
210 except:
211 return None
212
213def hash_string(data):
214 import hashlib
215 sha1 = hashlib.sha1()
216 sha1.update(data)
217 return sha1.hexdigest()
218
219def run_fossology(foss_command, full_spdx):
220 import string, re
221 import subprocess
222
223 p = subprocess.Popen(foss_command.split(),
224 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
225 foss_output, foss_error = p.communicate()
226 if p.returncode != 0:
227 return None
228
229 foss_output = unicode(foss_output, "utf-8")
230 foss_output = string.replace(foss_output, '\r', '')
231
232 # Package info
233 package_info = {}
234 if full_spdx:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500235 # All mandatory, only one occurrence
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500236 package_info['PackageCopyrightText'] = re.findall('PackageCopyrightText: (.*?</text>)', foss_output, re.S)[0]
237 package_info['PackageLicenseDeclared'] = re.findall('PackageLicenseDeclared: (.*)', foss_output)[0]
238 package_info['PackageLicenseConcluded'] = re.findall('PackageLicenseConcluded: (.*)', foss_output)[0]
239 # These may be more than one
240 package_info['PackageLicenseInfoFromFiles'] = re.findall('PackageLicenseInfoFromFiles: (.*)', foss_output)
241 else:
242 DEFAULT = "NOASSERTION"
243 package_info['PackageCopyrightText'] = "<text>" + DEFAULT + "</text>"
244 package_info['PackageLicenseDeclared'] = DEFAULT
245 package_info['PackageLicenseConcluded'] = DEFAULT
246 package_info['PackageLicenseInfoFromFiles'] = []
247
248 # File info
249 file_info = {}
250 records = []
251 # FileName is also in PackageFileName, so we match on FileType as well.
252 records = re.findall('FileName:.*?FileType:.*?</text>', foss_output, re.S)
253 for rec in records:
254 chksum = re.findall('FileChecksum: SHA1: (.*)\n', rec)[0]
255 file_info[chksum] = {}
256 file_info[chksum]['FileCopyrightText'] = re.findall('FileCopyrightText: '
257 + '(.*?</text>)', rec, re.S )[0]
258 fields = ['FileName', 'FileType', 'LicenseConcluded', 'LicenseInfoInFile']
259 for field in fields:
260 file_info[chksum][field] = re.findall(field + ': (.*)', rec)[0]
261
262 # Licenses
263 license_info = {}
264 licenses = []
265 licenses = re.findall('LicenseID:.*?LicenseName:.*?\n', foss_output, re.S)
266 for lic in licenses:
267 license_id = re.findall('LicenseID: (.*)\n', lic)[0]
268 license_info[license_id] = {}
269 license_info[license_id]['ExtractedText'] = re.findall('ExtractedText: (.*?</text>)', lic, re.S)[0]
270 license_info[license_id]['LicenseName'] = re.findall('LicenseName: (.*)', lic)[0]
271
272 return (package_info, file_info, license_info)
273
274def create_spdx_doc(file_info, scanned_files):
275 import json
276 ## push foss changes back into cache
277 for chksum, lic_info in scanned_files.iteritems():
278 if chksum in file_info:
279 file_info[chksum]['FileType'] = lic_info['FileType']
280 file_info[chksum]['FileChecksum: SHA1'] = chksum
281 file_info[chksum]['LicenseInfoInFile'] = lic_info['LicenseInfoInFile']
282 file_info[chksum]['LicenseConcluded'] = lic_info['LicenseConcluded']
283 file_info[chksum]['FileCopyrightText'] = lic_info['FileCopyrightText']
284 else:
285 bb.warn("SPDX: " + lic_info['FileName'] + " : " + chksum
286 + " : is not in the local file info: "
287 + json.dumps(lic_info, indent=1))
288 return file_info
289
290def get_ver_code(dirname):
291 chksums = []
292 for f_dir, f in list_files(dirname):
293 hash = hash_file(os.path.join(dirname, f_dir, f))
294 if not hash is None:
295 chksums.append(hash)
296 else:
297 bb.warn("SPDX: Could not hash file: " + path)
298 ver_code_string = ''.join(chksums).lower()
299 ver_code = hash_string(ver_code_string)
300 return ver_code
301
302def get_header_info(info, spdx_verification_code, package_info):
303 """
304 Put together the header SPDX information.
305 Eventually this needs to become a lot less
306 of a hardcoded thing.
307 """
308 from datetime import datetime
309 import os
310 head = []
311 DEFAULT = "NOASSERTION"
312
313 package_checksum = hash_file(info['tar_file'])
314 if package_checksum is None:
315 package_checksum = DEFAULT
316
317 ## document level information
318 head.append("## SPDX Document Information")
319 head.append("SPDXVersion: " + info['spdx_version'])
320 head.append("DataLicense: " + info['data_license'])
321 head.append("DocumentComment: <text>SPDX for "
322 + info['pn'] + " version " + info['pv'] + "</text>")
323 head.append("")
324
325 ## Creator information
326 ## Note that this does not give time in UTC.
327 now = datetime.now().strftime('%Y-%m-%dT%H:%M:%SZ')
328 head.append("## Creation Information")
329 ## Tools are supposed to have a version, but FOSSology+SPDX provides none.
330 head.append("Creator: Tool: FOSSology+SPDX")
331 head.append("Created: " + now)
332 head.append("CreatorComment: <text>UNO</text>")
333 head.append("")
334
335 ## package level information
336 head.append("## Package Information")
337 head.append("PackageName: " + info['pn'])
338 head.append("PackageVersion: " + info['pv'])
339 head.append("PackageFileName: " + os.path.basename(info['tar_file']))
340 head.append("PackageSupplier: Person:" + DEFAULT)
341 head.append("PackageDownloadLocation: " + DEFAULT)
342 head.append("PackageSummary: <text></text>")
343 head.append("PackageOriginator: Person:" + DEFAULT)
344 head.append("PackageChecksum: SHA1: " + package_checksum)
345 head.append("PackageVerificationCode: " + spdx_verification_code)
346 head.append("PackageDescription: <text>" + info['pn']
347 + " version " + info['pv'] + "</text>")
348 head.append("")
349 head.append("PackageCopyrightText: "
350 + package_info['PackageCopyrightText'])
351 head.append("")
352 head.append("PackageLicenseDeclared: "
353 + package_info['PackageLicenseDeclared'])
354 head.append("PackageLicenseConcluded: "
355 + package_info['PackageLicenseConcluded'])
356
357 for licref in package_info['PackageLicenseInfoFromFiles']:
358 head.append("PackageLicenseInfoFromFiles: " + licref)
359 head.append("")
360
361 ## header for file level
362 head.append("## File Information")
363 head.append("")
364
365 return '\n'.join(head)