aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoshua Watt <JPEWhacker@gmail.com>2021-09-01 08:44:51 -0500
committerRichard Purdie <richard.purdie@linuxfoundation.org>2021-09-03 09:03:36 +0100
commita6d9de5350937c7e25899491db59f473345f0b69 (patch)
tree33c261c512d66aaf66dd5b50ff32f01b79fecf92
parent28d9d035c0ff8fcaf28bc96a976a43a602a47e94 (diff)
downloadopenembedded-core-contrib-a6d9de5350937c7e25899491db59f473345f0b69.tar.gz
openembedded-core-contrib-a6d9de5350937c7e25899491db59f473345f0b69.tar.bz2
openembedded-core-contrib-a6d9de5350937c7e25899491db59f473345f0b69.zip
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 <JPEWhacker@gmail.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
-rw-r--r--meta/classes/create-spdx.bbclass49
1 files 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]