123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- import os
- from west import log
- from zspdx.walker import WalkerConfig, Walker
- from zspdx.scanner import ScannerConfig, scanDocument
- from zspdx.writer import writeSPDX
- class SBOMConfig:
- def __init__(self):
- super(SBOMConfig, self).__init__()
-
- self.namespacePrefix = ""
-
- self.buildDir = ""
-
- self.spdxDir = ""
-
- self.analyzeIncludes = False
-
- self.includeSDK = False
- def setupCmakeQuery(build_dir):
-
- cmakeApiDirPath = os.path.join(build_dir, ".cmake", "api", "v1", "query")
- if os.path.exists(cmakeApiDirPath):
- if not os.path.isdir(cmakeApiDirPath):
- log.err(f'cmake api query directory {cmakeApiDirPath} exists and is not a directory')
- return False
-
- else:
-
- os.makedirs(cmakeApiDirPath, exist_ok=False)
-
- queryFilePath = os.path.join(cmakeApiDirPath, "codemodel-v2")
- if os.path.exists(queryFilePath):
- if not os.path.isfile(queryFilePath):
- log.err(f'cmake api query file {queryFilePath} exists and is not a directory')
- return False
-
- return True
- else:
-
- os.mknod(queryFilePath)
- return True
- def makeSPDX(cfg):
-
- 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")
-
- walkerCfg = WalkerConfig()
- walkerCfg.namespacePrefix = cfg.namespacePrefix
- walkerCfg.buildDir = cfg.buildDir
- walkerCfg.analyzeIncludes = cfg.analyzeIncludes
- walkerCfg.includeSDK = cfg.includeSDK
-
- w = Walker(walkerCfg)
- retval = w.makeDocuments()
- if not retval:
- log.err("SPDX walker failed; bailing")
- return False
-
- scannerCfg = ScannerConfig()
-
- if cfg.includeSDK:
- scanDocument(scannerCfg, w.docSDK)
- scanDocument(scannerCfg, w.docApp)
- scanDocument(scannerCfg, w.docZephyr)
- scanDocument(scannerCfg, w.docBuild)
-
-
-
- if cfg.includeSDK:
- retval = writeSPDX(os.path.join(cfg.spdxDir, "sdk.spdx"), w.docSDK)
- if not retval:
- log.err("SPDX writer failed for SDK document; bailing")
- return False
-
- retval = writeSPDX(os.path.join(cfg.spdxDir, "app.spdx"), w.docApp)
- if not retval:
- log.err("SPDX writer failed for app document; bailing")
- return False
-
- writeSPDX(os.path.join(cfg.spdxDir, "zephyr.spdx"), w.docZephyr)
- if not retval:
- log.err("SPDX writer failed for zephyr document; bailing")
- return False
-
- writeSPDX(os.path.join(cfg.spdxDir, "build.spdx"), w.docBuild)
- if not retval:
- log.err("SPDX writer failed for build document; bailing")
- return False
- return True
|