3
3
# SPDX-License-Identifier: Apache-2.0
4
4
5
5
import os
6
- import yaml
7
6
import re
8
7
8
+ import yaml
9
9
from west import log
10
- from west .util import west_topdir , WestNotFound
10
+ from west .util import WestNotFound , west_topdir
11
11
12
+ import zspdx .spdxids
12
13
from zspdx .cmakecache import parseCMakeCacheFile
13
14
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
+ )
15
25
from zspdx .getincludes import getCIncludes
16
- import zspdx . spdxids
26
+
17
27
18
28
# WalkerConfig contains configuration data for the Walker.
19
29
class WalkerConfig :
20
30
def __init__ (self ):
21
- super (WalkerConfig , self ).__init__ ()
31
+ super ().__init__ ()
22
32
23
33
# prefix for Document namespaces; should not end with "/"
24
34
self .namespacePrefix = ""
@@ -38,7 +48,7 @@ def __init__(self):
38
48
class Walker :
39
49
# initialize with WalkerConfig
40
50
def __init__ (self , cfg ):
41
- super (Walker , self ).__init__ ()
51
+ super ().__init__ ()
42
52
43
53
# configuration - WalkerConfig
44
54
self .cfg = cfg
@@ -231,7 +241,8 @@ def setupZephyrDocument(self, zephyr, modules):
231
241
try :
232
242
relativeBaseDir = west_topdir (self .cm .paths_source )
233
243
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" )
235
246
return False
236
247
237
248
# set up zephyr sources package
@@ -278,7 +289,7 @@ def setupZephyrDocument(self, zephyr, modules):
278
289
module_revision = module .get ("revision" , None )
279
290
280
291
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" )
282
293
return False
283
294
284
295
# set up zephyr sources package
@@ -342,7 +353,7 @@ def setupModulesDocument(self, modules):
342
353
module_security = module .get ("security" , None )
343
354
344
355
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" )
346
357
return False
347
358
348
359
module_ext_ref = []
@@ -375,7 +386,7 @@ def setupDocuments(self):
375
386
if not self .setupZephyrDocument (content ["zephyr" ], content ["modules" ]):
376
387
return False
377
388
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" )
379
390
return False
380
391
381
392
self .setupAppDocument ()
@@ -446,15 +457,17 @@ def addBuildFile(self, cfgTarget, pkg):
446
457
447
458
# don't create build File if artifact path points to nonexistent file
448
459
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" )
450
462
return None
451
463
452
464
# create build File
453
465
bf = File (self .docBuild , pkg )
454
466
bf .abspath = artifactPath
455
467
bf .relpath = cfgTarget .target .artifacts [0 ]
456
468
# 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 )
458
471
# don't fill hashes / licenses / rlns now, we'll do that after walking
459
472
460
473
# add File to Package
@@ -476,7 +489,7 @@ def addBuildFile(self, cfgTarget, pkg):
476
489
# 2) Package for that target
477
490
# 3) build File for that target
478
491
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" )
480
493
481
494
targetIncludesSet = set ()
482
495
@@ -490,7 +503,8 @@ def collectPendingSourceFiles(self, cfgTarget, pkg, bf):
490
503
491
504
# check whether it even exists
492
505
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" )
494
508
continue
495
509
496
510
# add it to pending source files queue
@@ -543,13 +557,17 @@ def collectPendingSourceFiles(self, cfgTarget, pkg, bf):
543
557
def collectIncludes (self , cfgTarget , pkg , bf , src ):
544
558
# get the right compile group for this source file
545
559
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" )
547
563
return []
548
564
cg = cfgTarget .target .compileGroups [src .compileGroupIndex ]
549
565
550
566
# currently only doing C includes
551
567
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" )
553
571
return []
554
572
555
573
srcAbspath = src .path
@@ -619,7 +637,7 @@ def collectTargetDependencies(self, cfgTargets, cfgTarget, pkg):
619
637
# walk through pending sources and create corresponding files,
620
638
# assigning them to the appropriate Document and Package
621
639
def walkPendingSources (self ):
622
- log .dbg (f "walking pending sources" )
640
+ log .dbg ("walking pending sources" )
623
641
624
642
# only one package in each doc; get it
625
643
pkgZephyr = list (self .docZephyr .pkgs .values ())[0 ]
@@ -640,14 +658,22 @@ def walkPendingSources(self):
640
658
pkgZephyr = self .findZephyrPackage (srcAbspath )
641
659
642
660
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 } " )
644
663
srcDoc = self .docBuild
645
664
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
+ ):
647
670
log .dbg (f" - { srcAbspath } : assigning to sdk document" )
648
671
srcDoc = self .docSDK
649
672
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
+ ):
651
677
log .dbg (f" - { srcAbspath } : assigning to app document" )
652
678
srcDoc = self .docApp
653
679
srcPkg = pkgApp
@@ -718,7 +744,9 @@ def walkRelationships(self):
718
744
rln .refB = spdxIDB
719
745
rln .rlnType = rlnData .rlnType
720
746
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
+ )
722
750
723
751
# get owner (left side) document and SPDX ID of Relationship for given RelationshipData
724
752
# returns: doc, spdxID, rlnsArray (for either Document, Package, or File, as applicable)
@@ -727,15 +755,24 @@ def getRelationshipLeft(self, rlnData):
727
755
# find the document for this file abspath, and then the specific file's ID
728
756
ownerDoc = self .allFileLinks .get (rlnData .ownerFileAbspath , None )
729
757
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
+ )
731
762
return None , None , None
732
763
sf = ownerDoc .fileLinks .get (rlnData .ownerFileAbspath , None )
733
764
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
+ )
735
769
return None , None , None
736
770
# found it
737
771
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
+ )
739
776
return None , None , None
740
777
return ownerDoc , sf .spdxID , sf .rlns
741
778
elif rlnData .ownerType == RelationshipDataElementType .TARGETNAME :
@@ -746,10 +783,16 @@ def getRelationshipLeft(self, rlnData):
746
783
for pkg in ownerDoc .pkgs .values ():
747
784
if pkg .cfg .name == rlnData .ownerTargetName :
748
785
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
+ )
750
790
return None , None , None
751
791
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
+ )
753
796
return None , None , None
754
797
elif rlnData .ownerType == RelationshipDataElementType .DOCUMENT :
755
798
# will always be SPDXRef-DOCUMENT
@@ -764,15 +807,24 @@ def getRelationshipRight(self, rlnData, docA):
764
807
# find the document for this file abspath, and then the specific file's ID
765
808
otherDoc = self .allFileLinks .get (rlnData .otherFileAbspath , None )
766
809
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
+ )
768
814
return None
769
815
bf = otherDoc .fileLinks .get (rlnData .otherFileAbspath , None )
770
816
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
+ )
772
821
return None
773
822
# found it
774
823
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
+ )
776
828
return None
777
829
# figure out whether to append DocumentRef
778
830
spdxIDB = bf .spdxID
@@ -788,14 +840,20 @@ def getRelationshipRight(self, rlnData, docA):
788
840
for pkg in otherDoc .pkgs .values ():
789
841
if pkg .cfg .name == rlnData .otherTargetName :
790
842
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
+ )
792
847
return None
793
848
spdxIDB = pkg .cfg .spdxID
794
849
if otherDoc != docA :
795
850
spdxIDB = otherDoc .cfg .docRefID + ":" + spdxIDB
796
851
docA .externalDocuments .add (otherDoc )
797
852
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
+ )
799
857
return None
800
858
elif rlnData .otherType == RelationshipDataElementType .PACKAGEID :
801
859
# will just be the package ID that was passed in
0 commit comments