Skip to content

Commit 40f43c1

Browse files
Use glob pattern matching for exclusion
Signed-off-by: Wonjae Park <[email protected]>
1 parent 6ffe719 commit 40f43c1

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

src/fosslight_source/cli.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import platform
99
import warnings
1010
import logging
11+
import glob
1112
from datetime import datetime
1213
import fosslight_util.constant as constant
1314
from fosslight_util.set_log import init_log
@@ -261,7 +262,7 @@ def create_report_file(
261262
return scan_item
262263

263264

264-
def merge_results(scancode_result: list = [], scanoss_result: list = [], spdx_downloads: dict = {}, path_to_exclude =[]) -> list:
265+
def merge_results(scancode_result: list = [], scanoss_result: list = [], spdx_downloads: dict = {}, path_to_exclude=[]) -> list:
265266
"""
266267
Merge scanner results and spdx parsing result.
267268
:param scancode_result: list of scancode results in SourceItem.
@@ -285,14 +286,24 @@ def merge_results(scancode_result: list = [], scanoss_result: list = [], spdx_do
285286
new_result_item.download_location = download_location
286287
scancode_result.append(new_result_item)
287288

289+
# normalize for windows
290+
normalized_patterns = [os.path.normpath(pattern).replace("\\", "/") for pattern in path_to_exclude]
291+
292+
# cnt = 0
293+
if path_to_exclude:
294+
for i in range(len(scancode_result) - 1, -1, -1): # Iterate from last to first
295+
item_path = scancode_result[i].source_name_or_path # Assuming SourceItem has 'file_path' attribute
296+
if any(
297+
glob.fnmatch.fnmatch(item_path, f"{pattern}") or # file name
298+
glob.fnmatch.fnmatch(item_path, f"*/{pattern}") or # file name in sub directory
299+
glob.fnmatch.fnmatch(item_path, f"*{pattern}/*") # directory name
300+
for pattern in normalized_patterns
301+
):
302+
# cnt += 1
303+
# print(cnt, "EXCLUDED:", item_path)
304+
del scancode_result[i] # Delete matching item
305+
288306
for item in scancode_result:
289-
item_path = item.source_name_or_path
290-
if any(
291-
excluded in item_path and
292-
(item_path == excluded or item_path.startswith(f"{excluded}{os.sep}"))
293-
for excluded in path_to_exclude
294-
):
295-
continue
296307
item.set_oss_item()
297308

298309
return scancode_result
@@ -346,7 +357,8 @@ def run_scanners(
346357
success, result_log[RESULT_KEY], scancode_result, license_list = run_scan(path_to_scan, output_file_name,
347358
write_json_file, num_cores, True,
348359
print_matched_text, formats, called_by_cli,
349-
time_out, correct_mode, correct_filepath)
360+
time_out, correct_mode, correct_filepath,
361+
path_to_exclude)
350362
if selected_scanner == 'scanoss' or selected_scanner == 'all' or selected_scanner == '':
351363
scanoss_result = run_scanoss_py(path_to_scan, output_file_name, formats, True, write_json_file, num_cores,
352364
path_to_exclude)

src/fosslight_source/run_scanoss.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ def run_scanoss_py(path_to_scan: str, output_file_name: str = "", format: list =
110110

111111
try:
112112
if write_json_file:
113+
destination_file = os.path.join(output_path, SCANOSS_RESULT_FILE)
114+
if os.path.exists(destination_file):
115+
os.remove(destination_file)
113116
shutil.move(SCANOSS_RESULT_FILE, output_path)
114117
else:
115118
os.remove(output_json_file)

0 commit comments

Comments
 (0)