1
0
mirror of https://git.yoctoproject.org/poky synced 2026-06-03 01:40:07 +00:00

insane: improve license checksumming logic

Instead of opening files as bytes and battling decoding to UTF-8 which can throw
exceptions, open directly as strings and replace invalid codepoints.  This
handles licenses in encodings which are not UTF-8 but are based on ASCII much
better.

Also instead of extracting the license lines, writing them to a file, and then
hashing the file, hash the lines directly.

(From OE-Core rev: 63ef9d342277c4ba541b78cbb45ef181f071f495)

Signed-off-by: Ross Burton <ross.burton@intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
This commit is contained in:
Ross Burton
2019-03-21 12:30:42 +00:00
committed by Richard Purdie
parent 2187d06166
commit 24b9bcf043
+32 -49
View File
@@ -458,7 +458,6 @@ python populate_lic_qa_checksum() {
""" """
Check for changes in the license files. Check for changes in the license files.
""" """
import tempfile
sane = True sane = True
lic_files = d.getVar('LIC_FILES_CHKSUM') or '' lic_files = d.getVar('LIC_FILES_CHKSUM') or ''
@@ -496,61 +495,45 @@ python populate_lic_qa_checksum() {
if (not beginline) and (not endline): if (not beginline) and (not endline):
md5chksum = bb.utils.md5_file(srclicfile) md5chksum = bb.utils.md5_file(srclicfile)
with open(srclicfile, 'rb') as f: with open(srclicfile, 'r', errors='replace') as f:
license = f.read() license = f.read().splitlines()
else: else:
fi = open(srclicfile, 'rb') with open(srclicfile, 'rb') as f:
fo = tempfile.NamedTemporaryFile(mode='wb', prefix='poky.', suffix='.tmp', delete=False) import hashlib
tmplicfile = fo.name; lineno = 0
lineno = 0 license = []
linesout = 0 m = hashlib.md5()
license = [] for line in f:
for line in fi: lineno += 1
lineno += 1 if (lineno >= beginline):
if (lineno >= beginline): if ((lineno <= endline) or not endline):
if ((lineno <= endline) or not endline): m.update(line)
fo.write(line) license.append(line.decode('utf-8', errors='replace').rstrip())
license.append(line) else:
linesout += 1 break
else: md5chksum = m.hexdigest()
break
fo.flush()
fo.close()
fi.close()
md5chksum = bb.utils.md5_file(tmplicfile)
license = b''.join(license)
os.unlink(tmplicfile)
if recipemd5 == md5chksum: if recipemd5 == md5chksum:
bb.note (pn + ": md5 checksum matched for ", url) bb.note (pn + ": md5 checksum matched for ", url)
else: else:
if recipemd5: if recipemd5:
msg = pn + ": The LIC_FILES_CHKSUM does not match for " + url msg = pn + ": The LIC_FILES_CHKSUM does not match for " + url
msg = msg + "\n" + pn + ": The new md5 checksum is " + md5chksum msg = msg + "\n" + pn + ": The new md5 checksum is " + md5chksum
try: max_lines = int(d.getVar('QA_MAX_LICENSE_LINES') or 20)
license_lines = license.decode('utf-8').split('\n') if not license or license[-1] != '':
except: # Ensure that our license text ends with a line break
# License text might not be valid UTF-8, in which # (will be added with join() below).
# case we don't know how to include it in our output license.append('')
# and have to skip it. remove = len(license) - max_lines
pass if remove > 0:
else: start = max_lines // 2
max_lines = int(d.getVar('QA_MAX_LICENSE_LINES') or 20) end = start + remove - 1
if not license_lines or license_lines[-1] != '': del license[start:end]
# Ensure that our license text ends with a line break license.insert(start, '...')
# (will be added with join() below). msg = msg + "\n" + pn + ": Here is the selected license text:" + \
license_lines.append('') "\n" + \
remove = len(license_lines) - max_lines "{:v^70}".format(" beginline=%d " % beginline if beginline else "") + \
if remove > 0: "\n" + "\n".join(license) + \
start = max_lines // 2 "{:^^70}".format(" endline=%d " % endline if endline else "")
end = start + remove - 1
del license_lines[start:end]
license_lines.insert(start, '...')
msg = msg + "\n" + pn + ": Here is the selected license text:" + \
"\n" + \
"{:v^70}".format(" beginline=%d " % beginline if beginline else "") + \
"\n" + "\n".join(license_lines) + \
"{:^^70}".format(" endline=%d " % endline if endline else "")
if beginline: if beginline:
if endline: if endline:
srcfiledesc = "%s (lines %d through to %d)" % (srclicfile, beginline, endline) srcfiledesc = "%s (lines %d through to %d)" % (srclicfile, beginline, endline)