FOSSology  4.7.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  SPDX3_JSON = 5
23  SPDX3_TTL = 6
24  SPDX3_RDF = 7
25 
26 
27 class CliOptions(object):
28  """
29  Hold the various shared flags and data
30 
31  :ivar nomos: run nomos scanner
32  :ivar ojo: run ojo scanner
33  :ivar copyright: run copyright scanner
34  :ivar keyword: run keyword scanner
35  :ivar repo: scan whole repo or just diff
36  :ivar differential: scan between two versions of a repo
37  :ivar scan_dir: Scan a particular subdirectory
38  :ivar tags: tuple of length 2: (from_tag , to_tag) to scan
39  :ivar diff_dir: directory to scan
40  :ivar dir_path: Path to subdirectory to scan
41  :ivar keyword_conf_file_path: path to custom keyword.conf file passed by user
42  :ivar allowlist_path: path to allowlist.json file
43  :ivar allowlist: information from allowlist.json
44  :ivar report_format: Report format to use
45  :ivar scan_only_deps: Scan only dependencies
46  :ivar sbom_path: Path to sbom file
47  :ivar parser: Parser instance to hold list of parsed components
48  """
49  nomos: bool = False
50  ojo: bool = False
51  copyright: bool = False
52  keyword: bool = False
53  repo: bool = False
54  differential: bool = False
55  scan_dir: bool = False
56  tags: tuple = ('', '')
57  diff_dir: str = os.getcwd()
58  dir_path: str = ''
59  keyword_conf_file_path: str = ''
60  allowlist_path: str = None
61  allowlist: dict[str, list[str]] = {
62  'licenses': [],
63  'exclude': []
64  }
65  report_format: ReportFormat = ReportFormat.TEXT
66  scan_only_deps: bool = False
67  sbom_path: str = ''
68  parser = None
69 
70  def update_args(self, args: Namespace):
71  """
72  Update options based on argsparse values.
73 
74  :param args: Argparse from cli
75  """
76  # Convert args.operation to a set for efficient lookups
77  operations = set(args.operation) if hasattr(args, 'operation') else set()
78 
79  self.nomosnomos = 'nomos' in operations
80  self.copyrightcopyright = 'copyright' in operations
81  self.keywordkeyword = 'keyword' in operations
82  self.ojoojo = 'ojo' in operations
83 
84  if 'repo' in operations and 'differential' in operations:
85  raise ValueError(
86  "You can only specify either 'repo' or 'differential' scans at a time."
87  )
88 
89  self.reporepo = 'repo' in operations
90  self.differentialdifferential = 'differential' in operations
91  self.scan_only_depsscan_only_deps = 'scan-only-deps' in operations
92  self.scan_dirscan_dir = 'scan-dir' in operations
93 
94  if self.scan_dirscan_dir and args.dir_path != '':
95  self.dir_pathdir_path = args.dir_path
96  if args.tags is not None and self.differentialdifferential and len(args.tags) == 2:
97  self.tagstags = (args.tags[0], args.tags[1])
98  if args.allowlist_path:
99  self.allowlist_pathallowlist_path = args.allowlist_path
100 
101  # If no specific scanner is selected, enable all by default.
102  if not (self.nomosnomos or self.ojoojo or self.copyrightcopyright or self.keywordkeyword):
103  self.nomosnomos = self.ojoojo = self.copyrightcopyright = self.keywordkeyword = True
104 
105  self.report_formatreport_format = ReportFormat[args.report]
106  if self.keywordkeyword and args.keyword_conf:
107  self.keyword_conf_file_pathkeyword_conf_file_path = args.keyword_conf
108  if (self.scan_only_depsscan_only_deps or self.reporepo) and args.sbom_path:
109  self.sbom_pathsbom_path = args.sbom_path
def update_args(self, Namespace args)
Definition: CliOptions.py:70