lib/spdx30_tasks: support directories deployed by image recipes

create_image_spdx() implementation assumes that image is indeed a file.
If image recipe deploys a directory (for example, which contains an
hierarchy of flash artifacts, that is used by SoC vendor-specific
flashing tool) which follows ${IMAGE_NAME}.${IMAGE_TYPE} naming scheme,
create_image_spdx() function will fail after trying to hash a directory:

*** 0002:do_create_image_spdx(d)
     0003:
File: '.../meta/classes-recipe/create-spdx-image-3.0.bbclass', lineno: 48, function: do_create_image_spdx
     0044:addtask do_create_rootfs_spdx_setscene
     0045:
     0046:python do_create_image_spdx() {
     0047:    import oe.spdx30_tasks
 *** 0048:    oe.spdx30_tasks.create_image_spdx(d)
     0049:}
     0050:addtask do_create_image_spdx after do_image_complete do_create_rootfs_spdx before do_build
     0051:SSTATETASKS += "do_create_image_spdx"
...
File: '.../bitbake/lib/bb/utils.py', lineno: 536, function: _hasher
     0532:
     0533:def _hasher(method, filename):
     0534:    import mmap
     0535:
 *** 0536:    with open(filename, "rb") as f:
     0537:        try:
     0538:            with mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) as mm:
     0539:                for chunk in iter(lambda: mm.read(8192), b''):
     0540:                    method.update(chunk)
Exception: IsADirectoryError: [Errno 21] Is a directory: '...'

(From OE-Core rev: a0d63082a4db375a55586c7864e280cd8f45ff7b)

Signed-off-by: Igor Opaniuk <igor.opaniuk@foundries.io>
Signed-off-by: Mathieu Dubois-Briand <mathieu.dubois-briand@bootlin.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
This commit is contained in:
Igor Opaniuk
2025-01-15 14:07:02 +01:00
committed by Richard Purdie
parent 08680a5b54
commit f186e405c5

View File

@@ -135,7 +135,7 @@ def add_package_files(
topdir,
get_spdxid,
get_purposes,
license_data,
license_data=None,
*,
archive=None,
ignore_dirs=[],
@@ -169,7 +169,10 @@ def add_package_files(
)
spdx_files.add(spdx_file)
if oe.spdx30.software_SoftwarePurpose.source in file_purposes:
if (
oe.spdx30.software_SoftwarePurpose.source in file_purposes
and license_data is not None
):
objset.scan_declared_licenses(spdx_file, filepath, license_data)
if archive is not None:
@@ -1072,25 +1075,45 @@ def create_image_spdx(d):
for image in task["images"]:
image_filename = image["filename"]
image_path = image_deploy_dir / image_filename
a = objset.add_root(
oe.spdx30.software_File(
_id=objset.new_spdxid("image", image_filename),
creationInfo=objset.doc.creationInfo,
name=image_filename,
verifiedUsing=[
oe.spdx30.Hash(
algorithm=oe.spdx30.HashAlgorithm.sha256,
hashValue=bb.utils.sha256_file(image_path),
)
],
if os.path.isdir(image_path):
a = add_package_files(
d,
objset,
image_path,
lambda file_counter: objset.new_spdxid(
"imagefile", str(file_counter)
),
lambda filepath: [],
license_data=None,
ignore_dirs=[],
ignore_top_level_dirs=[],
archive=None,
)
artifacts.extend(a)
else:
a = objset.add_root(
oe.spdx30.software_File(
_id=objset.new_spdxid("image", image_filename),
creationInfo=objset.doc.creationInfo,
name=image_filename,
verifiedUsing=[
oe.spdx30.Hash(
algorithm=oe.spdx30.HashAlgorithm.sha256,
hashValue=bb.utils.sha256_file(image_path),
)
],
)
)
)
set_purposes(
d, a, "SPDX_IMAGE_PURPOSE:%s" % imagetype, "SPDX_IMAGE_PURPOSE"
)
set_timestamp_now(d, a, "builtTime")
artifacts.append(a)
artifacts.append(a)
for a in artifacts:
set_purposes(
d, a, "SPDX_IMAGE_PURPOSE:%s" % imagetype, "SPDX_IMAGE_PURPOSE"
)
set_timestamp_now(d, a, "builtTime")
if artifacts:
objset.new_scoped_relationship(