FOSSology  4.5.1
Open Source License Compliance by Open Source Software
CliOptions.py
1 #!/usr/bin/env python3
2 
3 # SPDX-FileCopyrightText: © 2023 Siemens AG
4 # SPDX-FileContributor: Gaurav Mishra <mishra.gaurav@siemens.com>
5 
6 # SPDX-License-Identifier: GPL-2.0-only
7 
8 import os
9 from argparse import Namespace
10 from enum import Enum
11 
12 
13 class ReportFormat(Enum):
14  """
15  Report formats supported by the script.
16  """
17  TEXT = 0
18  SPDX_JSON = 1
19  SPDX_RDF = 2
20  SPDX_TAG_VALUE = 3
21  SPDX_YAML = 4
22 
23 
24 class CliOptions(object):
25  """
26  Hold the various shared flags and data
27 
28  :ivar nomos: run nomos scanner
29  :ivar ojo: run ojo scanner
30  :ivar copyright: run copyright scanner
31  :ivar keyword: run keyword scanner
32  :ivar repo: scan whole repo or just diff
33  :ivar differential: scan between two versions of a repo
34  :ivar scan_dir: Scan a particular subdirectory
35  :ivar tags: tuple of length 2: (from_tag , to_tag) to scan
36  :ivar diff_dir: directory to scan
37  :ivar dir_path: Path to subdirectory to scan
38  :ivar keyword_conf_file_path: path to custom keyword.conf file passed by user
39  :ivar allowlist_path: path to allowlist.json file
40  :ivar allowlist: information from allowlist.json
41  :ivar report_format: Report format to use
42  :ivar scan_only_deps: Scan only dependencies
43  :ivar sbom_path: Path to sbom file
44  :ivar parser: Parser instance to hold list of parsed components
45  """
46  nomos: bool = False
47  ojo: bool = False
48  copyright: bool = False
49  keyword: bool = False
50  repo: bool = False
51  differential: bool = False
52  scan_dir: bool = False
53  tags: tuple = ('', '')
54  diff_dir: str = os.getcwd()
55  dir_path: str = ''
56  keyword_conf_file_path: str = ''
57  allowlist_path: str = None
58  allowlist: dict[str, list[str]] = {
59  'licenses': [],
60  'exclude': []
61  }
62  report_format: ReportFormat = ReportFormat.TEXT
63  scan_only_deps: bool = False
64  sbom_path: str = ''
65  parser = None
66 
67  def update_args(self, args: Namespace):
68  """
69  Update options based on argsparse values.
70 
71  :param args: Argparse from cli
72  """
73  # Convert args.operation to a set for efficient lookups
74  operations = set(args.operation) if hasattr(args, 'operation') else set()
75 
76  self.nomosnomos = 'nomos' in operations
77  self.copyrightcopyright = 'copyright' in operations
78  self.keywordkeyword = 'keyword' in operations
79  self.ojoojo = 'ojo' in operations
80 
81  if 'repo' in operations and 'differential' in operations:
82  raise ValueError(
83  "You can only specify either 'repo' or 'differential' scans at a time."
84  )
85 
86  self.reporepo = 'repo' in operations
87  self.differentialdifferential = 'differential' in operations
88  self.scan_only_depsscan_only_deps = 'scan-only-deps' in operations
89  self.scan_dirscan_dir = 'scan-dir' in operations
90 
91  if self.scan_dirscan_dir and args.dir_path != '':
92  self.dir_pathdir_path = args.dir_path
93  if args.tags is not None and self.differentialdifferential and len(args.tags) == 2:
94  self.tagstags = (args.tags[0], args.tags[1])
95  if args.allowlist_path:
96  self.allowlist_pathallowlist_path = args.allowlist_path
97 
98  # If no specific scanner is selected, enable all by default.
99  if not (self.nomosnomos or self.ojoojo or self.copyrightcopyright or self.keywordkeyword):
100  self.nomosnomos = self.ojoojo = self.copyrightcopyright = self.keywordkeyword = True
101 
102  self.report_formatreport_format = ReportFormat[args.report]
103  if self.keywordkeyword and args.keyword_conf:
104  self.keyword_conf_file_pathkeyword_conf_file_path = args.keyword_conf
105  if (self.scan_only_depsscan_only_deps or self.reporepo) and args.sbom_path:
106  self.sbom_pathsbom_path = args.sbom_path
def update_args(self, Namespace args)
Definition: CliOptions.py:67