|
6 | 6 | import uuid |
7 | 7 |
|
8 | 8 | from west.commands import WestCommand |
9 | | -from west import log |
10 | 9 |
|
11 | 10 | from zspdx.sbom import SBOMConfig, makeSPDX, setupCmakeQuery |
12 | 11 |
|
@@ -50,65 +49,65 @@ def do_add_parser(self, parser_adder): |
50 | 49 | return parser |
51 | 50 |
|
52 | 51 | def do_run(self, args, unknown_args): |
53 | | - log.dbg(f"running zephyr SPDX generator") |
| 52 | + self.dbg(f"running zephyr SPDX generator") |
54 | 53 |
|
55 | | - log.dbg(f" --init is", args.init) |
56 | | - log.dbg(f" --build-dir is", args.build_dir) |
57 | | - log.dbg(f" --namespace-prefix is", args.namespace_prefix) |
58 | | - log.dbg(f" --spdx-dir is", args.spdx_dir) |
59 | | - log.dbg(f" --analyze-includes is", args.analyze_includes) |
60 | | - log.dbg(f" --include-sdk is", args.include_sdk) |
| 54 | + self.dbg(f" --init is", args.init) |
| 55 | + self.dbg(f" --build-dir is", args.build_dir) |
| 56 | + self.dbg(f" --namespace-prefix is", args.namespace_prefix) |
| 57 | + self.dbg(f" --spdx-dir is", args.spdx_dir) |
| 58 | + self.dbg(f" --analyze-includes is", args.analyze_includes) |
| 59 | + self.dbg(f" --include-sdk is", args.include_sdk) |
61 | 60 |
|
62 | 61 | if args.init: |
63 | | - do_run_init(args) |
| 62 | + self.do_run_init(args) |
64 | 63 | else: |
65 | | - do_run_spdx(args) |
66 | | - |
67 | | -def do_run_init(args): |
68 | | - log.inf("initializing Cmake file-based API prior to build") |
69 | | - |
70 | | - if not args.build_dir: |
71 | | - log.die("Build directory not specified; call `west spdx --init --build-dir=BUILD_DIR`") |
72 | | - |
73 | | - # initialize CMake file-based API - empty query file |
74 | | - query_ready = setupCmakeQuery(args.build_dir) |
75 | | - if query_ready: |
76 | | - log.inf("initialized; run `west build` then run `west spdx`") |
77 | | - else: |
78 | | - log.err("Couldn't create Cmake file-based API query directory") |
79 | | - log.err("You can manually create an empty file at $BUILDDIR/.cmake/api/v1/query/codemodel-v2") |
80 | | - |
81 | | -def do_run_spdx(args): |
82 | | - if not args.build_dir: |
83 | | - log.die("Build directory not specified; call `west spdx --build-dir=BUILD_DIR`") |
84 | | - |
85 | | - # create the SPDX files |
86 | | - cfg = SBOMConfig() |
87 | | - cfg.buildDir = args.build_dir |
88 | | - if args.namespace_prefix: |
89 | | - cfg.namespacePrefix = args.namespace_prefix |
90 | | - else: |
91 | | - # create default namespace according to SPDX spec |
92 | | - # note that this is intentionally _not_ an actual URL where |
93 | | - # this document will be stored |
94 | | - cfg.namespacePrefix = f"http://spdx.org/spdxdocs/zephyr-{str(uuid.uuid4())}" |
95 | | - if args.spdx_dir: |
96 | | - cfg.spdxDir = args.spdx_dir |
97 | | - else: |
98 | | - cfg.spdxDir = os.path.join(args.build_dir, "spdx") |
99 | | - if args.analyze_includes: |
100 | | - cfg.analyzeIncludes = True |
101 | | - if args.include_sdk: |
102 | | - cfg.includeSDK = True |
103 | | - |
104 | | - # make sure SPDX directory exists, or create it if it doesn't |
105 | | - if os.path.exists(cfg.spdxDir): |
106 | | - if not os.path.isdir(cfg.spdxDir): |
107 | | - log.err(f'SPDX output directory {cfg.spdxDir} exists but is not a directory') |
108 | | - return |
109 | | - # directory exists, we're good |
110 | | - else: |
111 | | - # create the directory |
112 | | - os.makedirs(cfg.spdxDir, exist_ok=False) |
113 | | - |
114 | | - makeSPDX(cfg) |
| 64 | + self.do_run_spdx(args) |
| 65 | + |
| 66 | + def do_run_init(self, args): |
| 67 | + self.inf("initializing Cmake file-based API prior to build") |
| 68 | + |
| 69 | + if not args.build_dir: |
| 70 | + self.die("Build directory not specified; call `west spdx --init --build-dir=BUILD_DIR`") |
| 71 | + |
| 72 | + # initialize CMake file-based API - empty query file |
| 73 | + query_ready = setupCmakeQuery(args.build_dir) |
| 74 | + if query_ready: |
| 75 | + self.inf("initialized; run `west build` then run `west spdx`") |
| 76 | + else: |
| 77 | + self.err("Couldn't create Cmake file-based API query directory") |
| 78 | + self.err("You can manually create an empty file at $BUILDDIR/.cmake/api/v1/query/codemodel-v2") |
| 79 | + |
| 80 | + def do_run_spdx(self, args): |
| 81 | + if not args.build_dir: |
| 82 | + self.die("Build directory not specified; call `west spdx --build-dir=BUILD_DIR`") |
| 83 | + |
| 84 | + # create the SPDX files |
| 85 | + cfg = SBOMConfig() |
| 86 | + cfg.buildDir = args.build_dir |
| 87 | + if args.namespace_prefix: |
| 88 | + cfg.namespacePrefix = args.namespace_prefix |
| 89 | + else: |
| 90 | + # create default namespace according to SPDX spec |
| 91 | + # note that this is intentionally _not_ an actual URL where |
| 92 | + # this document will be stored |
| 93 | + cfg.namespacePrefix = f"http://spdx.org/spdxdocs/zephyr-{str(uuid.uuid4())}" |
| 94 | + if args.spdx_dir: |
| 95 | + cfg.spdxDir = args.spdx_dir |
| 96 | + else: |
| 97 | + cfg.spdxDir = os.path.join(args.build_dir, "spdx") |
| 98 | + if args.analyze_includes: |
| 99 | + cfg.analyzeIncludes = True |
| 100 | + if args.include_sdk: |
| 101 | + cfg.includeSDK = True |
| 102 | + |
| 103 | + # make sure SPDX directory exists, or create it if it doesn't |
| 104 | + if os.path.exists(cfg.spdxDir): |
| 105 | + if not os.path.isdir(cfg.spdxDir): |
| 106 | + self.err(f'SPDX output directory {cfg.spdxDir} exists but is not a directory') |
| 107 | + return |
| 108 | + # directory exists, we're good |
| 109 | + else: |
| 110 | + # create the directory |
| 111 | + os.makedirs(cfg.spdxDir, exist_ok=False) |
| 112 | + |
| 113 | + makeSPDX(cfg) |
0 commit comments