Skip to content

Commit 832a75e

Browse files
committed
chore:SP-1676 Refactor on scanoss-py policy analyzer
1 parent 991f214 commit 832a75e

File tree

8 files changed

+450
-240
lines changed

8 files changed

+450
-240
lines changed

src/scanoss/cli.py

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,13 @@
2525
import os
2626
from pathlib import Path
2727
import sys
28-
29-
3028
import pypac
3129
from scanoss.inspection.copyleft import Copyleft
3230
from scanoss.inspection.undeclared_component import UndeclaredComponent
3331
from .threadeddependencies import SCOPE
34-
from .scanner import Scanner
3532
from .scanoss_settings import ScanossSettings
3633
from .scancodedeps import ScancodeDeps
37-
from .scanner import FAST_WINNOWING, Scanner
34+
from .scanner import Scanner
3835
from .scantype import ScanType
3936
from .filecount import FileCount
4037
from .cyclonedx import CycloneDx
@@ -313,11 +310,11 @@ def setup_args() -> None:
313310
p_copyleft.add_argument('--include', help='List of Copyleft licenses to append to the default list. Provide licenses as a comma-separated list.')
314311
p_copyleft.add_argument('--exclude', help='List of Copyleft licenses to remove from default list. Provide licenses as a comma-separated list.')
315312
p_copyleft.add_argument('--explicit', help='Explicit list of Copyleft licenses to consider. Provide licenses as a comma-separated list.s')
316-
p_copyleft.set_defaults(func=inspect)
313+
p_copyleft.set_defaults(func=inspect_copyleft)
317314

318315
# Inspect Sub-command: inspect undeclared
319316
p_undeclared = p_inspect_sub.add_parser('undeclared', aliases=['un'],description="Inspect for undeclared components", help='Inspect for undeclared components')
320-
p_undeclared.set_defaults(func=inspect)
317+
p_undeclared.set_defaults(func=inspect_undeclared)
321318

322319
for p in [p_copyleft, p_undeclared]:
323320
p.add_argument('-i', '--input', nargs='?', help='Path to results file')
@@ -810,7 +807,7 @@ def convert(parser, args):
810807
if not success:
811808
exit(1)
812809

813-
def inspect(parser, args):
810+
def inspect_copyleft(parser, args):
814811
"""
815812
Run the "inspect" sub-command
816813
Parameters
@@ -820,27 +817,59 @@ def inspect(parser, args):
820817
args: Namespace
821818
Parsed arguments
822819
"""
823-
if args.subparsercmd and not args.input :
820+
if args.input is None:
824821
print_stderr('Please specify an input file to inspect')
825822
parser.parse_args([args.subparser, args.subparsercmd, '-h'])
826823
exit(1)
827-
success = False
828-
if args.subparsercmd == 'copyleft':
829-
i_copyleft = Copyleft(debug=False, trace=args.trace, quiet=args.quiet, filepath=args.input, format=args.format,
830-
status=args.status, output=args.output, include=args.include,
831-
exclude=args.exclude, explicit=args.explicit)
832-
status, _ = i_copyleft.run()
833-
sys.exit(status)
834-
835-
if args.subparsercmd == 'undeclared':
836-
i_undeclared = UndeclaredComponent(debug=args.debug, trace=args.trace, quiet=args.quiet, filepath=args.input,
837-
format=args.format, status=args.status, output=args.output)
838-
status, _ = i_undeclared.run()
839-
sys.exit(status)
840824

841-
if not success:
825+
output: str = None
826+
if args.output:
827+
output = args.output
828+
open(output, 'w').close()
829+
830+
status_output: str = None
831+
if args.status:
832+
status_output = args.status
833+
open(status_output, 'w').close()
834+
835+
i_copyleft = Copyleft(debug=False, trace=args.trace, quiet=args.quiet, filepath=args.input,
836+
format_type=args.format, status=status_output, output=output, include=args.include,
837+
exclude=args.exclude, explicit=args.explicit)
838+
status, _ = i_copyleft.run()
839+
sys.exit(status)
840+
841+
842+
843+
def inspect_undeclared(parser, args):
844+
"""
845+
Run the "inspect" sub-command
846+
Parameters
847+
----------
848+
parser: ArgumentParser
849+
command line parser object
850+
args: Namespace
851+
Parsed arguments
852+
"""
853+
if args.input is None:
854+
print_stderr('Please specify an input file to inspect')
855+
parser.parse_args([args.subparser, args.subparsercmd, '-h'])
842856
exit(1)
843857

858+
output: str = None
859+
if args.output:
860+
output = args.output
861+
open(output, 'w').close()
862+
863+
status_output: str = None
864+
if args.output:
865+
status_output = args.status_output
866+
open(status_output, 'w').close()
867+
i_undeclared = UndeclaredComponent(debug=args.debug, trace=args.trace, quiet=args.quiet,
868+
filepath=args.input, format_type=args.format,
869+
status=status_output, output=output)
870+
status, _ = i_undeclared.run()
871+
sys.exit(status)
872+
844873

845874
def utils_certloc(*_):
846875
"""

src/scanoss/inspection/copyleft.py

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
1+
"""
2+
SPDX-License-Identifier: MIT
3+
4+
Copyright (c) 2021, SCANOSS
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
THE SOFTWARE.
23+
"""
124
import json
2-
import sys
3-
from typing import Dict, Any, Callable, List
25+
from typing import Dict, Any
426
from scanoss.inspection.policy_check import PolicyCheck, PolicyStatus
5-
from scanoss.inspection.utils.license_utils import license_util
6-
from scanoss.inspection.utils.markdown_utils import generate_table
7-
827

928
class Copyleft(PolicyCheck):
1029
"""
@@ -13,21 +32,23 @@ class Copyleft(PolicyCheck):
1332
"""
1433

1534
def __init__(self, debug: bool = False, trace: bool = True, quiet: bool = False, filepath: str = None,
16-
format: str = 'json', status: str = None, output: str = None, include: str = None, exclude: str = None, explicit: str = None):
35+
format_type: str = 'json', status: str = None, output: str = None, include: str = None,
36+
exclude: str = None, explicit: str = None):
1737
"""
1838
Initialize the Copyleft class.
1939
:param debug: Enable debug mode
2040
:param trace: Enable trace mode (default True)
2141
:param quiet: Enable quiet mode
2242
:param filepath: Path to the file containing component data
23-
:param format: Output format ('json' or 'md')
43+
:param format_type: Output format ('json' or 'md')
2444
:param status: Path to save the status output
2545
:param output: Path to save detailed output
2646
:param include: Licenses to include in the analysis
2747
:param exclude: Licenses to exclude from the analysis
2848
:param explicit: Explicitly defined licenses
2949
"""
30-
super().__init__(debug, trace, quiet, filepath, format, status, output, name='Copyleft Policy')
50+
super().__init__(debug, trace, quiet, filepath, format_type, status, output, name='Copyleft Policy')
51+
self.license_util.init(include, exclude, explicit)
3152
self.filepath = filepath
3253
self.format = format
3354
self.output = output
@@ -74,25 +95,10 @@ def _markdown(self, components: list) -> Dict[str,Any]:
7495
rows.append(row)
7596

7697
return {
77-
'details': f"### Copyleft licenses \n {generate_table(headers,rows,centeredColumns)}",
98+
'details': f"### Copyleft licenses \n {self.generate_table(headers,rows,centeredColumns)}",
7899
'summary' : f"{len(components)} component(s) with copyleft licenses were found."
79100
}
80101

81-
def _get_formatter(self)-> Callable[[List[dict]], Dict[str,Any]] or None:
82-
"""
83-
Get the appropriate formatter function based on the specified format.
84-
:return: Formatter function (either _json or _markdown)
85-
"""
86-
valid_format = self._is_valid_format()
87-
if not valid_format:
88-
return None
89-
90-
function_map = {
91-
'json': self._json,
92-
'md': self._markdown
93-
}
94-
return function_map[self.format]
95-
96102
def _filter_components_with_copyleft_licenses(self, components: list) -> list:
97103
"""
98104
Filter the components list to include only those with copyleft licenses.
@@ -124,12 +130,8 @@ def run(self):
124130
125131
:return: Dictionary containing the inspection results
126132
"""
127-
if not self._init():
128-
return PolicyStatus.ERROR.value, {}
129133

130134
self._debug()
131-
132-
license_util.init(self.include, self.exclude, self.explicit)
133135
components = self._get_components()
134136
if components is None:
135137
return PolicyStatus.ERROR.value, {}

0 commit comments

Comments
 (0)