diff options
author | Joshua Watt <JPEWhacker@gmail.com> | 2021-09-01 08:44:51 -0500 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2021-09-03 09:53:28 +0100 |
commit | beeb0f9e53a7629a21679107db46e33e032a4d90 (patch) | |
tree | ae3b65e66aa4e3ddac0cc25f094b86fd19c4eea4 /meta | |
parent | 902480107dd0db7b77ada4654d2e0f061a04765d (diff) | |
download | poky-beeb0f9e53a7629a21679107db46e33e032a4d90.tar.gz |
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
(From OE-Core rev: a6d9de5350937c7e25899491db59f473345f0b69)
Signed-off-by: Joshua Watt <JPEWhacker@gmail.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'meta')
-rw-r--r-- | meta/classes/create-spdx.bbclass | 49 |
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 | |||
122 | import hashlib | 122 | import hashlib |
123 | 123 | ||
124 | source_date_epoch = d.getVar("SOURCE_DATE_EPOCH") | 124 | source_date_epoch = d.getVar("SOURCE_DATE_EPOCH") |
125 | if source_date_epoch: | ||
126 | source_date_epoch = int(source_date_epoch) | ||
125 | 127 | ||
126 | sha1s = [] | 128 | sha1s = [] |
127 | spdx_files = [] | 129 | spdx_files = [] |
@@ -143,22 +145,8 @@ def add_package_files(d, doc, spdx_pkg, topdir, get_spdxid, get_types, *, archiv | |||
143 | spdx_file.fileTypes.append(t) | 145 | spdx_file.fileTypes.append(t) |
144 | spdx_file.fileName = filename | 146 | spdx_file.fileName = filename |
145 | 147 | ||
146 | hashes = { | 148 | if archive is not None: |
147 | "SHA1": hashlib.sha1(), | 149 | with filepath.open("rb") as f: |
148 | "SHA256": hashlib.sha256(), | ||
149 | } | ||
150 | |||
151 | with filepath.open("rb") as f: | ||
152 | while True: | ||
153 | chunk = f.read(4096) | ||
154 | if not chunk: | ||
155 | break | ||
156 | |||
157 | for h in hashes.values(): | ||
158 | h.update(chunk) | ||
159 | |||
160 | if archive is not None: | ||
161 | f.seek(0) | ||
162 | info = archive.gettarinfo(fileobj=f) | 150 | info = archive.gettarinfo(fileobj=f) |
163 | info.name = filename | 151 | info.name = filename |
164 | info.uid = 0 | 152 | info.uid = 0 |
@@ -166,18 +154,21 @@ def add_package_files(d, doc, spdx_pkg, topdir, get_spdxid, get_types, *, archiv | |||
166 | info.uname = "root" | 154 | info.uname = "root" |
167 | info.gname = "root" | 155 | info.gname = "root" |
168 | 156 | ||
169 | if source_date_epoch is not None and info.mtime > int(source_date_epoch): | 157 | if source_date_epoch is not None and info.mtime > source_date_epoch: |
170 | info.mtime = int(source_date_epoch) | 158 | info.mtime = source_date_epoch |
171 | 159 | ||
172 | archive.addfile(info, f) | 160 | archive.addfile(info, f) |
173 | 161 | ||
174 | for k, v in hashes.items(): | 162 | sha1 = bb.utils.sha1_file(filepath) |
175 | spdx_file.checksums.append(oe.spdx.SPDXChecksum( | 163 | sha1s.append(sha1) |
176 | algorithm=k, | 164 | spdx_file.checksums.append(oe.spdx.SPDXChecksum( |
177 | checksumValue=v.hexdigest(), | 165 | algorithm="SHA1", |
166 | checksumValue=sha1, | ||
167 | )) | ||
168 | spdx_file.checksums.append(oe.spdx.SPDXChecksum( | ||
169 | algorithm="SHA256", | ||
170 | checksumValue=bb.utils.sha256_file(filepath), | ||
178 | )) | 171 | )) |
179 | |||
180 | sha1s.append(hashes["SHA1"].hexdigest()) | ||
181 | 172 | ||
182 | doc.files.append(spdx_file) | 173 | doc.files.append(spdx_file) |
183 | doc.add_relationship(spdx_pkg, "CONTAINS", spdx_file) | 174 | 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 | |||
231 | if not debugsrc_path.exists(): | 222 | if not debugsrc_path.exists(): |
232 | continue | 223 | continue |
233 | 224 | ||
234 | with debugsrc_path.open("rb") as f: | 225 | file_sha256 = bb.utils.sha256_file(debugsrc_path) |
235 | sha = hashlib.sha256() | ||
236 | while True: | ||
237 | chunk = f.read(4096) | ||
238 | if not chunk: | ||
239 | break | ||
240 | sha.update(chunk) | ||
241 | |||
242 | file_sha256 = sha.hexdigest() | ||
243 | 226 | ||
244 | if file_sha256 in sources: | 227 | if file_sha256 in sources: |
245 | source_file = sources[file_sha256] | 228 | source_file = sources[file_sha256] |