From df6509303033a2d824693bb68b550c486f9963ce Mon Sep 17 00:00:00 2001 From: Pieter De Gendt Date: Sat, 24 May 2025 21:47:34 +0200 Subject: [PATCH 1/9] scripts: west_commands: zspdx: cmakecache: Fix linter issue Fix redundant open mode. Signed-off-by: Pieter De Gendt --- .ruff-excludes.toml | 3 --- scripts/west_commands/zspdx/cmakecache.py | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/.ruff-excludes.toml b/.ruff-excludes.toml index adf13afda883..a7a80d92d448 100644 --- a/.ruff-excludes.toml +++ b/.ruff-excludes.toml @@ -1232,9 +1232,6 @@ "UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes "UP032", # https://docs.astral.sh/ruff/rules/f-string ] -"./scripts/west_commands/zspdx/cmakecache.py" = [ - "UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes -] "./scripts/west_commands/zspdx/cmakefileapi.py" = [ "F541", # https://docs.astral.sh/ruff/rules/f-string-missing-placeholders "I001", # https://docs.astral.sh/ruff/rules/unsorted-imports diff --git a/scripts/west_commands/zspdx/cmakecache.py b/scripts/west_commands/zspdx/cmakecache.py index 612cee0170fb..323db9812d7f 100644 --- a/scripts/west_commands/zspdx/cmakecache.py +++ b/scripts/west_commands/zspdx/cmakecache.py @@ -11,7 +11,7 @@ def parseCMakeCacheFile(filePath): log.dbg(f"parsing CMake cache file at {filePath}") kv = {} try: - with open(filePath, "r") as f: + with open(filePath) as f: # should be a short file, so we'll use readlines lines = f.readlines() From 8f45b2daeb16e9b95ca9765ab7663fdd5bf73fef Mon Sep 17 00:00:00 2001 From: Pieter De Gendt Date: Sat, 24 May 2025 21:49:04 +0200 Subject: [PATCH 2/9] scripts: west_commands: zspdx: cmakefileapi: Fix linter issues Fix issues reported by ruff. Signed-off-by: Pieter De Gendt --- .ruff-excludes.toml | 5 --- scripts/west_commands/zspdx/cmakefileapi.py | 35 +++++++++++---------- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/.ruff-excludes.toml b/.ruff-excludes.toml index a7a80d92d448..498c43dafce8 100644 --- a/.ruff-excludes.toml +++ b/.ruff-excludes.toml @@ -1232,11 +1232,6 @@ "UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes "UP032", # https://docs.astral.sh/ruff/rules/f-string ] -"./scripts/west_commands/zspdx/cmakefileapi.py" = [ - "F541", # https://docs.astral.sh/ruff/rules/f-string-missing-placeholders - "I001", # https://docs.astral.sh/ruff/rules/unsorted-imports - "UP008", # https://docs.astral.sh/ruff/rules/super-call-with-parameters -] "./scripts/west_commands/zspdx/cmakefileapijson.py" = [ "E501", # https://docs.astral.sh/ruff/rules/line-too-long "F541", # https://docs.astral.sh/ruff/rules/f-string-missing-placeholders diff --git a/scripts/west_commands/zspdx/cmakefileapi.py b/scripts/west_commands/zspdx/cmakefileapi.py index 772709f156be..6719ac2d2d1d 100644 --- a/scripts/west_commands/zspdx/cmakefileapi.py +++ b/scripts/west_commands/zspdx/cmakefileapi.py @@ -4,10 +4,11 @@ from enum import Enum + class Codemodel: def __init__(self): - super(Codemodel, self).__init__() + super().__init__() self.paths_source = "" self.paths_build = "" @@ -20,7 +21,7 @@ def __repr__(self): class Config: def __init__(self): - super(Config, self).__init__() + super().__init__() self.name = "" self.directories = [] @@ -29,7 +30,7 @@ def __init__(self): def __repr__(self): if self.name == "": - return f"Config: [no name]" + return "Config: [no name]" else: return f"Config: {self.name}" @@ -37,7 +38,7 @@ def __repr__(self): class ConfigDir: def __init__(self): - super(ConfigDir, self).__init__() + super().__init__() self.source = "" self.build = "" @@ -61,7 +62,7 @@ def __repr__(self): class ConfigProject: def __init__(self): - super(ConfigProject, self).__init__() + super().__init__() self.name = "" self.parentIndex = -1 @@ -82,7 +83,7 @@ def __repr__(self): class ConfigTarget: def __init__(self): - super(ConfigTarget, self).__init__() + super().__init__() self.name = "" self.id = "" @@ -114,7 +115,7 @@ class TargetType(Enum): class TargetInstallDestination: def __init__(self): - super(TargetInstallDestination, self).__init__() + super().__init__() self.path = "" self.backtrace = -1 @@ -127,7 +128,7 @@ def __repr__(self): class TargetCommandFragment: def __init__(self): - super(TargetCommandFragment, self).__init__() + super().__init__() self.fragment = "" self.role = "" @@ -139,7 +140,7 @@ def __repr__(self): class TargetDependency: def __init__(self): - super(TargetDependency, self).__init__() + super().__init__() self.id = "" self.backtrace = -1 @@ -151,7 +152,7 @@ def __repr__(self): class TargetSource: def __init__(self): - super(TargetSource, self).__init__() + super().__init__() self.path = "" self.compileGroupIndex = -1 @@ -170,7 +171,7 @@ def __repr__(self): class TargetSourceGroup: def __init__(self): - super(TargetSourceGroup, self).__init__() + super().__init__() self.name = "" self.sourceIndexes = [] @@ -185,7 +186,7 @@ def __repr__(self): class TargetCompileGroupInclude: def __init__(self): - super(TargetCompileGroupInclude, self).__init__() + super().__init__() self.path = "" self.isSystem = False @@ -198,7 +199,7 @@ def __repr__(self): class TargetCompileGroupPrecompileHeader: def __init__(self): - super(TargetCompileGroupPrecompileHeader, self).__init__() + super().__init__() self.header = "" self.backtrace = -1 @@ -210,7 +211,7 @@ def __repr__(self): class TargetCompileGroupDefine: def __init__(self): - super(TargetCompileGroupDefine, self).__init__() + super().__init__() self.define = "" self.backtrace = -1 @@ -222,7 +223,7 @@ def __repr__(self): class TargetCompileGroup: def __init__(self): - super(TargetCompileGroup, self).__init__() + super().__init__() self.sourceIndexes = [] self.language = "" @@ -242,7 +243,7 @@ def __repr__(self): class TargetBacktraceGraphNode: def __init__(self): - super(TargetBacktraceGraphNode, self).__init__() + super().__init__() self.file = -1 self.line = -1 @@ -257,7 +258,7 @@ def __repr__(self): class Target: def __init__(self): - super(Target, self).__init__() + super().__init__() self.name = "" self.id = "" From df1e8540eb8fb52e8cce8fe3ae5482229928acc9 Mon Sep 17 00:00:00 2001 From: Pieter De Gendt Date: Sat, 24 May 2025 22:04:02 +0200 Subject: [PATCH 3/9] scripts: west_commands: zspdx: cmakefileapijson: Fix linter issues Fix issues reported by ruff. Signed-off-by: Pieter De Gendt --- .ruff-excludes.toml | 7 --- .../west_commands/zspdx/cmakefileapijson.py | 44 +++++++++---------- 2 files changed, 21 insertions(+), 30 deletions(-) diff --git a/.ruff-excludes.toml b/.ruff-excludes.toml index 498c43dafce8..7d08f754cfc1 100644 --- a/.ruff-excludes.toml +++ b/.ruff-excludes.toml @@ -1232,13 +1232,6 @@ "UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes "UP032", # https://docs.astral.sh/ruff/rules/f-string ] -"./scripts/west_commands/zspdx/cmakefileapijson.py" = [ - "E501", # https://docs.astral.sh/ruff/rules/line-too-long - "F541", # https://docs.astral.sh/ruff/rules/f-string-missing-placeholders - "I001", # https://docs.astral.sh/ruff/rules/unsorted-imports - "SIM116", # https://docs.astral.sh/ruff/rules/if-else-block-instead-of-dict-lookup - "UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes -] "./scripts/west_commands/zspdx/datatypes.py" = [ "UP008", # https://docs.astral.sh/ruff/rules/super-call-with-parameters ] diff --git a/scripts/west_commands/zspdx/cmakefileapijson.py b/scripts/west_commands/zspdx/cmakefileapijson.py index 921a092742b1..bf74a48ea3ca 100644 --- a/scripts/west_commands/zspdx/cmakefileapijson.py +++ b/scripts/west_commands/zspdx/cmakefileapijson.py @@ -9,28 +9,29 @@ import zspdx.cmakefileapi + def parseReply(replyIndexPath): replyDir, _ = os.path.split(replyIndexPath) # first we need to find the codemodel reply file try: - with open(replyIndexPath, 'r') as indexFile: + with open(replyIndexPath) as indexFile: js = json.load(indexFile) # get reply object reply_dict = js.get("reply", {}) if reply_dict == {}: - log.err(f"no \"reply\" field found in index file") + log.err('no "reply" field found in index file') return None # get codemodel object cm_dict = reply_dict.get("codemodel-v2", {}) if cm_dict == {}: - log.err(f"no \"codemodel-v2\" field found in \"reply\" object in index file") + log.err('no "codemodel-v2" field found in "reply" object in index file') return None # and get codemodel filename jsonFile = cm_dict.get("jsonFile", "") if jsonFile == "": - log.err(f"no \"jsonFile\" field found in \"codemodel-v2\" object in index file") + log.err('no "jsonFile" field found in "codemodel-v2" object in index file') return None return parseCodemodel(replyDir, jsonFile) @@ -46,7 +47,7 @@ def parseCodemodel(replyDir, codemodelFile): codemodelPath = os.path.join(replyDir, codemodelFile) try: - with open(codemodelPath, 'r') as cmFile: + with open(codemodelPath) as cmFile: js = json.load(cmFile) cm = zspdx.cmakefileapi.Codemodel() @@ -54,15 +55,18 @@ def parseCodemodel(replyDir, codemodelFile): # for correctness, check kind and version kind = js.get("kind", "") if kind != "codemodel": - log.err(f"Error loading CMake API reply: expected \"kind\":\"codemodel\" in {codemodelPath}, got {kind}") + log.err('Error loading CMake API reply: expected "kind":"codemodel" ' + f'in {codemodelPath}, got {kind}') return None version = js.get("version", {}) versionMajor = version.get("major", -1) if versionMajor != 2: if versionMajor == -1: - log.err(f"Error loading CMake API reply: expected major version 2 in {codemodelPath}, no version found") + log.err("Error loading CMake API reply: expected major version 2 " + f"in {codemodelPath}, no version found") return None - log.err(f"Error loading CMake API reply: expected major version 2 in {codemodelPath}, got {versionMajor}") + log.err("Error loading CMake API reply: expected major version 2 " + f"in {codemodelPath}, got {versionMajor}") return None # get paths @@ -143,7 +147,7 @@ def parseConfig(cfg_dict, replyDir): def parseTarget(targetPath): try: - with open(targetPath, 'r') as targetFile: + with open(targetPath) as targetFile: js = json.load(targetFile) target = zspdx.cmakefileapi.Target() @@ -191,20 +195,14 @@ def parseTarget(targetPath): return None def parseTargetType(targetType): - if targetType == "EXECUTABLE": - return zspdx.cmakefileapi.TargetType.EXECUTABLE - elif targetType == "STATIC_LIBRARY": - return zspdx.cmakefileapi.TargetType.STATIC_LIBRARY - elif targetType == "SHARED_LIBRARY": - return zspdx.cmakefileapi.TargetType.SHARED_LIBRARY - elif targetType == "MODULE_LIBRARY": - return zspdx.cmakefileapi.TargetType.MODULE_LIBRARY - elif targetType == "OBJECT_LIBRARY": - return zspdx.cmakefileapi.TargetType.OBJECT_LIBRARY - elif targetType == "UTILITY": - return zspdx.cmakefileapi.TargetType.UTILITY - else: - return zspdx.cmakefileapi.TargetType.UNKNOWN + return { + "EXECUTABLE": zspdx.cmakefileapi.TargetType.EXECUTABLE, + "STATIC_LIBRARY": zspdx.cmakefileapi.TargetType.STATIC_LIBRARY, + "SHARED_LIBRARY": zspdx.cmakefileapi.TargetType.SHARED_LIBRARY, + "MODULE_LIBRARY": zspdx.cmakefileapi.TargetType.MODULE_LIBRARY, + "OBJECT_LIBRARY": zspdx.cmakefileapi.TargetType.OBJECT_LIBRARY, + "UTILITY": zspdx.cmakefileapi.TargetType.UTILITY, + }.get(targetType, zspdx.cmakefileapi.TargetType.UNKNOWN) def parseTargetInstall(target, js): install_dict = js.get("install", {}) From 5404fed7946ffebff05b38e3729712c4141223c6 Mon Sep 17 00:00:00 2001 From: Pieter De Gendt Date: Sat, 24 May 2025 22:05:10 +0200 Subject: [PATCH 4/9] scripts: west_commands: zspdx: datatypes: Fix UP008 Replace super call with parameters reported by ruff. Signed-off-by: Pieter De Gendt --- .ruff-excludes.toml | 3 --- scripts/west_commands/zspdx/datatypes.py | 14 +++++++------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/.ruff-excludes.toml b/.ruff-excludes.toml index 7d08f754cfc1..8bc0569ccee1 100644 --- a/.ruff-excludes.toml +++ b/.ruff-excludes.toml @@ -1232,9 +1232,6 @@ "UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes "UP032", # https://docs.astral.sh/ruff/rules/f-string ] -"./scripts/west_commands/zspdx/datatypes.py" = [ - "UP008", # https://docs.astral.sh/ruff/rules/super-call-with-parameters -] "./scripts/west_commands/zspdx/getincludes.py" = [ "I001", # https://docs.astral.sh/ruff/rules/unsorted-imports "UP021", # https://docs.astral.sh/ruff/rules/replace-universal-newlines diff --git a/scripts/west_commands/zspdx/datatypes.py b/scripts/west_commands/zspdx/datatypes.py index 01f2498e0c27..ca996be08e2c 100644 --- a/scripts/west_commands/zspdx/datatypes.py +++ b/scripts/west_commands/zspdx/datatypes.py @@ -9,7 +9,7 @@ # should be built. class DocumentConfig: def __init__(self): - super(DocumentConfig, self).__init__() + super().__init__() # name of document self.name = "" @@ -27,7 +27,7 @@ def __init__(self): class Document: # initialize with a DocumentConfig def __init__(self, cfg): - super(Document, self).__init__() + super().__init__() # configuration - DocumentConfig self.cfg = cfg @@ -63,7 +63,7 @@ def __init__(self, cfg): # be built. class PackageConfig: def __init__(self): - super(PackageConfig, self).__init__() + super().__init__() # package name self.name = "" @@ -106,7 +106,7 @@ class Package: # 1) PackageConfig # 2) the Document that owns this Package def __init__(self, cfg, doc): - super(Package, self).__init__() + super().__init__() # configuration - PackageConfig self.cfg = cfg @@ -153,7 +153,7 @@ class RelationshipDataElementType(Enum): # Relationship after we have organized the SPDX Package and File data. class RelationshipData: def __init__(self): - super(RelationshipData, self).__init__() + super().__init__() # for the "owner" element (e.g., the left side of the Relationship), # is it a filename or a target name (e.g., a Package in the build doc) @@ -191,7 +191,7 @@ def __init__(self): # Document's context. class Relationship: def __init__(self): - super(Relationship, self).__init__() + super().__init__() # SPDX ID for left side of relationship # including "SPDXRef-" as well as "DocumentRef-" if needed @@ -213,7 +213,7 @@ class File: # 1) Document containing this File # 2) Package containing this File def __init__(self, doc, pkg): - super(File, self).__init__() + super().__init__() # absolute path to this file on disk self.abspath = "" From 6185bbd08310b1e2c09abe8393443de88b175cd8 Mon Sep 17 00:00:00 2001 From: Pieter De Gendt Date: Sat, 24 May 2025 22:06:39 +0200 Subject: [PATCH 5/9] scripts: west_commands: zspdx: getincludes: Fix linter issues Fix issues reported by ruff. Signed-off-by: Pieter De Gendt --- .ruff-excludes.toml | 5 ----- scripts/west_commands/zspdx/getincludes.py | 4 ++-- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/.ruff-excludes.toml b/.ruff-excludes.toml index 8bc0569ccee1..26d1acbc16d6 100644 --- a/.ruff-excludes.toml +++ b/.ruff-excludes.toml @@ -1232,11 +1232,6 @@ "UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes "UP032", # https://docs.astral.sh/ruff/rules/f-string ] -"./scripts/west_commands/zspdx/getincludes.py" = [ - "I001", # https://docs.astral.sh/ruff/rules/unsorted-imports - "UP021", # https://docs.astral.sh/ruff/rules/replace-universal-newlines - "UP022", # https://docs.astral.sh/ruff/rules/replace-stdout-stderr -] "./scripts/west_commands/zspdx/sbom.py" = [ "F541", # https://docs.astral.sh/ruff/rules/f-string-missing-placeholders "I001", # https://docs.astral.sh/ruff/rules/unsorted-imports diff --git a/scripts/west_commands/zspdx/getincludes.py b/scripts/west_commands/zspdx/getincludes.py index 27a0c57965b7..86faa44b3e2f 100644 --- a/scripts/west_commands/zspdx/getincludes.py +++ b/scripts/west_commands/zspdx/getincludes.py @@ -2,7 +2,7 @@ # # SPDX-License-Identifier: Apache-2.0 -from subprocess import run, PIPE +from subprocess import run from west import log @@ -30,7 +30,7 @@ def getCIncludes(compilerPath, srcFile, tcg): # prepare command invocation cmd = [compilerPath, "-E", "-H"] + fragments + includes + defines + [srcFile] - cp = run(cmd, stdout=PIPE, stderr=PIPE, universal_newlines=True) + cp = run(cmd, capture_output=True, text=True) if cp.returncode != 0: log.dbg(f" - calling {compilerPath} failed with error code {cp.returncode}") return [] From 9c46f2ca11534d97c4af230a022eea52e4a54cc3 Mon Sep 17 00:00:00 2001 From: Pieter De Gendt Date: Sat, 24 May 2025 22:10:39 +0200 Subject: [PATCH 6/9] scripts: west_commands: zspdx: sbom: Fix linter issues Fix issues reported by ruff. Signed-off-by: Pieter De Gendt --- .ruff-excludes.toml | 6 ------ scripts/west_commands/zspdx/sbom.py | 12 ++++++------ 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/.ruff-excludes.toml b/.ruff-excludes.toml index 26d1acbc16d6..2d3ccb2e2d0e 100644 --- a/.ruff-excludes.toml +++ b/.ruff-excludes.toml @@ -1232,12 +1232,6 @@ "UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes "UP032", # https://docs.astral.sh/ruff/rules/f-string ] -"./scripts/west_commands/zspdx/sbom.py" = [ - "F541", # https://docs.astral.sh/ruff/rules/f-string-missing-placeholders - "I001", # https://docs.astral.sh/ruff/rules/unsorted-imports - "SIM115", # https://docs.astral.sh/ruff/rules/open-file-with-context-handler - "UP008", # https://docs.astral.sh/ruff/rules/super-call-with-parameters -] "./scripts/west_commands/zspdx/scanner.py" = [ "SIM113", # https://docs.astral.sh/ruff/rules/enumerate-for-loop "UP008", # https://docs.astral.sh/ruff/rules/super-call-with-parameters diff --git a/scripts/west_commands/zspdx/sbom.py b/scripts/west_commands/zspdx/sbom.py index dceda34fcaf5..459210eba830 100644 --- a/scripts/west_commands/zspdx/sbom.py +++ b/scripts/west_commands/zspdx/sbom.py @@ -6,8 +6,8 @@ from west import log -from zspdx.walker import WalkerConfig, Walker from zspdx.scanner import ScannerConfig, scanDocument +from zspdx.walker import Walker, WalkerConfig from zspdx.writer import writeSPDX @@ -15,7 +15,7 @@ # SBOM maker subcomponents. class SBOMConfig: def __init__(self): - super(SBOMConfig, self).__init__() + super().__init__() # prefix for Document namespaces; should not end with "/" self.namespacePrefix = "" @@ -58,8 +58,8 @@ def setupCmakeQuery(build_dir): return True else: # file doesn't exist, let's create an empty file - cm_fd = open(queryFilePath, "w") - cm_fd.close() + with open(queryFilePath, "w"): + pass return True @@ -69,8 +69,8 @@ def setupCmakeQuery(build_dir): def makeSPDX(cfg): # report any odd configuration settings if cfg.analyzeIncludes and not cfg.includeSDK: - log.wrn(f"config: requested to analyze includes but not to generate SDK SPDX document;") - log.wrn(f"config: will proceed but will discard detected includes for SDK header files") + log.wrn("config: requested to analyze includes but not to generate SDK SPDX document;") + log.wrn("config: will proceed but will discard detected includes for SDK header files") # set up walker configuration walkerCfg = WalkerConfig() From 63ee7017964cc0ca5147e43cf6be0e326329b889 Mon Sep 17 00:00:00 2001 From: Pieter De Gendt Date: Sat, 24 May 2025 22:13:33 +0200 Subject: [PATCH 7/9] scripts: west_commands: zspdx: scanner: Fix linter issues Fix issues reported by ruff. Signed-off-by: Pieter De Gendt --- .ruff-excludes.toml | 5 ----- scripts/west_commands/zspdx/scanner.py | 8 +++----- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/.ruff-excludes.toml b/.ruff-excludes.toml index 2d3ccb2e2d0e..d73712708b37 100644 --- a/.ruff-excludes.toml +++ b/.ruff-excludes.toml @@ -1232,11 +1232,6 @@ "UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes "UP032", # https://docs.astral.sh/ruff/rules/f-string ] -"./scripts/west_commands/zspdx/scanner.py" = [ - "SIM113", # https://docs.astral.sh/ruff/rules/enumerate-for-loop - "UP008", # https://docs.astral.sh/ruff/rules/super-call-with-parameters - "UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes -] "./scripts/west_commands/zspdx/walker.py" = [ "E501", # https://docs.astral.sh/ruff/rules/line-too-long "F541", # https://docs.astral.sh/ruff/rules/f-string-missing-placeholders diff --git a/scripts/west_commands/zspdx/scanner.py b/scripts/west_commands/zspdx/scanner.py index 21975242acdf..2a4ac792b9cd 100644 --- a/scripts/west_commands/zspdx/scanner.py +++ b/scripts/west_commands/zspdx/scanner.py @@ -16,7 +16,7 @@ # Document scanning should occur. class ScannerConfig: def __init__(self): - super(ScannerConfig, self).__init__() + super().__init__() # when assembling a Package's data, should we auto-conclude the # Package's license, based on the licenses of its Files? @@ -63,11 +63,9 @@ def getExpressionData(filePath, numLines): """ log.dbg(f" - getting licenses for {filePath}") - with open(filePath, "r") as f: + with open(filePath) as f: try: - lineno = 0 - for line in f: - lineno += 1 + for lineno, line in enumerate(f, start=1): if lineno > numLines > 0: break expression = parseLineForExpression(line) From a98e7501022ca6bd5c6f513472a3b67c232ec408 Mon Sep 17 00:00:00 2001 From: Pieter De Gendt Date: Sat, 24 May 2025 22:22:58 +0200 Subject: [PATCH 8/9] scripts: west_commands: zspdx: walker: Fix linter issues Fix issues reported by ruff. Signed-off-by: Pieter De Gendt --- .ruff-excludes.toml | 6 -- scripts/west_commands/zspdx/walker.py | 120 +++++++++++++++++++------- 2 files changed, 89 insertions(+), 37 deletions(-) diff --git a/.ruff-excludes.toml b/.ruff-excludes.toml index d73712708b37..fd5099faa7e6 100644 --- a/.ruff-excludes.toml +++ b/.ruff-excludes.toml @@ -1232,12 +1232,6 @@ "UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes "UP032", # https://docs.astral.sh/ruff/rules/f-string ] -"./scripts/west_commands/zspdx/walker.py" = [ - "E501", # https://docs.astral.sh/ruff/rules/line-too-long - "F541", # https://docs.astral.sh/ruff/rules/f-string-missing-placeholders - "I001", # https://docs.astral.sh/ruff/rules/unsorted-imports - "UP008", # https://docs.astral.sh/ruff/rules/super-call-with-parameters -] "./scripts/west_commands/zspdx/writer.py" = [ "E501", # https://docs.astral.sh/ruff/rules/line-too-long "F541", # https://docs.astral.sh/ruff/rules/f-string-missing-placeholders diff --git a/scripts/west_commands/zspdx/walker.py b/scripts/west_commands/zspdx/walker.py index 196c68228fe7..4afda9863478 100644 --- a/scripts/west_commands/zspdx/walker.py +++ b/scripts/west_commands/zspdx/walker.py @@ -3,22 +3,32 @@ # SPDX-License-Identifier: Apache-2.0 import os -import yaml import re +import yaml from west import log -from west.util import west_topdir, WestNotFound +from west.util import WestNotFound, west_topdir +import zspdx.spdxids from zspdx.cmakecache import parseCMakeCacheFile from zspdx.cmakefileapijson import parseReply -from zspdx.datatypes import DocumentConfig, Document, File, PackageConfig, Package, RelationshipDataElementType, RelationshipData, Relationship +from zspdx.datatypes import ( + Document, + DocumentConfig, + File, + Package, + PackageConfig, + Relationship, + RelationshipData, + RelationshipDataElementType, +) from zspdx.getincludes import getCIncludes -import zspdx.spdxids + # WalkerConfig contains configuration data for the Walker. class WalkerConfig: def __init__(self): - super(WalkerConfig, self).__init__() + super().__init__() # prefix for Document namespaces; should not end with "/" self.namespacePrefix = "" @@ -38,7 +48,7 @@ def __init__(self): class Walker: # initialize with WalkerConfig def __init__(self, cfg): - super(Walker, self).__init__() + super().__init__() # configuration - WalkerConfig self.cfg = cfg @@ -231,7 +241,8 @@ def setupZephyrDocument(self, zephyr, modules): try: relativeBaseDir = west_topdir(self.cm.paths_source) except WestNotFound: - log.err(f"cannot find west_topdir for CMake Codemodel sources path {self.cm.paths_source}; bailing") + log.err("cannot find west_topdir for CMake Codemodel sources path " + f"{self.cm.paths_source}; bailing") return False # set up zephyr sources package @@ -278,7 +289,7 @@ def setupZephyrDocument(self, zephyr, modules): module_revision = module.get("revision", None) if not module_name: - log.err(f"cannot find module name in meta file; bailing") + log.err("cannot find module name in meta file; bailing") return False # set up zephyr sources package @@ -342,7 +353,7 @@ def setupModulesDocument(self, modules): module_security = module.get("security", None) if not module_name: - log.err(f"cannot find module name in meta file; bailing") + log.err("cannot find module name in meta file; bailing") return False module_ext_ref = [] @@ -375,7 +386,7 @@ def setupDocuments(self): if not self.setupZephyrDocument(content["zephyr"], content["modules"]): return False except (FileNotFoundError, yaml.YAMLError): - log.err(f"cannot find a valid zephyr_meta.yml required for SPDX generation; bailing") + log.err("cannot find a valid zephyr_meta.yml required for SPDX generation; bailing") return False self.setupAppDocument() @@ -446,7 +457,8 @@ def addBuildFile(self, cfgTarget, pkg): # don't create build File if artifact path points to nonexistent file if not os.path.exists(artifactPath): - log.dbg(f" - target {cfgTarget.name} lists build artifact {artifactPath} but file not found after build; skipping") + log.dbg(f" - target {cfgTarget.name} lists build artifact {artifactPath} " + "but file not found after build; skipping") return None # create build File @@ -454,7 +466,8 @@ def addBuildFile(self, cfgTarget, pkg): bf.abspath = artifactPath bf.relpath = cfgTarget.target.artifacts[0] # can use nameOnDisk b/c it is just the filename w/out directory paths - bf.spdxID = zspdx.spdxids.getUniqueFileID(cfgTarget.target.nameOnDisk, self.docBuild.timesSeen) + bf.spdxID = zspdx.spdxids.getUniqueFileID(cfgTarget.target.nameOnDisk, + self.docBuild.timesSeen) # don't fill hashes / licenses / rlns now, we'll do that after walking # add File to Package @@ -476,7 +489,7 @@ def addBuildFile(self, cfgTarget, pkg): # 2) Package for that target # 3) build File for that target def collectPendingSourceFiles(self, cfgTarget, pkg, bf): - log.dbg(f" - collecting source files and adding to pending queue") + log.dbg(" - collecting source files and adding to pending queue") targetIncludesSet = set() @@ -490,7 +503,8 @@ def collectPendingSourceFiles(self, cfgTarget, pkg, bf): # check whether it even exists if not (os.path.exists(srcAbspath) and os.path.isfile(srcAbspath)): - log.dbg(f" - {srcAbspath} does not exist but is referenced in sources for target {pkg.cfg.name}; skipping") + log.dbg(f" - {srcAbspath} does not exist but is referenced in sources for " + f"target {pkg.cfg.name}; skipping") continue # add it to pending source files queue @@ -543,13 +557,17 @@ def collectPendingSourceFiles(self, cfgTarget, pkg, bf): def collectIncludes(self, cfgTarget, pkg, bf, src): # get the right compile group for this source file if len(cfgTarget.target.compileGroups) < (src.compileGroupIndex + 1): - log.dbg(f" - {cfgTarget.target.name} has compileGroupIndex {src.compileGroupIndex} but only {len(cfgTarget.target.compileGroups)} found; skipping included files search") + log.dbg(f" - {cfgTarget.target.name} has compileGroupIndex {src.compileGroupIndex} " + f"but only {len(cfgTarget.target.compileGroups)} found; " + "skipping included files search") return [] cg = cfgTarget.target.compileGroups[src.compileGroupIndex] # currently only doing C includes if cg.language != "C": - log.dbg(f" - {cfgTarget.target.name} has compile group language {cg.language} but currently only searching includes for C files; skipping included files search") + log.dbg(f" - {cfgTarget.target.name} has compile group language {cg.language} " + "but currently only searching includes for C files; " + "skipping included files search") return [] srcAbspath = src.path @@ -619,7 +637,7 @@ def collectTargetDependencies(self, cfgTargets, cfgTarget, pkg): # walk through pending sources and create corresponding files, # assigning them to the appropriate Document and Package def walkPendingSources(self): - log.dbg(f"walking pending sources") + log.dbg("walking pending sources") # only one package in each doc; get it pkgZephyr = list(self.docZephyr.pkgs.values())[0] @@ -640,14 +658,22 @@ def walkPendingSources(self): pkgZephyr = self.findZephyrPackage(srcAbspath) if pkgBuild: - log.dbg(f" - {srcAbspath}: assigning to build document, package {pkgBuild.cfg.name}") + log.dbg(f" - {srcAbspath}: assigning to build document, " + f"package {pkgBuild.cfg.name}") srcDoc = self.docBuild srcPkg = pkgBuild - elif self.cfg.includeSDK and os.path.commonpath([srcAbspath, pkgSDK.cfg.relativeBaseDir]) == pkgSDK.cfg.relativeBaseDir: + elif ( + self.cfg.includeSDK + and os.path.commonpath([srcAbspath, pkgSDK.cfg.relativeBaseDir]) + == pkgSDK.cfg.relativeBaseDir + ): log.dbg(f" - {srcAbspath}: assigning to sdk document") srcDoc = self.docSDK srcPkg = pkgSDK - elif os.path.commonpath([srcAbspath, pkgApp.cfg.relativeBaseDir]) == pkgApp.cfg.relativeBaseDir: + elif ( + os.path.commonpath([srcAbspath, pkgApp.cfg.relativeBaseDir]) + == pkgApp.cfg.relativeBaseDir + ): log.dbg(f" - {srcAbspath}: assigning to app document") srcDoc = self.docApp srcPkg = pkgApp @@ -718,7 +744,9 @@ def walkRelationships(self): rln.refB = spdxIDB rln.rlnType = rlnData.rlnType rlnsA.append(rln) - log.dbg(f" - adding relationship to {docA.cfg.name}: {rln.refA} {rln.rlnType} {rln.refB}") + log.dbg( + f" - adding relationship to {docA.cfg.name}: {rln.refA} {rln.rlnType} {rln.refB}" + ) # get owner (left side) document and SPDX ID of Relationship for given RelationshipData # returns: doc, spdxID, rlnsArray (for either Document, Package, or File, as applicable) @@ -727,15 +755,24 @@ def getRelationshipLeft(self, rlnData): # find the document for this file abspath, and then the specific file's ID ownerDoc = self.allFileLinks.get(rlnData.ownerFileAbspath, None) if not ownerDoc: - log.dbg(f" - searching for relationship, can't find document with file {rlnData.ownerFileAbspath}; skipping") + log.dbg( + " - searching for relationship, can't find document with file " + f"{rlnData.ownerFileAbspath}; skipping" + ) return None, None, None sf = ownerDoc.fileLinks.get(rlnData.ownerFileAbspath, None) if not sf: - log.dbg(f" - searching for relationship for file {rlnData.ownerFileAbspath} points to document {ownerDoc.cfg.name} but file not found; skipping") + log.dbg( + f" - searching for relationship for file {rlnData.ownerFileAbspath} " + f"points to document {ownerDoc.cfg.name} but file not found; skipping" + ) return None, None, None # found it if not sf.spdxID: - log.dbg(f" - searching for relationship for file {rlnData.ownerFileAbspath} found file, but empty ID; skipping") + log.dbg( + f" - searching for relationship for file {rlnData.ownerFileAbspath} " + "found file, but empty ID; skipping" + ) return None, None, None return ownerDoc, sf.spdxID, sf.rlns elif rlnData.ownerType == RelationshipDataElementType.TARGETNAME: @@ -746,10 +783,16 @@ def getRelationshipLeft(self, rlnData): for pkg in ownerDoc.pkgs.values(): if pkg.cfg.name == rlnData.ownerTargetName: if not pkg.cfg.spdxID: - log.dbg(f" - searching for relationship for target {rlnData.ownerTargetName} found package, but empty ID; skipping") + log.dbg( + " - searching for relationship for target " + f"{rlnData.ownerTargetName} found package, but empty ID; skipping" + ) return None, None, None return ownerDoc, pkg.cfg.spdxID, pkg.rlns - log.dbg(f" - searching for relationship for target {rlnData.ownerTargetName}, target not found in build document; skipping") + log.dbg( + f" - searching for relationship for target {rlnData.ownerTargetName}, " + "target not found in build document; skipping" + ) return None, None, None elif rlnData.ownerType == RelationshipDataElementType.DOCUMENT: # will always be SPDXRef-DOCUMENT @@ -764,15 +807,24 @@ def getRelationshipRight(self, rlnData, docA): # find the document for this file abspath, and then the specific file's ID otherDoc = self.allFileLinks.get(rlnData.otherFileAbspath, None) if not otherDoc: - log.dbg(f" - searching for relationship, can't find document with file {rlnData.otherFileAbspath}; skipping") + log.dbg( + " - searching for relationship, can't find document with file " + f"{rlnData.otherFileAbspath}; skipping" + ) return None bf = otherDoc.fileLinks.get(rlnData.otherFileAbspath, None) if not bf: - log.dbg(f" - searching for relationship for file {rlnData.otherFileAbspath} points to document {otherDoc.cfg.name} but file not found; skipping") + log.dbg( + f" - searching for relationship for file {rlnData.otherFileAbspath} " + f"points to document {otherDoc.cfg.name} but file not found; skipping" + ) return None # found it if not bf.spdxID: - log.dbg(f" - searching for relationship for file {rlnData.otherFileAbspath} found file, but empty ID; skipping") + log.dbg( + f" - searching for relationship for file {rlnData.otherFileAbspath} " + "found file, but empty ID; skipping" + ) return None # figure out whether to append DocumentRef spdxIDB = bf.spdxID @@ -788,14 +840,20 @@ def getRelationshipRight(self, rlnData, docA): for pkg in otherDoc.pkgs.values(): if pkg.cfg.name == rlnData.otherTargetName: if not pkg.cfg.spdxID: - log.dbg(f" - searching for relationship for target {rlnData.otherTargetName} found package, but empty ID; skipping") + log.dbg( + f" - searching for relationship for target {rlnData.otherTargetName}" + " found package, but empty ID; skipping" + ) return None spdxIDB = pkg.cfg.spdxID if otherDoc != docA: spdxIDB = otherDoc.cfg.docRefID + ":" + spdxIDB docA.externalDocuments.add(otherDoc) return spdxIDB - log.dbg(f" - searching for relationship for target {rlnData.otherTargetName}, target not found in build document; skipping") + log.dbg( + f" - searching for relationship for target {rlnData.otherTargetName}, " + "target not found in build document; skipping" + ) return None elif rlnData.otherType == RelationshipDataElementType.PACKAGEID: # will just be the package ID that was passed in From ffed30e1a125ccc43ade08381153252b380a20da Mon Sep 17 00:00:00 2001 From: Pieter De Gendt Date: Sat, 24 May 2025 22:25:16 +0200 Subject: [PATCH 9/9] scripts: west_commands: zspdx: writer: Fix linter issues Fix issues reported by ruff. Signed-off-by: Pieter De Gendt --- .ruff-excludes.toml | 5 ----- scripts/west_commands/zspdx/writer.py | 25 +++++++++++++++---------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/.ruff-excludes.toml b/.ruff-excludes.toml index fd5099faa7e6..930d8c18e681 100644 --- a/.ruff-excludes.toml +++ b/.ruff-excludes.toml @@ -1232,11 +1232,6 @@ "UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes "UP032", # https://docs.astral.sh/ruff/rules/f-string ] -"./scripts/west_commands/zspdx/writer.py" = [ - "E501", # https://docs.astral.sh/ruff/rules/line-too-long - "F541", # https://docs.astral.sh/ruff/rules/f-string-missing-placeholders - "I001", # https://docs.astral.sh/ruff/rules/unsorted-imports -] "./scripts/zephyr_module.py" = [ "E501", # https://docs.astral.sh/ruff/rules/line-too-long "I001", # https://docs.astral.sh/ruff/rules/unsorted-imports diff --git a/scripts/west_commands/zspdx/writer.py b/scripts/west_commands/zspdx/writer.py index d1d77f7a5bb1..8223d0ec1eb9 100644 --- a/scripts/west_commands/zspdx/writer.py +++ b/scripts/west_commands/zspdx/writer.py @@ -2,14 +2,13 @@ # # SPDX-License-Identifier: Apache-2.0 +import re from datetime import datetime from west import log from zspdx.util import getHashes -import re - CPE23TYPE_REGEX = ( r'^cpe:2\.3:[aho\*\-](:(((\?*|\*?)([a-zA-Z0-9\-\._]|(\\[\\\*\?!"#$$%&\'\(\)\+,\/:;<=>@\[\]\^' r"`\{\|}~]))+(\?*|\*?))|[\*\-])){5}(:(([a-zA-Z]{2,3}(-([a-zA-Z]{2}|[0-9]{3}))?)|[\*\-]))(:(((\?*" @@ -26,7 +25,10 @@ def _normalize_spdx_name(name): # 1) f: file handle for SPDX document # 2) rln: Relationship object being described def writeRelationshipSPDX(f, rln): - f.write(f"Relationship: {_normalize_spdx_name(rln.refA)} {rln.rlnType} {_normalize_spdx_name(rln.refB)}\n") + f.write( + f"Relationship: {_normalize_spdx_name(rln.refA)} {rln.rlnType} " + f"{_normalize_spdx_name(rln.refB)}\n" + ) # Output tag-value SPDX 2.3 content for the given File object. # Arguments: @@ -45,7 +47,7 @@ def writeFileSPDX(f, bf): f.write(f"FileChecksum: MD5: {bf.md5}\n") f.write(f"LicenseConcluded: {bf.concludedLicense}\n") if len(bf.licenseInfoInFile) == 0: - f.write(f"LicenseInfoInFile: NONE\n") + f.write("LicenseInfoInFile: NONE\n") else: for licInfoInFile in bf.licenseInfoInFile: f.write(f"LicenseInfoInFile: {licInfoInFile}\n") @@ -111,10 +113,10 @@ def writePackageSPDX(f, pkg): for licFromFiles in pkg.licenseInfoFromFiles: f.write(f"PackageLicenseInfoFromFiles: {licFromFiles}\n") else: - f.write(f"PackageLicenseInfoFromFiles: NOASSERTION\n") + f.write("PackageLicenseInfoFromFiles: NOASSERTION\n") f.write(f"FilesAnalyzed: true\nPackageVerificationCode: {pkg.verificationCode}\n\n") else: - f.write(f"FilesAnalyzed: false\nPackageComment: Utility target; no files\n\n") + f.write("FilesAnalyzed: false\nPackageComment: Utility target; no files\n\n") # write package relationships if len(pkg.rlns) > 0: @@ -162,14 +164,17 @@ def writeDocumentSPDX(f, doc): extDocs = list(doc.externalDocuments) extDocs.sort(key = lambda x: x.cfg.docRefID) for extDoc in extDocs: - f.write(f"ExternalDocumentRef: {extDoc.cfg.docRefID} {extDoc.cfg.namespace} SHA1: {extDoc.myDocSHA1}\n") - f.write(f"\n") + f.write( + f"ExternalDocumentRef: {extDoc.cfg.docRefID} {extDoc.cfg.namespace} " + f"SHA1: {extDoc.myDocSHA1}\n" + ) + f.write("\n") # write relationships owned by this Document (not by its Packages, etc.), if any if len(doc.relationships) > 0: for rln in doc.relationships: writeRelationshipSPDX(f, rln) - f.write(f"\n") + f.write("\n") # write packages for pkg in doc.pkgs.values(): @@ -198,7 +203,7 @@ def writeSPDX(spdxPath, doc): # calculate hash of the document we just wrote hashes = getHashes(spdxPath) if not hashes: - log.err(f"Error: created document but unable to calculate hash values") + log.err("Error: created document but unable to calculate hash values") return False doc.myDocSHA1 = hashes[0]