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  """
45  nomos: bool = False
46  ojo: bool = False
47  copyright: bool = False
48  keyword: bool = False
49  repo: bool = False
50  differential: bool = False
51  scan_dir: bool = False
52  tags: tuple = ('','')
53  diff_dir: str = os.getcwd()
54  dir_path: str = ''
55  keyword_conf_file_path : str = ''
56  allowlist_path: str = None
57  allowlist: dict[str, list[str]] = {
58  'licenses': [],
59  'exclude': []
60  }
61  report_format: ReportFormat = ReportFormat.TEXT
62  scan_only_deps: bool = False
63  sbom_path : str = ''
64 
65  def update_args(self, args: Namespace):
66  """
67  Update options based on argsparse values.
68 
69  :param args: Argparse from cli
70  """
71  if "nomos" in args.operation:
72  self.nomosnomos = True
73  if "copyright" in args.operation:
74  self.copyrightcopyright = True
75  if "keyword" in args.operation:
76  self.keywordkeyword = True
77  if "ojo" in args.operation:
78  self.ojoojo = True
79  if 'repo' in args.operation and 'differential' in args.operation:
80  raise ValueError("You can only specify either 'repo' or 'differential' scans at a time.")
81  if "repo" in args.operation:
82  self.reporepo = True
83  if "differential" in args.operation:
84  self.differentialdifferential = True
85  if 'scan-only-deps' in args.operation:
86  self.scan_only_depsscan_only_deps = True
87  if "scan-dir" in args.operation:
88  self.scan_dirscan_dir = True
89  if self.scan_dirscan_dir and args.dir_path != '':
90  self.dir_pathdir_path = args.dir_path
91  if args.tags is not None and self.differentialdifferential and len(args.tags) == 2:
92  self.tagstags = (args.tags[0],args.tags[1])
93  if args.allowlist_path:
94  self.allowlist_pathallowlist_path = args.allowlist_path
95  if self.nomosnomos is False and self.ojoojo is False and self.copyrightcopyright is False \
96  and self.keywordkeyword is False:
97  self.nomosnomos = True
98  self.ojoojo = True
99  self.copyrightcopyright = True
100  self.keywordkeyword = True
101  self.report_formatreport_format = ReportFormat[args.report]
102  if self.keywordkeyword and args.keyword_conf:
103  self.keyword_conf_file_pathkeyword_conf_file_path = args.keyword_conf
104  if (self.scan_only_depsscan_only_deps or self.reporepo) and args.sbom_path:
105  self.sbom_pathsbom_path = args.sbom_path
def update_args(self, Namespace args)
Definition: CliOptions.py:65