From a6d9de5350937c7e25899491db59f473345f0b69 Mon Sep 17 00:00:00 2001 From: Joshua Watt Date: Wed, 1 Sep 2021 08:44:51 -0500 Subject: classes/create-spdx: Speed up hash calculations Use the bb.utils.sha* utilities to hash files since they are much faster than the loops we were rolling ourselves Signed-off-by: Joshua Watt Signed-off-by: Richard Purdie --- meta/classes/create-spdx.bbclass | 49 +++++++++++++--------------------------- 1 file changed, 16 insertions(+), 33 deletions(-) diff --git a/meta/classes/create-spdx.bbclass b/meta/classes/create-spdx.bbclass index 2e13b19b5b..2638b3dc97 100644 --- a/meta/classes/create-spdx.bbclass +++ b/meta/classes/create-spdx.bbclass @@ -122,6 +122,8 @@ def add_package_files(d, doc, spdx_pkg, topdir, get_spdxid, get_types, *, archiv import hashlib source_date_epoch = d.getVar("SOURCE_DATE_EPOCH") + if source_date_epoch: + source_date_epoch = int(source_date_epoch) sha1s = [] spdx_files = [] @@ -143,22 +145,8 @@ def add_package_files(d, doc, spdx_pkg, topdir, get_spdxid, get_types, *, archiv spdx_file.fileTypes.append(t) spdx_file.fileName = filename - hashes = { - "SHA1": hashlib.sha1(), - "SHA256": hashlib.sha256(), - } - - with filepath.open("rb") as f: - while True: - chunk = f.read(4096) - if not chunk: - break - - for h in hashes.values(): - h.update(chunk) - - if archive is not None: - f.seek(0) + if archive is not None: + with filepath.open("rb") as f: info = archive.gettarinfo(fileobj=f) info.name = filename info.uid = 0 @@ -166,18 +154,21 @@ def add_package_files(d, doc, spdx_pkg, topdir, get_spdxid, get_types, *, archiv info.uname = "root" info.gname = "root" - if source_date_epoch is not None and info.mtime > int(source_date_epoch): - info.mtime = int(source_date_epoch) + if source_date_epoch is not None and info.mtime > source_date_epoch: + info.mtime = source_date_epoch archive.addfile(info, f) - for k, v in hashes.items(): - spdx_file.checksums.append(oe.spdx.SPDXChecksum( - algorithm=k, - checksumValue=v.hexdigest(), + sha1 = bb.utils.sha1_file(filepath) + sha1s.append(sha1) + spdx_file.checksums.append(oe.spdx.SPDXChecksum( + algorithm="SHA1", + checksumValue=sha1, + )) + spdx_file.checksums.append(oe.spdx.SPDXChecksum( + algorithm="SHA256", + checksumValue=bb.utils.sha256_file(filepath), )) - - sha1s.append(hashes["SHA1"].hexdigest()) doc.files.append(spdx_file) doc.add_relationship(spdx_pkg, "CONTAINS", spdx_file) @@ -231,15 +222,7 @@ def add_package_sources_from_debug(d, package_doc, spdx_package, package, packag if not debugsrc_path.exists(): continue - with debugsrc_path.open("rb") as f: - sha = hashlib.sha256() - while True: - chunk = f.read(4096) - if not chunk: - break - sha.update(chunk) - - file_sha256 = sha.hexdigest() + file_sha256 = bb.utils.sha256_file(debugsrc_path) if file_sha256 in sources: source_file = sources[file_sha256] -- cgit 1.2.3-korg