Skip to content

Commit cbff6aa

Browse files
committed
scripts: west_commands: zspdx: walker: Fix linter issues
Fix issues reported by ruff. Signed-off-by: Pieter De Gendt <pieter.degendt@basalte.be>
1 parent 50afe1b commit cbff6aa

File tree

2 files changed

+89
-37
lines changed

2 files changed

+89
-37
lines changed

.ruff-excludes.toml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,12 +1232,6 @@
12321232
"UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes
12331233
"UP032", # https://docs.astral.sh/ruff/rules/f-string
12341234
]
1235-
"./scripts/west_commands/zspdx/walker.py" = [
1236-
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
1237-
"F541", # https://docs.astral.sh/ruff/rules/f-string-missing-placeholders
1238-
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
1239-
"UP008", # https://docs.astral.sh/ruff/rules/super-call-with-parameters
1240-
]
12411235
"./scripts/west_commands/zspdx/writer.py" = [
12421236
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
12431237
"F541", # https://docs.astral.sh/ruff/rules/f-string-missing-placeholders

scripts/west_commands/zspdx/walker.py

Lines changed: 89 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,32 @@
33
# SPDX-License-Identifier: Apache-2.0
44

55
import os
6-
import yaml
76
import re
87

8+
import yaml
99
from west import log
10-
from west.util import west_topdir, WestNotFound
10+
from west.util import WestNotFound, west_topdir
1111

12+
import zspdx.spdxids
1213
from zspdx.cmakecache import parseCMakeCacheFile
1314
from zspdx.cmakefileapijson import parseReply
14-
from zspdx.datatypes import DocumentConfig, Document, File, PackageConfig, Package, RelationshipDataElementType, RelationshipData, Relationship
15+
from zspdx.datatypes import (
16+
Document,
17+
DocumentConfig,
18+
File,
19+
Package,
20+
PackageConfig,
21+
Relationship,
22+
RelationshipData,
23+
RelationshipDataElementType,
24+
)
1525
from zspdx.getincludes import getCIncludes
16-
import zspdx.spdxids
26+
1727

1828
# WalkerConfig contains configuration data for the Walker.
1929
class WalkerConfig:
2030
def __init__(self):
21-
super(WalkerConfig, self).__init__()
31+
super().__init__()
2232

2333
# prefix for Document namespaces; should not end with "/"
2434
self.namespacePrefix = ""
@@ -38,7 +48,7 @@ def __init__(self):
3848
class Walker:
3949
# initialize with WalkerConfig
4050
def __init__(self, cfg):
41-
super(Walker, self).__init__()
51+
super().__init__()
4252

4353
# configuration - WalkerConfig
4454
self.cfg = cfg
@@ -231,7 +241,8 @@ def setupZephyrDocument(self, zephyr, modules):
231241
try:
232242
relativeBaseDir = west_topdir(self.cm.paths_source)
233243
except WestNotFound:
234-
log.err(f"cannot find west_topdir for CMake Codemodel sources path {self.cm.paths_source}; bailing")
244+
log.err("cannot find west_topdir for CMake Codemodel sources path "
245+
f"{self.cm.paths_source}; bailing")
235246
return False
236247

237248
# set up zephyr sources package
@@ -278,7 +289,7 @@ def setupZephyrDocument(self, zephyr, modules):
278289
module_revision = module.get("revision", None)
279290

280291
if not module_name:
281-
log.err(f"cannot find module name in meta file; bailing")
292+
log.err("cannot find module name in meta file; bailing")
282293
return False
283294

284295
# set up zephyr sources package
@@ -342,7 +353,7 @@ def setupModulesDocument(self, modules):
342353
module_security = module.get("security", None)
343354

344355
if not module_name:
345-
log.err(f"cannot find module name in meta file; bailing")
356+
log.err("cannot find module name in meta file; bailing")
346357
return False
347358

348359
module_ext_ref = []
@@ -375,7 +386,7 @@ def setupDocuments(self):
375386
if not self.setupZephyrDocument(content["zephyr"], content["modules"]):
376387
return False
377388
except (FileNotFoundError, yaml.YAMLError):
378-
log.err(f"cannot find a valid zephyr_meta.yml required for SPDX generation; bailing")
389+
log.err("cannot find a valid zephyr_meta.yml required for SPDX generation; bailing")
379390
return False
380391

381392
self.setupAppDocument()
@@ -446,15 +457,17 @@ def addBuildFile(self, cfgTarget, pkg):
446457

447458
# don't create build File if artifact path points to nonexistent file
448459
if not os.path.exists(artifactPath):
449-
log.dbg(f" - target {cfgTarget.name} lists build artifact {artifactPath} but file not found after build; skipping")
460+
log.dbg(f" - target {cfgTarget.name} lists build artifact {artifactPath} "
461+
"but file not found after build; skipping")
450462
return None
451463

452464
# create build File
453465
bf = File(self.docBuild, pkg)
454466
bf.abspath = artifactPath
455467
bf.relpath = cfgTarget.target.artifacts[0]
456468
# can use nameOnDisk b/c it is just the filename w/out directory paths
457-
bf.spdxID = zspdx.spdxids.getUniqueFileID(cfgTarget.target.nameOnDisk, self.docBuild.timesSeen)
469+
bf.spdxID = zspdx.spdxids.getUniqueFileID(cfgTarget.target.nameOnDisk,
470+
self.docBuild.timesSeen)
458471
# don't fill hashes / licenses / rlns now, we'll do that after walking
459472

460473
# add File to Package
@@ -476,7 +489,7 @@ def addBuildFile(self, cfgTarget, pkg):
476489
# 2) Package for that target
477490
# 3) build File for that target
478491
def collectPendingSourceFiles(self, cfgTarget, pkg, bf):
479-
log.dbg(f" - collecting source files and adding to pending queue")
492+
log.dbg(" - collecting source files and adding to pending queue")
480493

481494
targetIncludesSet = set()
482495

@@ -490,7 +503,8 @@ def collectPendingSourceFiles(self, cfgTarget, pkg, bf):
490503

491504
# check whether it even exists
492505
if not (os.path.exists(srcAbspath) and os.path.isfile(srcAbspath)):
493-
log.dbg(f" - {srcAbspath} does not exist but is referenced in sources for target {pkg.cfg.name}; skipping")
506+
log.dbg(f" - {srcAbspath} does not exist but is referenced in sources for "
507+
f"target {pkg.cfg.name}; skipping")
494508
continue
495509

496510
# add it to pending source files queue
@@ -543,13 +557,17 @@ def collectPendingSourceFiles(self, cfgTarget, pkg, bf):
543557
def collectIncludes(self, cfgTarget, pkg, bf, src):
544558
# get the right compile group for this source file
545559
if len(cfgTarget.target.compileGroups) < (src.compileGroupIndex + 1):
546-
log.dbg(f" - {cfgTarget.target.name} has compileGroupIndex {src.compileGroupIndex} but only {len(cfgTarget.target.compileGroups)} found; skipping included files search")
560+
log.dbg(f" - {cfgTarget.target.name} has compileGroupIndex {src.compileGroupIndex} "
561+
f"but only {len(cfgTarget.target.compileGroups)} found; "
562+
"skipping included files search")
547563
return []
548564
cg = cfgTarget.target.compileGroups[src.compileGroupIndex]
549565

550566
# currently only doing C includes
551567
if cg.language != "C":
552-
log.dbg(f" - {cfgTarget.target.name} has compile group language {cg.language} but currently only searching includes for C files; skipping included files search")
568+
log.dbg(f" - {cfgTarget.target.name} has compile group language {cg.language} "
569+
"but currently only searching includes for C files; "
570+
"skipping included files search")
553571
return []
554572

555573
srcAbspath = src.path
@@ -619,7 +637,7 @@ def collectTargetDependencies(self, cfgTargets, cfgTarget, pkg):
619637
# walk through pending sources and create corresponding files,
620638
# assigning them to the appropriate Document and Package
621639
def walkPendingSources(self):
622-
log.dbg(f"walking pending sources")
640+
log.dbg("walking pending sources")
623641

624642
# only one package in each doc; get it
625643
pkgZephyr = list(self.docZephyr.pkgs.values())[0]
@@ -640,14 +658,22 @@ def walkPendingSources(self):
640658
pkgZephyr = self.findZephyrPackage(srcAbspath)
641659

642660
if pkgBuild:
643-
log.dbg(f" - {srcAbspath}: assigning to build document, package {pkgBuild.cfg.name}")
661+
log.dbg(f" - {srcAbspath}: assigning to build document, "
662+
f"package {pkgBuild.cfg.name}")
644663
srcDoc = self.docBuild
645664
srcPkg = pkgBuild
646-
elif self.cfg.includeSDK and os.path.commonpath([srcAbspath, pkgSDK.cfg.relativeBaseDir]) == pkgSDK.cfg.relativeBaseDir:
665+
elif (
666+
self.cfg.includeSDK
667+
and os.path.commonpath([srcAbspath, pkgSDK.cfg.relativeBaseDir])
668+
== pkgSDK.cfg.relativeBaseDir
669+
):
647670
log.dbg(f" - {srcAbspath}: assigning to sdk document")
648671
srcDoc = self.docSDK
649672
srcPkg = pkgSDK
650-
elif os.path.commonpath([srcAbspath, pkgApp.cfg.relativeBaseDir]) == pkgApp.cfg.relativeBaseDir:
673+
elif (
674+
os.path.commonpath([srcAbspath, pkgApp.cfg.relativeBaseDir])
675+
== pkgApp.cfg.relativeBaseDir
676+
):
651677
log.dbg(f" - {srcAbspath}: assigning to app document")
652678
srcDoc = self.docApp
653679
srcPkg = pkgApp
@@ -718,7 +744,9 @@ def walkRelationships(self):
718744
rln.refB = spdxIDB
719745
rln.rlnType = rlnData.rlnType
720746
rlnsA.append(rln)
721-
log.dbg(f" - adding relationship to {docA.cfg.name}: {rln.refA} {rln.rlnType} {rln.refB}")
747+
log.dbg(
748+
f" - adding relationship to {docA.cfg.name}: {rln.refA} {rln.rlnType} {rln.refB}"
749+
)
722750

723751
# get owner (left side) document and SPDX ID of Relationship for given RelationshipData
724752
# returns: doc, spdxID, rlnsArray (for either Document, Package, or File, as applicable)
@@ -727,15 +755,24 @@ def getRelationshipLeft(self, rlnData):
727755
# find the document for this file abspath, and then the specific file's ID
728756
ownerDoc = self.allFileLinks.get(rlnData.ownerFileAbspath, None)
729757
if not ownerDoc:
730-
log.dbg(f" - searching for relationship, can't find document with file {rlnData.ownerFileAbspath}; skipping")
758+
log.dbg(
759+
" - searching for relationship, can't find document with file "
760+
f"{rlnData.ownerFileAbspath}; skipping"
761+
)
731762
return None, None, None
732763
sf = ownerDoc.fileLinks.get(rlnData.ownerFileAbspath, None)
733764
if not sf:
734-
log.dbg(f" - searching for relationship for file {rlnData.ownerFileAbspath} points to document {ownerDoc.cfg.name} but file not found; skipping")
765+
log.dbg(
766+
f" - searching for relationship for file {rlnData.ownerFileAbspath} "
767+
f"points to document {ownerDoc.cfg.name} but file not found; skipping"
768+
)
735769
return None, None, None
736770
# found it
737771
if not sf.spdxID:
738-
log.dbg(f" - searching for relationship for file {rlnData.ownerFileAbspath} found file, but empty ID; skipping")
772+
log.dbg(
773+
f" - searching for relationship for file {rlnData.ownerFileAbspath} "
774+
"found file, but empty ID; skipping"
775+
)
739776
return None, None, None
740777
return ownerDoc, sf.spdxID, sf.rlns
741778
elif rlnData.ownerType == RelationshipDataElementType.TARGETNAME:
@@ -746,10 +783,16 @@ def getRelationshipLeft(self, rlnData):
746783
for pkg in ownerDoc.pkgs.values():
747784
if pkg.cfg.name == rlnData.ownerTargetName:
748785
if not pkg.cfg.spdxID:
749-
log.dbg(f" - searching for relationship for target {rlnData.ownerTargetName} found package, but empty ID; skipping")
786+
log.dbg(
787+
" - searching for relationship for target "
788+
f"{rlnData.ownerTargetName} found package, but empty ID; skipping"
789+
)
750790
return None, None, None
751791
return ownerDoc, pkg.cfg.spdxID, pkg.rlns
752-
log.dbg(f" - searching for relationship for target {rlnData.ownerTargetName}, target not found in build document; skipping")
792+
log.dbg(
793+
f" - searching for relationship for target {rlnData.ownerTargetName}, "
794+
"target not found in build document; skipping"
795+
)
753796
return None, None, None
754797
elif rlnData.ownerType == RelationshipDataElementType.DOCUMENT:
755798
# will always be SPDXRef-DOCUMENT
@@ -764,15 +807,24 @@ def getRelationshipRight(self, rlnData, docA):
764807
# find the document for this file abspath, and then the specific file's ID
765808
otherDoc = self.allFileLinks.get(rlnData.otherFileAbspath, None)
766809
if not otherDoc:
767-
log.dbg(f" - searching for relationship, can't find document with file {rlnData.otherFileAbspath}; skipping")
810+
log.dbg(
811+
" - searching for relationship, can't find document with file "
812+
f"{rlnData.otherFileAbspath}; skipping"
813+
)
768814
return None
769815
bf = otherDoc.fileLinks.get(rlnData.otherFileAbspath, None)
770816
if not bf:
771-
log.dbg(f" - searching for relationship for file {rlnData.otherFileAbspath} points to document {otherDoc.cfg.name} but file not found; skipping")
817+
log.dbg(
818+
f" - searching for relationship for file {rlnData.otherFileAbspath} "
819+
f"points to document {otherDoc.cfg.name} but file not found; skipping"
820+
)
772821
return None
773822
# found it
774823
if not bf.spdxID:
775-
log.dbg(f" - searching for relationship for file {rlnData.otherFileAbspath} found file, but empty ID; skipping")
824+
log.dbg(
825+
f" - searching for relationship for file {rlnData.otherFileAbspath} "
826+
"found file, but empty ID; skipping"
827+
)
776828
return None
777829
# figure out whether to append DocumentRef
778830
spdxIDB = bf.spdxID
@@ -788,14 +840,20 @@ def getRelationshipRight(self, rlnData, docA):
788840
for pkg in otherDoc.pkgs.values():
789841
if pkg.cfg.name == rlnData.otherTargetName:
790842
if not pkg.cfg.spdxID:
791-
log.dbg(f" - searching for relationship for target {rlnData.otherTargetName} found package, but empty ID; skipping")
843+
log.dbg(
844+
f" - searching for relationship for target {rlnData.otherTargetName}"
845+
" found package, but empty ID; skipping"
846+
)
792847
return None
793848
spdxIDB = pkg.cfg.spdxID
794849
if otherDoc != docA:
795850
spdxIDB = otherDoc.cfg.docRefID + ":" + spdxIDB
796851
docA.externalDocuments.add(otherDoc)
797852
return spdxIDB
798-
log.dbg(f" - searching for relationship for target {rlnData.otherTargetName}, target not found in build document; skipping")
853+
log.dbg(
854+
f" - searching for relationship for target {rlnData.otherTargetName}, "
855+
"target not found in build document; skipping"
856+
)
799857
return None
800858
elif rlnData.otherType == RelationshipDataElementType.PACKAGEID:
801859
# will just be the package ID that was passed in

0 commit comments

Comments
 (0)