Skip to content

Commit 53fe700

Browse files
authored
Merge pull request #183 from fosslight/ossitem
Refactoring OSS Item
2 parents 3877214 + 0921181 commit 53fe700

File tree

8 files changed

+81
-98
lines changed

8 files changed

+81
-98
lines changed

requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ pyparsing
22
scancode-toolkit>=32.0.2,==32.0.*
33
scanoss
44
XlsxWriter
5-
fosslight_util~=1.4.47
5+
fosslight_util>=2.0.0
66
PyYAML
77
wheel>=0.38.1
88
intbitset
9-
fosslight_binary
9+
fosslight_binary>=5.0.0
1010
typecode-libmagic;sys_platform!="darwin"

src/fosslight_source/_parsing_scancode_file_item.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import re
99
import fosslight_util.constant as constant
1010
from ._license_matched import MatchedLicense
11-
from ._scan_item import ScanItem
11+
from ._scan_item import SourceItem
1212
from ._scan_item import is_exclude_dir
1313
from ._scan_item import is_exclude_file
1414
from ._scan_item import replace_word
@@ -75,7 +75,7 @@ def parsing_scancode_32_earlier(scancode_file_list, has_error=False):
7575
licenses = file.get("licenses", [])
7676
copyright_list = file.get("copyrights", [])
7777

78-
result_item = ScanItem(file_path)
78+
result_item = SourceItem(file_path)
7979

8080
if has_error and "scan_errors" in file:
8181
error_msg = file.get("scan_errors", [])
@@ -201,7 +201,7 @@ def parsing_scancode_32_later(scancode_file_list, has_error=False):
201201
if (not file_path) or is_binary or is_dir:
202202
continue
203203

204-
result_item = ScanItem(file_path)
204+
result_item = SourceItem(file_path)
205205

206206
if has_error:
207207
error_msg = file.get("scan_errors", [])

src/fosslight_source/_parsing_scanoss_file.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import os
77
import logging
88
import fosslight_util.constant as constant
9-
from ._scan_item import ScanItem
9+
from ._scan_item import SourceItem
1010
from ._scan_item import is_exclude_file
1111
from ._scan_item import replace_word
1212

@@ -22,14 +22,14 @@ def parsing_extraInfo(scanned_result):
2222
license_w_source = scan_item.scanoss_reference
2323
if scan_item.matched_lines:
2424
if license_w_source:
25-
extra_item = [scan_item.file, ','.join(license_w_source['component_declared']),
25+
extra_item = [scan_item.source_name_or_path, ','.join(license_w_source['component_declared']),
2626
','.join(license_w_source['file_spdx_tag']),
2727
','.join(license_w_source['file_header']),
2828
','.join(license_w_source['license_file']),
2929
','.join(license_w_source['scancode']),
3030
scan_item.matched_lines, scan_item.fileURL]
3131
else:
32-
extra_item = [scan_item.file, '', '', '', '', '', scan_item.matched_lines, scan_item.fileURL]
32+
extra_item = [scan_item.source_name_or_path, '', '', '', '', '', scan_item.matched_lines, scan_item.fileURL]
3333
scanoss_extra_info.append(extra_item)
3434
scanoss_extra_info.insert(0, SCANOSS_INFO_HEADER)
3535
return scanoss_extra_info
@@ -43,7 +43,7 @@ def parsing_scanResult(scanoss_report, path_to_scan="", path_to_exclude=[]):
4343
abs_file_path = os.path.abspath(os.path.join(path_to_scan, file_path))
4444
if any(os.path.commonpath([abs_file_path, exclude_path]) == exclude_path for exclude_path in abs_path_to_exclude):
4545
continue
46-
result_item = ScanItem(file_path)
46+
result_item = SourceItem(file_path)
4747
if 'id' in findings[0]:
4848
if "none" == findings[0]['id']:
4949
continue

src/fosslight_source/_scan_item.py

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import logging
88
import re
99
import fosslight_util.constant as constant
10+
from fosslight_util.oss_item import FileItem, OssItem
1011

1112
logger = logging.getLogger(constant.LOGGER_NAME)
1213
replace_word = ["-only", "-old-style", "-or-later", "licenseref-scancode-", "licenseref-"]
@@ -25,43 +26,28 @@
2526
SUBSTRING_LICENSE_COMMENT = "Maximum character limit (License)"
2627

2728

28-
class ScanItem:
29-
file = ""
30-
scanoss_reference = {}
31-
exclude = False
32-
is_license_text = False
33-
oss_name = ""
34-
oss_version = ""
35-
download_location = []
36-
matched_lines = "" # Only for SCANOSS results
37-
fileURL = "" # Only for SCANOSS results
38-
license_reference = ""
29+
class SourceItem(FileItem):
3930

4031
def __init__(self, value):
41-
self.file = value
42-
self._copyright = []
43-
self._licenses = []
44-
self.download_location = []
45-
self.comment = ""
46-
self.exclude = False
32+
super().__init__("")
33+
self.source_name_or_path = value
4734
self.is_license_text = False
35+
self.license_reference = ""
36+
self.scanoss_reference = {}
37+
self.matched_lines = "" # Only for SCANOSS results
38+
self.fileURL = "" # Only for SCANOSS results
39+
self.download_location = []
40+
self.copyright = []
41+
self._licenses = []
42+
self.oss_name = ""
43+
self.oss_version = ""
4844

4945
def __del__(self):
5046
pass
5147

5248
def __hash__(self):
5349
return hash(self.file)
5450

55-
@property
56-
def copyright(self):
57-
return self._copyright
58-
59-
@copyright.setter
60-
def copyright(self, value):
61-
self._copyright.extend(value)
62-
if len(self._copyright) > 0:
63-
self._copyright = list(set(self._copyright))
64-
6551
@property
6652
def licenses(self):
6753
return self._licenses
@@ -84,27 +70,34 @@ def licenses(self, value):
8470
if max_length_exceed and (SUBSTRING_LICENSE_COMMENT not in self.comment):
8571
self.comment = f"{self.comment}/ {SUBSTRING_LICENSE_COMMENT}" if self.comment else SUBSTRING_LICENSE_COMMENT
8672

87-
def get_file(self):
88-
return self.file
73+
def set_oss_item(self):
74+
self.oss_items = []
75+
if self.download_location:
76+
for url in self.download_location:
77+
item = OssItem(self.oss_name, self.oss_version, self.licenses, url)
78+
item.copyright = "\n".join(self.copyright)
79+
item.comment = self.comment
80+
self.oss_items.append(item)
81+
else:
82+
item = OssItem(self.oss_name, self.oss_version, self.licenses)
83+
item.copyright = "\n".join(self.copyright)
84+
item.comment = self.comment
85+
self.oss_items.append(item)
8986

90-
def get_row_to_print(self):
87+
def get_print_array(self):
9188
print_rows = []
92-
if not self.download_location:
93-
print_rows.append([self.file, self.oss_name, self.oss_version, ",".join(self.licenses), "", "",
94-
"\n".join(self.copyright), "Exclude" if self.exclude else "", self.comment,
89+
for item in self.oss_items:
90+
print_rows.append([self.source_name_or_path, item.name, item.version, ",".join(item.license),
91+
item.download_location, "",
92+
item.copyright, "Exclude" if self.exclude else "", item.comment,
9593
self.license_reference])
96-
else:
97-
for url in self.download_location:
98-
print_rows.append([self.file, self.oss_name, self.oss_version, ",".join(self.licenses), url, "",
99-
"\n".join(self.copyright), "Exclude" if self.exclude else "", self.comment,
100-
self.license_reference])
10194
return print_rows
10295

10396
def __eq__(self, other):
10497
if type(other) == str:
105-
return self.file == other
98+
return self.source_name_or_path == other
10699
else:
107-
return self.file == other.file
100+
return self.source_name_or_path == other.source_name_or_path
108101

109102

110103
def is_exclude_dir(dir_path):

src/fosslight_source/cli.py

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
import yaml
2222
import argparse
2323
from .run_spdx_extractor import get_spdx_downloads
24-
from ._scan_item import ScanItem
25-
from fosslight_util.cover import CoverItem
24+
from ._scan_item import SourceItem
25+
from fosslight_util.oss_item import ScannerItem
2626

2727
SRC_SHEET_NAME = 'SRC_FL_Source'
2828
SCANOSS_HEADER = {SRC_SHEET_NAME: ['ID', 'Source Path', 'OSS Name',
@@ -35,7 +35,7 @@
3535

3636
logger = logging.getLogger(constant.LOGGER_NAME)
3737
warnings.filterwarnings("ignore", category=FutureWarning)
38-
_PKG_NAME = "fosslight_source"
38+
PKG_NAME = "fosslight_source"
3939
RESULT_KEY = "Scan Result"
4040

4141

@@ -75,7 +75,7 @@ def main():
7575
if args.help:
7676
print_help_msg_source_scanner()
7777
if args.version:
78-
print_version(_PKG_NAME)
78+
print_version(PKG_NAME)
7979
if not args.path:
8080
path_to_scan = os.getcwd()
8181
else:
@@ -171,24 +171,21 @@ def create_report_file(_start_time, merged_result, license_list, scanoss_result,
171171

172172
if not correct_filepath:
173173
correct_filepath = path_to_scan
174-
cover = CoverItem(tool_name=_PKG_NAME,
175-
start_time=_start_time,
176-
input_path=path_to_scan,
177-
exclude_path=path_to_exclude)
174+
175+
scan_item = ScannerItem(PKG_NAME, _start_time)
176+
scan_item.set_cover_pathinfo(path_to_scan, path_to_exclude)
178177
files_count, removed_files_count = count_files(path_to_scan, path_to_exclude)
179-
cover.comment = f"Total number of files / removed files: {files_count} / {removed_files_count}"
178+
scan_item.set_cover_comment(f"Total number of files / removed files: {files_count} / {removed_files_count}")
180179

181-
if len(merged_result) == 0:
180+
if not merged_result:
182181
if files_count < 1:
183-
cover.comment += "(No file detected.)"
182+
scan_item.set_cover_comment("(No file detected.)")
184183
else:
185-
cover.comment += "(No OSS detected.)"
184+
scan_item.set_cover_comment("(No OSS detected.)")
186185

187-
sheet_list[SRC_SHEET_NAME] = []
188186
if merged_result:
189-
for scan_item in merged_result:
190-
for row in scan_item.get_row_to_print():
191-
sheet_list[SRC_SHEET_NAME].append(row)
187+
sheet_list = {}
188+
scan_item.append_file_items(merged_result, PKG_NAME)
192189

193190
if selected_scanner == 'scanoss':
194191
extended_header = SCANOSS_HEADER
@@ -203,37 +200,40 @@ def create_report_file(_start_time, merged_result, license_list, scanoss_result,
203200
else:
204201
sheet_list["scancode_reference"] = get_license_list_to_print(license_list)
205202
sheet_list["scanoss_reference"] = get_scanoss_extra_info(scanoss_result)
203+
if sheet_list:
204+
scan_item.external_sheets = sheet_list
206205

207206
if correct_mode:
208-
success, msg_correct, correct_list = correct_with_yaml(correct_filepath, path_to_scan, sheet_list)
207+
success, msg_correct, correct_item = correct_with_yaml(correct_filepath, path_to_scan, scan_item)
209208
if not success:
210209
logger.info(f"No correction with yaml: {msg_correct}")
211210
else:
212-
sheet_list = correct_list
211+
scan_item = correct_item
213212
logger.info("Success to correct with yaml.")
214213

215214
combined_paths_and_files = [os.path.join(output_path, file) for file in output_files]
216215
results = []
217216
for combined_path_and_file, output_extension in zip(combined_paths_and_files, output_extensions):
218-
if need_license and output_extension == _json_ext and "scanoss_reference" in sheet_list:
219-
del sheet_list["scanoss_reference"]
220-
results.append(write_output_file(combined_path_and_file, output_extension, sheet_list, extended_header, "", cover))
217+
# if need_license and output_extension == _json_ext and "scanoss_reference" in sheet_list:
218+
# del sheet_list["scanoss_reference"]
219+
results.append(write_output_file(combined_path_and_file, output_extension, scan_item, extended_header, ""))
221220
for success, msg, result_file in results:
222221
if success:
223222
logger.info(f"Output file: {result_file}")
224-
if cover:
225-
logger.info(f'{cover.comment}')
223+
for row in scan_item.get_cover_comment():
224+
logger.info(row)
226225
else:
227226
logger.error(f"Fail to generate result file {result_file}. msg:({msg})")
227+
return scan_item
228228

229229

230230
def merge_results(scancode_result=[], scanoss_result=[], spdx_downloads={}):
231231
"""
232232
Merge scanner results and spdx parsing result.
233-
:param scancode_result: list of scancode results in ScanItem.
234-
:param scanoss_result: list of scanoss results in ScanItem.
233+
:param scancode_result: list of scancode results in SourceItem.
234+
:param scanoss_result: list of scanoss results in SourceItem.
235235
:param spdx_downloads: dictionary of spdx parsed results.
236-
:return merged_result: list of merged result in ScanItem.
236+
:return merged_result: list of merged result in SourceItem.
237237
"""
238238

239239
# If anything that is found at SCANOSS only exist, add it to result.
@@ -247,9 +247,13 @@ def merge_results(scancode_result=[], scanoss_result=[], spdx_downloads={}):
247247
merged_result_item = scancode_result[scancode_result.index(file_name)]
248248
merged_result_item.download_location = download_location
249249
else:
250-
new_result_item = ScanItem(file_name)
250+
new_result_item = SourceItem(file_name)
251251
new_result_item.download_location = download_location
252252
scancode_result.append(new_result_item)
253+
254+
for item in scancode_result:
255+
item.set_oss_item()
256+
253257
return scancode_result
254258

255259

@@ -284,7 +288,7 @@ def run_scanners(path_to_scan, output_file_name="", write_json_file=False, num_c
284288
success, msg, output_path, output_files, output_extensions = check_output_formats(output_file_name, formats)
285289

286290
logger, result_log = init_log(os.path.join(output_path, f"fosslight_log_src_{start_time}.txt"),
287-
True, logging.INFO, logging.DEBUG, _PKG_NAME, path_to_scan, path_to_exclude)
291+
True, logging.INFO, logging.DEBUG, PKG_NAME, path_to_scan, path_to_exclude)
288292

289293
if '.xlsx' not in output_extensions and print_matched_text:
290294
logger.warning("-m option is only available for excel.")
@@ -302,17 +306,17 @@ def run_scanners(path_to_scan, output_file_name="", write_json_file=False, num_c
302306
if selected_scanner in SCANNER_TYPE:
303307
spdx_downloads = get_spdx_downloads(path_to_scan, path_to_exclude)
304308
merged_result = merge_results(scancode_result, scanoss_result, spdx_downloads)
305-
create_report_file(start_time, merged_result, license_list, scanoss_result, selected_scanner,
306-
print_matched_text, output_path, output_files, output_extensions, correct_mode,
307-
correct_filepath, path_to_scan, path_to_exclude)
309+
scan_item = create_report_file(start_time, merged_result, license_list, scanoss_result, selected_scanner,
310+
print_matched_text, output_path, output_files, output_extensions, correct_mode,
311+
correct_filepath, path_to_scan, path_to_exclude)
308312
else:
309313
print_help_msg_source_scanner()
310314
result_log[RESULT_KEY] = "Unsupported scanner"
311315
success = False
312316
else:
313317
result_log[RESULT_KEY] = f"Format error. {msg}"
314318
success = False
315-
return success, result_log.get(RESULT_KEY, ""), merged_result, license_list, scanoss_result
319+
return success, result_log.get(RESULT_KEY, ""), scan_item, license_list, scanoss_result
316320

317321

318322
if __name__ == '__main__':

src/fosslight_source/run_scancode.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from fosslight_util.set_log import init_log
1515
from ._parsing_scancode_file_item import parsing_file_item
1616
from ._parsing_scancode_file_item import get_error_from_header
17-
from ._license_matched import get_license_list_to_print
1817
from fosslight_util.output_format import check_output_formats
1918
from fosslight_binary.binary_analysis import check_binary
2019

@@ -100,13 +99,10 @@ def run_scan(path_to_scan, output_file_name="",
10099
output_json_pp=output_json_file, only_findings=True,
101100
license_text=True, url=True, timeout=time_out,
102101
include=(), ignore=tuple(total_files_to_excluded))
103-
104102
if not rc:
105103
msg = "Source code analysis failed."
106104
success = False
107-
108105
if results:
109-
sheet_list = {}
110106
has_error = False
111107
if "headers" in results:
112108
has_error, error_msg = get_error_from_header(results["headers"])
@@ -125,13 +121,8 @@ def run_scan(path_to_scan, output_file_name="",
125121
result_list, key=lambda row: (''.join(row.licenses)))
126122

127123
for scan_item in result_list:
128-
if check_binary(os.path.join(path_to_scan, scan_item.file)):
124+
if check_binary(os.path.join(path_to_scan, scan_item.source_name_or_path)):
129125
scan_item.exclude = True
130-
131-
sheet_list["SRC_FL_Source"] = [scan_item.get_row_to_print() for scan_item in result_list]
132-
if need_license:
133-
sheet_list["matched_text"] = get_license_list_to_print(license_list)
134-
135126
except Exception as ex:
136127
success = False
137128
msg = str(ex)

tests/cli_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ def main():
3434
if len(ret) > 2:
3535
try:
3636
for scan_item in ret[2]:
37-
logger.warning(scan_item.get_row_to_print())
37+
logger.warning(scan_item.get_print_array())
3838
except Exception as ex:
3939
logger.error("Error:"+str(ex))
4040
if ret_scanoss:
4141
for scan_item in ret_scanoss:
42-
logger.warning(scan_item.get_row_to_print())
42+
logger.warning(scan_item.get_print_array())
4343

4444

4545
if __name__ == '__main__':

0 commit comments

Comments
 (0)