Skip to content

Commit c4c1c5e

Browse files
JustinWonjaeParksoimkim
authored andcommitted
Seperate common codes for scanning. Add scanoss.py as a scanner
1 parent 3f6dc00 commit c4c1c5e

File tree

9 files changed

+495
-147
lines changed

9 files changed

+495
-147
lines changed

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@
3131
"Programming Language :: Python :: 3.8",
3232
"Programming Language :: Python :: 3.9", ],
3333
install_requires=required,
34+
extras_require={":python_version>'3.6'": ["scanoss>=0.7.0"]},
3435
entry_points={
3536
"console_scripts": [
3637
"fosslight_convert = fosslight_source.convert_scancode:main",
37-
"fosslight_source = fosslight_source.run_scancode:main",
38+
"fosslight_source = fosslight_source.cli:main",
3839
"convert_scancode = fosslight_source.convert_scancode:main",
3940
"run_scancode = fosslight_source.run_scancode:main"
4041
]

src/fosslight_source/_help.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@
1919
2020
Optional
2121
-h\t\t\t\t Print help message
22-
-j\t\t\t\t Generate additional result of executing ScanCode in json format
23-
-m\t\t\t\t Print the Matched text for each license on a separate sheet
22+
-j\t\t\t\t Generate raw result of scanners in json format
23+
-m\t\t\t\t Print the Matched text for each license on a separate sheet (Scancode Only)
2424
-o <output_path>\t\t Output path
2525
\t\t\t\t (If you want to generate the specific file name, add the output path with file name.)
26-
-f <format>\t\t\t Output file format (excel, csv, opossum)"""
26+
-f <format>\t\t\t Output file format (excel, csv, opossum)
27+
-s <scanner>\t\t Select which scanner to be run (scancode, scanoss, all)"""
2728

2829
_HELP_MESSAGE_CONVERT = """
2930
Usage: fosslight_convert [option1] <arg1> [option2] <arg2>...

src/fosslight_source/_parsing_scancode_file_item.py

Lines changed: 6 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -8,96 +8,18 @@
88
import re
99
import fosslight_util.constant as constant
1010
from ._license_matched import MatchedLicense
11+
from ._scan_item import ScanItem
12+
from ._scan_item import is_exclude_dir
13+
from ._scan_item import is_exclude_file
14+
from ._scan_item import replace_word
1115

1216
logger = logging.getLogger(constant.LOGGER_NAME)
13-
_replace_word = ["-only", "-old-style", "-or-later", "licenseref-scancode-"]
14-
_exclude_filename = ["changelog", "config.guess", "config.sub",
15-
"config.h.in", "changes", "ltmain.sh",
16-
"aclocal.m4", "configure", "configure.ac",
17-
"depcomp", "compile", "missing", "libtool.m4",
18-
"makefile"]
1917
_exclude_directory = ["test", "tests", "doc", "docs"]
2018
_exclude_directory = [os.path.sep + dir_name +
2119
os.path.sep for dir_name in _exclude_directory]
2220
_exclude_directory.append("/.")
2321

2422

25-
class ScanCodeItem:
26-
file = ""
27-
licenses = []
28-
copyright = ""
29-
exclude = False
30-
is_license_text = False
31-
32-
def __init__(self, value):
33-
self.file = value
34-
self.copyright = []
35-
self.licenses = []
36-
self.comment = ""
37-
self.exclude = False
38-
self.is_license_text = False
39-
40-
def __del__(self):
41-
pass
42-
43-
def set_comment(self, value):
44-
self.comment = value
45-
46-
def set_file(self, value):
47-
self.file = value
48-
49-
def set_copyright(self, value):
50-
self.copyright.extend(value)
51-
if len(self.copyright) > 0:
52-
self.copyright = list(set(self.copyright))
53-
54-
def set_licenses(self, value):
55-
self.licenses.extend(value)
56-
if len(self.licenses) > 0:
57-
self.licenses = list(set(self.licenses))
58-
59-
def set_exclude(self, value):
60-
self.exclude = value
61-
62-
def set_is_license_text(self, value):
63-
self.is_license_text = value
64-
65-
def get_row_to_print(self):
66-
print_rows = [self.file, "", "", ','.join(self.licenses), "", "",
67-
','.join(self.copyright),
68-
"Exclude" if self.exclude else "",
69-
self.comment]
70-
return print_rows
71-
72-
73-
def is_exclude_dir(dir_path):
74-
if dir_path != "":
75-
dir_path = dir_path.lower()
76-
dir_path = dir_path if dir_path.endswith(
77-
os.path.sep) else dir_path + os.path.sep
78-
dir_path = dir_path if dir_path.startswith(
79-
os.path.sep) else os.path.sep + dir_path
80-
return any(dir_name in dir_path for dir_name in _exclude_directory)
81-
return False
82-
83-
84-
def is_exclude_file(file_path, prev_dir, prev_dir_exclude_value):
85-
file_path = file_path.lower()
86-
filename = os.path.basename(file_path)
87-
if filename in _exclude_filename:
88-
return True
89-
90-
dir_path = os.path.dirname(file_path)
91-
if dir_path == prev_dir:
92-
return prev_dir_exclude_value
93-
else:
94-
# There will be no execution of this else statement.
95-
# Because scancode json output results are sorted by path,
96-
# most of them will match the previous if statement.
97-
return is_exclude_dir(dir_path)
98-
return False
99-
100-
10123
def get_error_from_header(header_item):
10224
has_error = False
10325
str_error = ""
@@ -146,7 +68,7 @@ def parsing_file_item(scancode_file_list, has_error, need_matched_license=False)
14668
licenses = file["licenses"]
14769
copyright_list = file["copyrights"]
14870

149-
result_item = ScanCodeItem(file_path)
71+
result_item = ScanItem(file_path)
15072

15173
if has_error and "scan_errors" in file:
15274
error_msg = file["scan_errors"]
@@ -196,7 +118,7 @@ def parsing_file_item(scancode_file_list, has_error, need_matched_license=False)
196118
except Exception:
197119
pass
198120

199-
for word in _replace_word:
121+
for word in replace_word:
200122
if word in license_value:
201123
license_value = license_value.replace(word, "")
202124
license_detected.append(license_value)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# Copyright (c) 2020 LG Electronics Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
import logging
7+
import fosslight_util.constant as constant
8+
from ._scan_item import ScanItem
9+
from ._scan_item import is_exclude_file
10+
11+
logger = logging.getLogger(constant.LOGGER_NAME)
12+
13+
14+
def parsing_scanResult(scanoss_report):
15+
scanoss_file_item = []
16+
17+
for file_path, findings in scanoss_report.items():
18+
result_item = ScanItem(file_path)
19+
if 'id' in findings[0]:
20+
if "none" == findings[0]['id']:
21+
continue
22+
23+
if 'component' in findings[0]:
24+
result_item.set_oss_name(findings[0]['component'])
25+
if 'version' in findings[0]:
26+
result_item.set_oss_version(findings[0]['version'])
27+
if 'url' in findings[0]:
28+
result_item.set_download_location(findings[0]['url'])
29+
30+
license_detected = []
31+
copyright_detected = []
32+
if 'licenses' in findings[0]:
33+
for license in findings[0]['licenses']:
34+
license_detected.append(license['name'].lower())
35+
if len(license_detected) > 0:
36+
result_item.set_licenses(license_detected)
37+
if 'copyrights' in findings[0]:
38+
for copyright in findings[0]['copyrights']:
39+
copyright_detected.append(copyright['name'])
40+
if len(copyright_detected) > 0:
41+
result_item.set_copyright(copyright_detected)
42+
43+
if is_exclude_file(file_path):
44+
result_item.set_exclude(True)
45+
46+
comment = ""
47+
comment += "Match Type : " + findings[0]['id'] + ", "
48+
if 'vendor' in findings[0]:
49+
comment += "Vendor : " + findings[0]['vendor'] + ", "
50+
if 'file_url' in findings[0]:
51+
comment += "File URL : " + findings[0]['file_url'] + ", "
52+
if 'matched' in findings[0]:
53+
comment += "Matched : " + findings[0]['matched'] + ", "
54+
if 'lines' in findings[0]:
55+
comment += "Lines : " + findings[0]['lines']
56+
result_item.set_comment(comment)
57+
58+
scanoss_file_item.append(result_item)
59+
60+
return scanoss_file_item

src/fosslight_source/_scan_item.py

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# Copyright (c) 2020 LG Electronics Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
import os
7+
import logging
8+
import fosslight_util.constant as constant
9+
10+
logger = logging.getLogger(constant.LOGGER_NAME)
11+
replace_word = ["-only", "-old-style", "-or-later", "licenseref-scancode-"]
12+
_exclude_filename = ["changelog", "config.guess", "config.sub",
13+
"config.h.in", "changes", "ltmain.sh",
14+
"aclocal.m4", "configure", "configure.ac",
15+
"depcomp", "compile", "missing", "libtool.m4",
16+
"makefile"]
17+
_exclude_directory = ["test", "tests", "doc", "docs"]
18+
_exclude_directory = [os.path.sep + dir_name +
19+
os.path.sep for dir_name in _exclude_directory]
20+
_exclude_directory.append("/.")
21+
22+
23+
class ScanItem:
24+
file = ""
25+
licenses = []
26+
copyright = ""
27+
exclude = False
28+
is_license_text = False
29+
oss_name = ""
30+
oss_version = ""
31+
download_location = ""
32+
33+
def __init__(self, value):
34+
self.file = value
35+
self.copyright = []
36+
self.licenses = []
37+
self.comment = ""
38+
self.exclude = False
39+
self.is_license_text = False
40+
41+
def __del__(self):
42+
pass
43+
44+
def set_comment(self, value):
45+
self.comment = value
46+
47+
def set_file(self, value):
48+
self.file = value
49+
50+
def set_copyright(self, value):
51+
self.copyright.extend(value)
52+
if len(self.copyright) > 0:
53+
self.copyright = list(set(self.copyright))
54+
55+
def set_licenses(self, value):
56+
self.licenses.extend(value)
57+
if len(self.licenses) > 0:
58+
self.licenses = list(set(self.licenses))
59+
60+
def set_exclude(self, value):
61+
self.exclude = value
62+
63+
def set_is_license_text(self, value):
64+
self.is_license_text = value
65+
66+
def set_oss_name(self, value):
67+
self.oss_name = value
68+
69+
def set_oss_version(self, value):
70+
self.oss_version = value
71+
72+
def set_download_location(self, value):
73+
self.download_location = value
74+
75+
def get_row_to_print(self):
76+
if not self.download_location:
77+
print_rows = [self.file, "", "", ','.join(self.licenses), "", "",
78+
','.join(self.copyright),
79+
"Exclude" if self.exclude else "",
80+
self.comment]
81+
else:
82+
print_rows = [self.file, self.oss_name, self.oss_version, ','.join(self.licenses), self.download_location, "",
83+
','.join(self.copyright),
84+
"Exclude" if self.exclude else "",
85+
self.comment]
86+
return print_rows
87+
88+
def merge_scan_item(self, other):
89+
"""
90+
Merge two ScanItem instance into one.
91+
92+
TODO: define how to merge comments and implement.
93+
"""
94+
self.licenses = list(set(self.licenses + other.licenses))
95+
96+
if len(self.copyright) > 0:
97+
self.copyright = list(set(self.copyright))
98+
99+
if self.exclude and other.exclude:
100+
self.exclude = True
101+
else:
102+
self.exclude = False
103+
104+
if not self.oss_name:
105+
self.oss_name = other.oss_name
106+
if not self.oss_version:
107+
self.oss_version = other.oss_version
108+
if not self.download_location:
109+
self.download_location = other.download_location
110+
111+
def __eq__(self, other):
112+
return self.file == other.file
113+
114+
115+
def is_exclude_dir(dir_path):
116+
if dir_path != "":
117+
dir_path = dir_path.lower()
118+
dir_path = dir_path if dir_path.endswith(
119+
os.path.sep) else dir_path + os.path.sep
120+
dir_path = dir_path if dir_path.startswith(
121+
os.path.sep) else os.path.sep + dir_path
122+
return any(dir_name in dir_path for dir_name in _exclude_directory)
123+
return False
124+
125+
126+
def is_exclude_file(file_path, prev_dir=None, prev_dir_exclude_value=None):
127+
file_path = file_path.lower()
128+
filename = os.path.basename(file_path)
129+
if filename in _exclude_filename:
130+
return True
131+
132+
dir_path = os.path.dirname(file_path)
133+
if prev_dir is not None: # running ScanCode
134+
if dir_path == prev_dir:
135+
return prev_dir_exclude_value
136+
else:
137+
# There will be no execution of this else statement.
138+
# Because scancode json output results are sorted by path,
139+
# most of them will match the previous if statement.
140+
return is_exclude_dir(dir_path)
141+
else: # running SCANOSS
142+
return is_exclude_dir(dir_path)
143+
return False

0 commit comments

Comments
 (0)