diff options
| author | Joshua Watt <JPEWhacker@gmail.com> | 2025-02-13 10:18:17 -0700 |
|---|---|---|
| committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2025-02-18 11:56:03 +0000 |
| commit | 54e4a89a758906102067dca19f1d3212e0d76ad9 (patch) | |
| tree | 23d30f8d36ff0f00648a354920d48c2d5414923b | |
| parent | d55cd3fc39b638b4b765b07c484e58880996febf (diff) | |
| download | poky-54e4a89a758906102067dca19f1d3212e0d76ad9.tar.gz | |
spdx30: Improve os.walk() handling
There have been errors seen when assembling root file system SPDX
documents where they will references files that don't exist in the
package SPDX.
The speculation is that this is caused by os.walk() ignoring errors when
walking, causing files to be omitted. Improve the code by adding an
error handler to os.walk() to report errors when they occur.
In addition, sort the files and directories while walking to ensure
consistent ordering of the file SPDX IDs.
(From OE-Core rev: 86b581e80637cd8136ce7a7e95db94d9553d2f60)
Signed-off-by: Joshua Watt <JPEWhacker@gmail.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
| -rw-r--r-- | meta/lib/oe/spdx30_tasks.py | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/meta/lib/oe/spdx30_tasks.py b/meta/lib/oe/spdx30_tasks.py index 6a39246fe1..e3e5dbc742 100644 --- a/meta/lib/oe/spdx30_tasks.py +++ b/meta/lib/oe/spdx30_tasks.py | |||
| @@ -19,6 +19,10 @@ from datetime import datetime, timezone | |||
| 19 | from pathlib import Path | 19 | from pathlib import Path |
| 20 | 20 | ||
| 21 | 21 | ||
| 22 | def walk_error(err): | ||
| 23 | bb.error(f"ERROR walking {err.filename}: {err}") | ||
| 24 | |||
| 25 | |||
| 22 | def set_timestamp_now(d, o, prop): | 26 | def set_timestamp_now(d, o, prop): |
| 23 | if d.getVar("SPDX_INCLUDE_TIMESTAMPS") == "1": | 27 | if d.getVar("SPDX_INCLUDE_TIMESTAMPS") == "1": |
| 24 | setattr(o, prop, datetime.now(timezone.utc)) | 28 | setattr(o, prop, datetime.now(timezone.utc)) |
| @@ -148,11 +152,13 @@ def add_package_files( | |||
| 148 | spdx_files = set() | 152 | spdx_files = set() |
| 149 | 153 | ||
| 150 | file_counter = 1 | 154 | file_counter = 1 |
| 151 | for subdir, dirs, files in os.walk(topdir): | 155 | for subdir, dirs, files in os.walk(topdir, onerror=walk_error): |
| 152 | dirs[:] = [d for d in dirs if d not in ignore_dirs] | 156 | dirs[:] = [d for d in dirs if d not in ignore_dirs] |
| 153 | if subdir == str(topdir): | 157 | if subdir == str(topdir): |
| 154 | dirs[:] = [d for d in dirs if d not in ignore_top_level_dirs] | 158 | dirs[:] = [d for d in dirs if d not in ignore_top_level_dirs] |
| 155 | 159 | ||
| 160 | dirs.sort() | ||
| 161 | files.sort() | ||
| 156 | for file in files: | 162 | for file in files: |
| 157 | filepath = Path(subdir) / file | 163 | filepath = Path(subdir) / file |
| 158 | if filepath.is_symlink() or not filepath.is_file(): | 164 | if filepath.is_symlink() or not filepath.is_file(): |
| @@ -356,7 +362,9 @@ def add_download_files(d, objset): | |||
| 356 | if fd.type == "file": | 362 | if fd.type == "file": |
| 357 | if os.path.isdir(fd.localpath): | 363 | if os.path.isdir(fd.localpath): |
| 358 | walk_idx = 1 | 364 | walk_idx = 1 |
| 359 | for root, dirs, files in os.walk(fd.localpath): | 365 | for root, dirs, files in os.walk(fd.localpath, onerror=walk_error): |
| 366 | dirs.sort() | ||
| 367 | files.sort() | ||
| 360 | for f in files: | 368 | for f in files: |
| 361 | f_path = os.path.join(root, f) | 369 | f_path = os.path.join(root, f) |
| 362 | if os.path.islink(f_path): | 370 | if os.path.islink(f_path): |
| @@ -1046,7 +1054,9 @@ def create_rootfs_spdx(d): | |||
| 1046 | collect_build_package_inputs(d, objset, rootfs_build, packages, files_by_hash) | 1054 | collect_build_package_inputs(d, objset, rootfs_build, packages, files_by_hash) |
| 1047 | 1055 | ||
| 1048 | files = set() | 1056 | files = set() |
| 1049 | for dirpath, dirnames, filenames in os.walk(image_rootfs): | 1057 | for dirpath, dirnames, filenames in os.walk(image_rootfs, onerror=walk_error): |
| 1058 | dirnames.sort() | ||
| 1059 | filenames.sort() | ||
| 1050 | for fn in filenames: | 1060 | for fn in filenames: |
| 1051 | fpath = Path(dirpath) / fn | 1061 | fpath = Path(dirpath) / fn |
| 1052 | if not fpath.is_file() or fpath.is_symlink(): | 1062 | if not fpath.is_file() or fpath.is_symlink(): |
| @@ -1282,7 +1292,9 @@ def create_sdk_sbom(d, sdk_deploydir, spdx_work_dir, toolchain_outputname): | |||
| 1282 | root_files = [] | 1292 | root_files = [] |
| 1283 | 1293 | ||
| 1284 | # NOTE: os.walk() doesn't return symlinks | 1294 | # NOTE: os.walk() doesn't return symlinks |
| 1285 | for dirpath, dirnames, filenames in os.walk(sdk_deploydir): | 1295 | for dirpath, dirnames, filenames in os.walk(sdk_deploydir, onerror=walk_error): |
| 1296 | dirnames.sort() | ||
| 1297 | filenames.sort() | ||
| 1286 | for fn in filenames: | 1298 | for fn in filenames: |
| 1287 | fpath = Path(dirpath) / fn | 1299 | fpath = Path(dirpath) / fn |
| 1288 | if not fpath.is_file() or fpath.is_symlink(): | 1300 | if not fpath.is_file() or fpath.is_symlink(): |
