Skip to content

Commit bc6e448

Browse files
committed
Supports for excluding paths in lint mode
Signed-off-by: SeongjunJo <[email protected]>
1 parent 0c37530 commit bc6e448

File tree

6 files changed

+64
-19
lines changed

6 files changed

+64
-19
lines changed

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ binaryornot
33
requests
44
reuse==1.1.2
55
PyYAML
6-
fosslight_util>=1.4.23
6+
fosslight_util>=1.4.43
77
jinja2

src/fosslight_prechecker/_help.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
1111
Usage: fosslight_prechecker [Mode] [option1] <arg1> [option2] <arg2>...
1212
ex) fosslight_prechecker lint -p /home/test/src/
13+
fosslight_prechecker lint -p /home/test/src/ -e test.py dep/temp
1314
fosslight_prechecker add -p /home/test/test.py -c "2019-2021 LG Electronics Inc." -l "GPL-3.0-only"
1415
fosslight_prechecker convert -p /home/test/sbom_info.yaml
1516
@@ -23,6 +24,7 @@
2324
-h\t\t\t Print help message
2425
-v\t\t\t Print FOSSLight Prechecker version
2526
-p <path>\t\t Path to check(Default: current directory)
27+
-e <path>\t\t Path to exclude from checking(only work with 'lint' mode)
2628
-f <format>\t\t Result format(yaml, xml, html)
2729
-o <file_name>\t Output file name
2830
-i\t\t\t Don't both write log file and show progress bar

src/fosslight_prechecker/_precheck.py

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
is_windows = platform.system() == 'Windows'
2626
REUSE_CONFIG_FILE = ".reuse/dep5"
2727
DEFAULT_EXCLUDE_EXTENSION_FILES = [] # Exclude files from reuse
28+
user_exclude_list = [] # Exclude paths from checking
2829
_turn_on_exclude_config = True
2930
_check_only_file_mode = False
3031
error_items = []
@@ -86,7 +87,7 @@ def exclude_git_related_files(path):
8687
logger.warning(f"Error to get git related files : {ex}")
8788

8889

89-
def find_oss_pkg_info_and_exlcude_file(path):
90+
def find_oss_pkg_info_and_exclude_file(path):
9091
global DEFAULT_EXCLUDE_EXTENSION_FILES
9192
oss_pkg_info = []
9293
git_present = shutil.which("git")
@@ -96,8 +97,14 @@ def find_oss_pkg_info_and_exlcude_file(path):
9697

9798
try:
9899
for root, dirs, files in os.walk(path):
100+
if os.path.abspath(root) in user_exclude_list:
101+
continue
99102
for dir in dirs:
100103
# For hidden folders
104+
dir_path = os.path.join(root, dir)
105+
dir_abs_path = os.path.abspath(dir_path)
106+
if any(os.path.commonpath([dir_abs_path, exclude_path]) == exclude_path for exclude_path in user_exclude_list):
107+
continue
101108
if dir.startswith("."):
102109
all_files_in_dir = [os.path.join(root, dir, file)
103110
for file in os.listdir(os.path.join(root, dir))
@@ -106,17 +113,20 @@ def find_oss_pkg_info_and_exlcude_file(path):
106113
DEFAULT_EXCLUDE_EXTENSION_FILES.extend(all_files_rel_path)
107114

108115
for file in files:
116+
file_path = os.path.join(root, file)
117+
file_abs_path = os.path.abspath(file_path)
118+
if any(os.path.commonpath([file_abs_path, exclude_path]) == exclude_path for exclude_path in user_exclude_list):
119+
continue
109120
file_lower_case = file.lower()
110-
file_abs_path = os.path.join(root, file)
111-
file_rel_path = os.path.relpath(file_abs_path, path)
121+
file_rel_path = os.path.relpath(file_path, path)
112122

113123
if any(re.search(re_oss_pkg_pattern, file_lower_case) for re_oss_pkg_pattern in OSS_PKG_INFO_FILES) \
114124
or file_lower_case.startswith("module_license_"):
115125
oss_pkg_info.append(file_rel_path)
116126
# Exclude hidden files
117127
elif _turn_on_exclude_config and file.startswith('.'):
118128
DEFAULT_EXCLUDE_EXTENSION_FILES.append(file_rel_path)
119-
elif is_binary(file_abs_path):
129+
elif is_binary(file_path):
120130
DEFAULT_EXCLUDE_EXTENSION_FILES.append(file_rel_path)
121131
else:
122132
extension = file_lower_case.split(".")[-1]
@@ -232,7 +242,7 @@ def precheck_for_project(path_to_find):
232242
missing_license = []
233243
missing_copyright = []
234244

235-
oss_pkg_info_files = find_oss_pkg_info_and_exlcude_file(path_to_find)
245+
oss_pkg_info_files = find_oss_pkg_info_and_exclude_file(path_to_find)
236246
if _turn_on_exclude_config:
237247
need_rollback, temp_file_name, temp_dir_name = create_reuse_dep5_file(path_to_find)
238248

@@ -244,13 +254,16 @@ def precheck_for_project(path_to_find):
244254
missing_license = [str(sub) for sub in set(report.files_without_licenses)]
245255
if not path_to_find.endswith(f"{os.sep}"):
246256
path_to_find += f"{os.sep}"
257+
missing_license = filter_missing_list(missing_license)
247258
missing_license = [sub.replace(path_to_find, '', 1) for sub in missing_license]
248259

249260
# File list that missing copyright text
250261
missing_copyright = [str(sub) for sub in set(report.files_without_copyright)]
251262
if not path_to_find.endswith(f"{os.sep}"):
252263
path_to_find += f"{os.sep}"
264+
missing_copyright = filter_missing_list(missing_copyright)
253265
missing_copyright = [sub.replace(path_to_find, '', 1) for sub in missing_copyright]
266+
254267
except Exception as ex:
255268
dump_error_msg(f"Error prechecker lint: {ex}", True)
256269

@@ -259,6 +272,15 @@ def precheck_for_project(path_to_find):
259272
return missing_license, missing_copyright, oss_pkg_info_files, project, report
260273

261274

275+
def filter_missing_list(missing_list):
276+
filtered_list = []
277+
for file in missing_list:
278+
abs_path = os.path.abspath(file)
279+
if not any(os.path.commonpath([abs_path, path]) == path for path in user_exclude_list):
280+
filtered_list.append(file)
281+
return filtered_list
282+
283+
262284
def dump_error_msg(error_msg: str, exit=False):
263285
global error_items
264286
error_items.append(error_msg)
@@ -267,13 +289,14 @@ def dump_error_msg(error_msg: str, exit=False):
267289
sys.exit(1)
268290

269291

270-
def init(path_to_find, output_path, file_list, need_log_file=True):
292+
def init(path_to_find, output_path, file_list, need_log_file=True, exclude_path=[]):
271293
global logger, _result_log
294+
272295
if file_list:
273296
_result_log["File list to check"] = file_list
274297
path_to_find = file_list
275298
logger, _result_log = init_log(os.path.join(output_path, f"fosslight_log_pre_{_start_time}.txt"),
276-
need_log_file, logging.INFO, logging.DEBUG, PKG_NAME, path_to_find)
299+
need_log_file, logging.INFO, logging.DEBUG, PKG_NAME, path_to_find, exclude_path)
277300

278301

279302
def get_path_to_find(target_path, _check_only_file_mode):
@@ -305,7 +328,15 @@ def get_path_to_find(target_path, _check_only_file_mode):
305328
return path_to_find, file_to_check_list, _check_only_file_mode
306329

307330

308-
def run_lint(target_path, disable, output_file_name, format='', need_log_file=True):
331+
def set_exclude_list(path_to_find, exclude_path):
332+
global user_exclude_list
333+
334+
for path in exclude_path:
335+
if path.strip != "":
336+
user_exclude_list.append(os.path.abspath(os.path.join(path_to_find, path)))
337+
338+
339+
def run_lint(target_path, disable, output_file_name, format='', need_log_file=True, exclude_path=[]):
309340
global _turn_on_exclude_config, _check_only_file_mode, _start_time
310341

311342
file_to_check_list = []
@@ -322,9 +353,10 @@ def run_lint(target_path, disable, output_file_name, format='', need_log_file=Tr
322353
dump_error_msg(f"Error - locale : {ex}")
323354

324355
path_to_find, file_to_check_list, _check_only_file_mode = get_path_to_find(target_path, _check_only_file_mode)
356+
set_exclude_list(path_to_find, exclude_path)
325357

326358
result_file, output_path, output_extension = create_result_file(output_file_name, format, _start_time)
327-
init(path_to_find, output_path, file_to_check_list, need_log_file)
359+
init(path_to_find, output_path, file_to_check_list, need_log_file, exclude_path)
328360

329361
if os.path.isdir(path_to_find):
330362
oss_pkg_info = []
@@ -353,7 +385,8 @@ def run_lint(target_path, disable, output_file_name, format='', need_log_file=Tr
353385
_check_only_file_mode,
354386
file_to_check_list,
355387
error_items,
356-
DEFAULT_EXCLUDE_EXTENSION_FILES)
388+
DEFAULT_EXCLUDE_EXTENSION_FILES,
389+
user_exclude_list)
357390

358391
success, exit_code = write_result_file(result_file, output_extension, _exit_code,
359392
result_item, _result_log, project, path_to_find)

src/fosslight_prechecker/_result.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def __init__(self):
4141
self._files_without_cop = []
4242
self._os_info = ""
4343
self._path_to_analyze = ""
44+
self._path_to_exclude = ""
4445
self._python_ver = ""
4546
self._fl_prechecker_ver = ""
4647
self._log_msg = ""
@@ -79,6 +80,8 @@ def get_print_yaml(self):
7980
result_tool_item = {}
8081
result_tool_item["OS"] = self._os_info
8182
result_tool_item["Analyze path"] = self._path_to_analyze
83+
if self._path_to_exclude:
84+
result_tool_item["Exclude path"] = self._path_to_exclude
8285
result_tool_item["Python version"] = self._python_ver
8386
result_tool_item["fosslight_prechecker version"] = self._fl_prechecker_ver
8487
result_item["Tool Info"] = result_tool_item
@@ -264,10 +267,12 @@ def exclude_file_in_yaml(path_to_find, yaml_files, license_missing_files, copyri
264267
return license_missing_files, copyright_missing_files, abnormal_yaml_files
265268

266269

267-
def get_total_file_list(path_to_find, prj_report, exclude_files):
270+
def get_total_file_list(path_to_find, prj_report, exclude_files, user_exclude_files=[]):
268271
if not path_to_find.endswith(f'{os.path.sep}'):
269272
path_to_find += f'{os.path.sep}'
270-
total_files = [str(file_report.path).replace(path_to_find, '', 1) for file_report in prj_report.file_reports]
273+
total_files = [str(file_report.path).replace(path_to_find, '', 1) for file_report in prj_report.file_reports
274+
if not any(os.path.commonpath([os.path.abspath(file_report.path), user_exclude_file]) == user_exclude_file
275+
for user_exclude_file in user_exclude_files)]
271276
total_files_excluded = list(set(total_files) - set(exclude_files))
272277
return total_files_excluded
273278

@@ -292,8 +297,8 @@ def add_reason_to_file_name(oss_pkg_info_files, abnormal_yaml_files, path_to_fin
292297
return oss_pkg_info_files
293298

294299

295-
def result_for_summary(path_to_find, oss_pkg_info_files, license_missing_files, copyright_missing_files,
296-
prj_report, _result_log, _check_only_file_mode, file_to_check_list, error_items, exclude_files):
300+
def result_for_summary(path_to_find, oss_pkg_info_files, license_missing_files, copyright_missing_files, prj_report,
301+
_result_log, _check_only_file_mode, file_to_check_list, error_items, exclude_files, user_exclude_files):
297302
prechecker_compliant = False
298303
detected_lic = []
299304
missing_both_files = []
@@ -303,7 +308,7 @@ def result_for_summary(path_to_find, oss_pkg_info_files, license_missing_files,
303308
if _check_only_file_mode:
304309
file_total_num = len(file_to_check_list)
305310
else:
306-
file_total_num = len(get_total_file_list(path_to_find, prj_report, exclude_files))
311+
file_total_num = len(get_total_file_list(path_to_find, prj_report, exclude_files, user_exclude_files))
307312

308313
# Get detected License
309314
for i, lic in enumerate(sorted(prj_report.used_licenses)):
@@ -345,6 +350,7 @@ def result_for_summary(path_to_find, oss_pkg_info_files, license_missing_files,
345350
result_item._files_without_cop = sorted(copyright_missing_files)
346351
result_item._fl_prechecker_ver = _result_log["Tool Info"]
347352
result_item._path_to_analyze = _result_log["Path to analyze"]
353+
result_item._path_to_exclude = _result_log.get("Path to exclude", '')
348354
result_item._os_info = _result_log["OS"]
349355
result_item._python_ver = _result_log["Python version"]
350356
result_item._check_only_file_mode = _check_only_file_mode

src/fosslight_prechecker/cli.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
from fosslight_prechecker._precheck import run_lint
1414

1515

16-
def run_main(mode, path, output, format, no_log, disable, copyright, license, dl_url, parser):
16+
def run_main(mode, path, output, format, no_log, disable, copyright, license, dl_url, parser, exclude_path):
1717
if mode != "add" and (copyright != "" or license != ""):
1818
parser.print_help()
1919
sys.exit(1)
2020

2121
if mode == "lint":
22-
run_lint(path, disable, output, format, no_log)
22+
run_lint(path, disable, output, format, no_log, exclude_path)
2323
elif mode == "add":
2424
add_content(path, license, copyright, dl_url, output, no_log)
2525
elif mode == "convert":
@@ -41,6 +41,7 @@ def main():
4141
parser.add_argument('-l', '--license', help="License name to add(used in only 'add' mode)", type=str, dest='license', default="")
4242
parser.add_argument('-c', '--copyright', help="Copyright to add(used in only 'add' mode)", type=str, dest='copyright', default="")
4343
parser.add_argument('-u', '--dlurl', help="Download URL to add(used in only 'add' mode)", type=str, dest='dlurl', default="")
44+
parser.add_argument('-e', '--exclude', help='Path to exclude from checking', nargs='*', dest='exclude_path', default=[])
4445
parser.add_argument('--notice', help="Show OSS notice", action='store_true', required=False)
4546
try:
4647
args = parser.parse_args()
@@ -67,7 +68,7 @@ def main():
6768
print(f.read())
6869
else:
6970
run_main(args.mode, args.path, args.output, args.format,
70-
args.log, args.disable, args.copyright, args.license, args.dlurl, parser)
71+
args.log, args.disable, args.copyright, args.license, args.dlurl, parser, args.exclude_path)
7172

7273

7374
if __name__ == "__main__":

tox.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ filterwarnings = ignore::DeprecationWarning
2626
commands =
2727
fosslight_prechecker lint -p src -o "test_result\prechecker_result.yaml"
2828
fosslight_prechecker lint -p src -f yaml -o "test_result2\prechecker_result.yaml"
29+
fosslight_prechecker lint -p tests -e convert add\test_no_license.py -o "test_result3\prechecker_result.yaml"
2930
fosslight_prechecker convert -p "tests\convert"
3031
cmd /c del /s/q tests\add_result
3132
cmd /c del /s/q tests\add\LICENSES
@@ -39,6 +40,7 @@ commands =
3940
commands =
4041
fosslight_prechecker lint -p src/ -o "test_result/prechecker_result.yaml"
4142
fosslight_prechecker lint -p src/ -f yaml -o "test_result2/prechecker_result.yaml"
43+
fosslight_prechecker lint -p tests/ -e convert add/test_no_license.py -o "test_result3/prechecker_result.yaml"
4244
fosslight_prechecker convert -p "tests/convert"
4345
rm -rf tests/add_result
4446
rm -rf tests/add/LICENSES
@@ -56,6 +58,7 @@ commands =
5658
fosslight_prechecker -h
5759
fosslight_prechecker lint -p src/ -o "test_result/prechecker_result.yaml"
5860
fosslight_prechecker lint -p src/ -f yaml -o "test_result2/prechecker_result.yaml"
61+
fosslight_prechecker lint -p tests -e convert add/test_no_license.py -o "test_result3/prechecker_result.yaml"
5962
fosslight_prechecker convert -p tests/convert
6063
cp -r tests/add tests/add_result
6164
fosslight_prechecker add -p tests/add_result -c "2019-2021 LG Electronics Inc." -l "GPL-3.0-only"

0 commit comments

Comments
 (0)