Skip to content

Commit 19a4a86

Browse files
committed
Import exclude path fct. from FL util
1 parent 0b6cc9f commit 19a4a86

File tree

4 files changed

+27
-30
lines changed

4 files changed

+27
-30
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==2.*
6+
fosslight_util>=2.1.10
77
jinja2

src/fosslight_prechecker/_add.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from fosslight_util.spdx_licenses import get_spdx_licenses_json, get_license_from_nick
1616
from fosslight_util.parsing_yaml import find_sbom_yaml_files, parsing_yml
1717
from fosslight_util.output_format import check_output_format
18+
from fosslight_util.exclude import excluding_files
1819
from datetime import datetime
1920
from fosslight_prechecker._precheck import precheck_for_project, precheck_for_files, dump_error_msg, \
2021
get_path_to_find, DEFAULT_EXCLUDE_EXTENSION_FILES
@@ -402,12 +403,14 @@ def add_content(
402403
input_copyright: str = "",
403404
input_dl_url: str = "",
404405
output_path: str = "",
405-
need_log_file: bool = True
406+
need_log_file: bool = True,
407+
exclude_path: list = []
406408
) -> None:
407409
global _result_log, spdx_licenses
408410
_check_only_file_mode = False
409411
file_to_check_list = []
410412
missing_license = []
413+
abs_path_to_exclude = []
411414

412415
path_to_find, file_to_check_list, _check_only_file_mode = get_path_to_find(target_path, _check_only_file_mode)
413416

@@ -417,6 +420,9 @@ def add_content(
417420
else:
418421
output_path = os.path.abspath(output_path)
419422

423+
user_exclude_path = excluding_files(exclude_path, path_to_find)
424+
abs_path_to_exclude = [os.path.abspath(path) for path in user_exclude_path]
425+
420426
now = datetime.now().strftime('%y%m%d_%H%M')
421427
logger, _result_log = init_log(os.path.join(output_path, f"fosslight_log_pre_{now}.txt"),
422428
need_log_file, logging.INFO, logging.DEBUG, PKG_NAME, path_to_find)
@@ -440,7 +446,6 @@ def add_content(
440446
except Exception as ex:
441447
dump_error_msg(f"Error access to get_spdx_licenses_json : {ex}")
442448

443-
# File Only mode (-f option)
444449
if _check_only_file_mode:
445450
main_parser = reuse_parser()
446451
missing_license_list, missing_copyright_list, project = precheck_for_files(path_to_find, file_to_check_list)
@@ -468,17 +473,15 @@ def add_content(
468473
# Add Download URL
469474
if input_dl_url:
470475
add_dl_url_into_file(main_parser, project, path_to_find, input_dl_url, file_to_check_list)
471-
472-
# Path mode (-p option)
473476
else:
474477
# Download license text file of OSS-pkg-info.yaml
475478
download_oss_info_license(path_to_find, input_license)
476479

477480
# Get missing license / copyright file list
478-
missing_license, missing_copyright, _, project, prj_report = precheck_for_project(path_to_find)
481+
missing_license, missing_copyright, _, project, prj_report = precheck_for_project(path_to_find, abs_path_to_exclude)
479482

480483
# Get total files except excluded file
481-
total_files_excluded = get_total_file_list(path_to_find, prj_report, DEFAULT_EXCLUDE_EXTENSION_FILES)
484+
total_files_excluded = get_total_file_list(path_to_find, prj_report, DEFAULT_EXCLUDE_EXTENSION_FILES, abs_path_to_exclude)
482485
skip_files = sorted(set(total_files_excluded) - set(missing_license) - set(missing_copyright))
483486
logger.info(f"\n# File list that have both license and copyright : {len(skip_files)} / {len(total_files_excluded)}")
484487

src/fosslight_prechecker/_precheck.py

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import fosslight_util.constant as constant
1717
from fosslight_util.set_log import init_log
1818
from fosslight_util.timer_thread import TimerThread
19+
from fosslight_util.exclude import excluding_files
1920
from reuse import report
2021
from reuse.project import Project
2122
from reuse.report import ProjectReport
@@ -26,7 +27,6 @@
2627
is_windows = platform.system() == 'Windows'
2728
REUSE_CONFIG_FILE = ".reuse/dep5"
2829
DEFAULT_EXCLUDE_EXTENSION_FILES = [] # Exclude files from reuse
29-
user_exclude_list = [] # Exclude paths from checking
3030
_turn_on_exclude_config = True
3131
_check_only_file_mode = False
3232
error_items = []
@@ -88,7 +88,7 @@ def exclude_git_related_files(path: str) -> None:
8888
logger.warning(f"Error to get git related files : {ex}")
8989

9090

91-
def find_oss_pkg_info_and_exclude_file(path: str) -> List[str]:
91+
def find_oss_pkg_info_and_exclude_file(path: str, abs_path_to_exclude: List) -> List[str]:
9292
global DEFAULT_EXCLUDE_EXTENSION_FILES
9393
oss_pkg_info = []
9494
git_present = shutil.which("git")
@@ -98,13 +98,13 @@ def find_oss_pkg_info_and_exclude_file(path: str) -> List[str]:
9898

9999
try:
100100
for root, dirs, files in os.walk(path):
101-
if os.path.abspath(root) in user_exclude_list:
101+
if os.path.abspath(root) in abs_path_to_exclude:
102102
continue
103103
for dir in dirs:
104104
# For hidden folders
105105
dir_path = os.path.join(root, dir)
106106
dir_abs_path = os.path.abspath(dir_path)
107-
if any(os.path.commonpath([dir_abs_path, exclude_path]) == exclude_path for exclude_path in user_exclude_list):
107+
if any(os.path.commonpath([dir_abs_path, exclude_path]) == exclude_path for exclude_path in abs_path_to_exclude):
108108
continue
109109
if dir.startswith("."):
110110
all_files_in_dir = [os.path.join(root, dir, file)
@@ -116,7 +116,7 @@ def find_oss_pkg_info_and_exclude_file(path: str) -> List[str]:
116116
for file in files:
117117
file_path = os.path.join(root, file)
118118
file_abs_path = os.path.abspath(file_path)
119-
if any(os.path.commonpath([file_abs_path, exclude_path]) == exclude_path for exclude_path in user_exclude_list):
119+
if any(os.path.commonpath([file_abs_path, exclude_path]) == exclude_path for exclude_path in abs_path_to_exclude):
120120
continue
121121
file_lower_case = file.lower()
122122
file_rel_path = os.path.relpath(file_path, path)
@@ -243,11 +243,11 @@ def precheck_for_files(path: str, files: List[str]) -> Tuple[List[str], List[str
243243
return missing_license_list, missing_copyright_list, prj
244244

245245

246-
def precheck_for_project(path_to_find: str) -> Tuple[List[str], List[str], List[str], Project, ProjectReport]:
246+
def precheck_for_project(path_to_find: str, abs_path_to_exclude: List) -> Tuple[List[str], List[str], List[str], Project, ProjectReport]:
247247
missing_license = []
248248
missing_copyright = []
249249

250-
oss_pkg_info_files = find_oss_pkg_info_and_exclude_file(path_to_find)
250+
oss_pkg_info_files = find_oss_pkg_info_and_exclude_file(path_to_find, abs_path_to_exclude)
251251
if _turn_on_exclude_config:
252252
need_rollback, temp_file_name, temp_dir_name = create_reuse_dep5_file(path_to_find)
253253

@@ -259,14 +259,14 @@ def precheck_for_project(path_to_find: str) -> Tuple[List[str], List[str], List[
259259
missing_license = [str(sub) for sub in set(report.files_without_licenses)]
260260
if not path_to_find.endswith(f"{os.sep}"):
261261
path_to_find += f"{os.sep}"
262-
missing_license = filter_missing_list(missing_license)
262+
missing_license = filter_missing_list(missing_license, abs_path_to_exclude)
263263
missing_license = [sub.replace(path_to_find, '', 1) for sub in missing_license]
264264

265265
# File list that missing copyright text
266266
missing_copyright = [str(sub) for sub in set(report.files_without_copyright)]
267267
if not path_to_find.endswith(f"{os.sep}"):
268268
path_to_find += f"{os.sep}"
269-
missing_copyright = filter_missing_list(missing_copyright)
269+
missing_copyright = filter_missing_list(missing_copyright, abs_path_to_exclude)
270270
missing_copyright = [sub.replace(path_to_find, '', 1) for sub in missing_copyright]
271271

272272
except Exception as ex:
@@ -277,11 +277,11 @@ def precheck_for_project(path_to_find: str) -> Tuple[List[str], List[str], List[
277277
return missing_license, missing_copyright, oss_pkg_info_files, project, report
278278

279279

280-
def filter_missing_list(missing_list: List[str]) -> List[str]:
280+
def filter_missing_list(missing_list: List[str], abs_path_to_exclude: List) -> List[str]:
281281
filtered_list = []
282282
for file in missing_list:
283283
abs_path = os.path.abspath(file)
284-
if not any(os.path.commonpath([abs_path, path]) == path for path in user_exclude_list):
284+
if not any(os.path.commonpath([abs_path, path]) == path for path in abs_path_to_exclude):
285285
filtered_list.append(file)
286286
return filtered_list
287287

@@ -339,14 +339,6 @@ def get_path_to_find(target_path: str, _check_only_file_mode: bool) -> Tuple[str
339339
return path_to_find, file_to_check_list, _check_only_file_mode
340340

341341

342-
def set_exclude_list(path_to_find: str, exclude_path: List[str]):
343-
global user_exclude_list
344-
345-
for path in exclude_path:
346-
if path.strip != "":
347-
user_exclude_list.append(os.path.abspath(os.path.join(path_to_find, path)))
348-
349-
350342
def run_lint(
351343
target_path: str,
352344
disable: bool,
@@ -357,6 +349,7 @@ def run_lint(
357349
) -> None:
358350
global _turn_on_exclude_config, _check_only_file_mode, _start_time
359351

352+
abs_path_to_exclude = []
360353
file_to_check_list = []
361354
_exit_code = 0
362355
path_to_find = ""
@@ -371,7 +364,8 @@ def run_lint(
371364
dump_error_msg(f"Error - locale : {ex}")
372365

373366
path_to_find, file_to_check_list, _check_only_file_mode = get_path_to_find(target_path, _check_only_file_mode)
374-
set_exclude_list(path_to_find, exclude_path)
367+
user_exclude_path = excluding_files(exclude_path, path_to_find)
368+
abs_path_to_exclude = [os.path.abspath(path) for path in user_exclude_path]
375369

376370
result_file, output_path, output_extension = create_result_file(output_file_name, format, _start_time)
377371
init(path_to_find, output_path, file_to_check_list, need_log_file, exclude_path)
@@ -389,7 +383,7 @@ def run_lint(
389383
if _check_only_file_mode:
390384
license_missing_files, copyright_missing_files, project = precheck_for_files(path_to_find, file_to_check_list)
391385
else:
392-
license_missing_files, copyright_missing_files, oss_pkg_info, project, report = precheck_for_project(path_to_find)
386+
license_missing_files, copyright_missing_files, oss_pkg_info, project, report = precheck_for_project(path_to_find, abs_path_to_exclude)
393387

394388
if need_log_file:
395389
timer.stop = True
@@ -404,7 +398,7 @@ def run_lint(
404398
file_to_check_list,
405399
error_items,
406400
DEFAULT_EXCLUDE_EXTENSION_FILES,
407-
user_exclude_list)
401+
abs_path_to_exclude)
408402

409403
success, exit_code = write_result_file(result_file, output_extension, _exit_code,
410404
result_item, _result_log, project, path_to_find)

src/fosslight_prechecker/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def run_main(mode: str, path, output, format, no_log, disable, copyright, licens
2121
if mode == "lint":
2222
run_lint(path, disable, output, format, no_log, exclude_path)
2323
elif mode == "add":
24-
add_content(path, license, copyright, dl_url, output, no_log)
24+
add_content(path, license, copyright, dl_url, output, no_log, exclude_path)
2525
elif mode == "convert":
2626
convert_report(path, output, format, no_log)
2727
else:

0 commit comments

Comments
 (0)