Skip to content

Commit d619317

Browse files
authored
Merge pull request #129 from fosslight/develop
Modify to check file extension efficiently
2 parents 5c76c35 + a5d9433 commit d619317

File tree

4 files changed

+33
-50
lines changed

4 files changed

+33
-50
lines changed

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ binaryornot
33
requests
44
reuse
55
PyYAML
6-
fosslight_util>=1.4.7
6+
fosslight_util>=1.4.14

src/fosslight_prechecker/_add.py

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
from fosslight_util.parsing_yaml import find_sbom_yaml_files, parsing_yml
1616
from fosslight_util.output_format import check_output_format
1717
from datetime import datetime
18-
from fosslight_prechecker._precheck import precheck_for_project, precheck_for_files, dump_error_msg, get_path_to_find
18+
from fosslight_prechecker._precheck import precheck_for_project, precheck_for_files, dump_error_msg, \
19+
get_path_to_find, DEFAULT_EXCLUDE_EXTENSION_FILES
20+
from fosslight_prechecker._result import get_total_file_list
1921
from reuse.header import run as reuse_header
2022
from reuse.download import run as reuse_download
2123
from reuse._comment import EXTENSION_COMMENT_STYLE_MAP_LOWERCASE
@@ -36,33 +38,6 @@
3638
logger = logging.getLogger(constant.LOGGER_NAME)
3739

3840

39-
def check_file_extension(file_list, is_all_file_list=False):
40-
if file_list != "":
41-
for file in file_list:
42-
try:
43-
file_extension = os.path.splitext(file)[1].lower()
44-
if file_extension == "" and is_all_file_list:
45-
logger.info(f" No extension file : {file}")
46-
if file_extension in EXTENSION_COMMENT_STYLE_MAP_LOWERCASE:
47-
yield file
48-
except Exception as ex:
49-
dump_error_msg(f"Error - Unknown error to check file extension: {ex}")
50-
51-
52-
def check_license_and_copyright(path_to_find, all_files, missing_license, missing_copyright):
53-
# Check file extension for each list
54-
all_files_filtered = list(check_file_extension(all_files, True))
55-
missing_license_filtered = list(check_file_extension(missing_license))
56-
missing_copyright_filtered = list(check_file_extension(missing_copyright))
57-
58-
skip_files = sorted(set(all_files_filtered) - set(missing_license_filtered) - set(missing_copyright_filtered))
59-
logger.info(f"\n# File list that have both license and copyright : {len(skip_files)} / {len(all_files_filtered)}")
60-
61-
precheck_for_files(path_to_find, skip_files)
62-
63-
return missing_license_filtered, missing_copyright_filtered
64-
65-
6641
def convert_to_spdx_style(input_string):
6742
input_string = input_string.replace(" ", "-")
6843
input_converted = f"LicenseRef-{input_string}"
@@ -444,26 +419,31 @@ def add_content(target_path="", input_license="", input_copyright="", output_pat
444419
# Download license text file of OSS-pkg-info.yaml
445420
download_oss_info_license(path_to_find, input_license)
446421

447-
# Get all files List in path
448-
all_files_list = get_allfiles_list(path_to_find)
449-
450422
# Get missing license / copyright file list
451-
missing_license, missing_copyright, _, project, _ = precheck_for_project(path_to_find, 'add')
423+
missing_license, missing_copyright, _, project, prj_report = precheck_for_project(path_to_find)
452424

453-
# Print Skipped Files
454-
missing_license_filtered, missing_copyright_filtered = \
455-
check_license_and_copyright(path_to_find, all_files_list, missing_license, missing_copyright)
425+
# Get total files except excluded file
426+
total_files_excluded = get_total_file_list(path_to_find, prj_report, DEFAULT_EXCLUDE_EXTENSION_FILES)
427+
skip_files = sorted(set(total_files_excluded) - set(missing_license) - set(missing_copyright))
428+
logger.info(f"\n# File list that have both license and copyright : {len(skip_files)} / {len(total_files_excluded)}")
429+
430+
# Filter by file extension
431+
missing_license = [file for file in missing_license if os.path.splitext(file)[1].lower() in EXTENSION_COMMENT_STYLE_MAP_LOWERCASE]
432+
missing_copyright = [file for file in missing_copyright if os.path.splitext(file)[1].lower() in EXTENSION_COMMENT_STYLE_MAP_LOWERCASE]
433+
434+
# Check license and copyright of each file
435+
precheck_for_files(path_to_find, skip_files)
456436

457437
# Set missing license and copyright
458-
set_missing_license_copyright(missing_license_filtered,
459-
missing_copyright_filtered,
438+
set_missing_license_copyright(missing_license,
439+
missing_copyright,
460440
project,
461441
path_to_find,
462442
input_license,
463443
input_copyright)
464444

465445
# Find and create representative license file
466-
if input_license != "":
446+
if input_license != "" and len(missing_license) > 0:
467447
find_representative_license(path_to_find, input_license)
468448

469449
save_result_log()

src/fosslight_prechecker/_precheck.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ def exclude_git_related_files(path):
8888
logger.error(f"Error to get git related files : {ex}")
8989

9090

91-
def find_oss_pkg_info_and_exlcude_file(path, mode='lint'):
91+
def find_oss_pkg_info_and_exlcude_file(path):
9292
global DEFAULT_EXCLUDE_EXTENSION_FILES
9393
oss_pkg_info = []
9494
git_present = shutil.which("git")
9595

96-
if mode == 'lint' and _turn_on_exclude_config and git_present and VCSStrategyGit.in_repo(path):
96+
if _turn_on_exclude_config and git_present and VCSStrategyGit.in_repo(path):
9797
exclude_git_related_files(path)
9898

9999
try:
@@ -230,11 +230,11 @@ def precheck_for_files(path, files):
230230
return missing_license_list, missing_copyright_list, prj
231231

232232

233-
def precheck_for_project(path_to_find, mode='lint'):
233+
def precheck_for_project(path_to_find):
234234
missing_license = []
235235
missing_copyright = []
236236

237-
oss_pkg_info_files = find_oss_pkg_info_and_exlcude_file(path_to_find, mode)
237+
oss_pkg_info_files = find_oss_pkg_info_and_exlcude_file(path_to_find)
238238
if _turn_on_exclude_config:
239239
need_rollback, temp_file_name, temp_dir_name = create_reuse_dep5_file(path_to_find)
240240

@@ -340,7 +340,7 @@ def run_lint(target_path, disable, output_file_name, format='', need_log_file=Tr
340340
if _check_only_file_mode:
341341
license_missing_files, copyright_missing_files, project = precheck_for_files(path_to_find, file_to_check_list)
342342
else:
343-
license_missing_files, copyright_missing_files, oss_pkg_info, project, report = precheck_for_project(path_to_find, 'lint')
343+
license_missing_files, copyright_missing_files, oss_pkg_info, project, report = precheck_for_project(path_to_find)
344344

345345
if need_log_file:
346346
timer.stop = True

src/fosslight_prechecker/_result.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -266,22 +266,25 @@ def exclude_file_in_yaml(path_to_find, yaml_files, license_missing_files, copyri
266266
return license_missing_files, copyright_missing_files
267267

268268

269+
def get_total_file_list(path_to_find, prj_report, exclude_files):
270+
if not path_to_find.endswith('/'):
271+
path_to_find += '/'
272+
total_files = [str(file_report.path).replace(path_to_find, '', 1) for file_report in prj_report.file_reports]
273+
total_files_excluded = list(set(total_files) - set(exclude_files))
274+
return total_files_excluded
275+
276+
269277
def result_for_summary(path_to_find, oss_pkg_info_files, license_missing_files, copyright_missing_files,
270278
prj_report, _result_log, _check_only_file_mode, file_to_check_list, error_items, exclude_files):
271279
prechecker_compliant = False
272280
detected_lic = []
273281
missing_both_files = []
274282
file_total_num = ""
275-
total_files_exclude = ()
276283

277284
if _check_only_file_mode:
278285
file_total_num = len(file_to_check_list)
279286
else:
280-
if not path_to_find.endswith('/'):
281-
path_to_find += '/'
282-
total_files = [str(file_report.path).replace(path_to_find, '', 1) for file_report in prj_report.file_reports]
283-
total_files_exclude = set(total_files) - set(exclude_files)
284-
file_total_num = len(total_files_exclude)
287+
file_total_num = len(get_total_file_list(path_to_find, prj_report, exclude_files))
285288

286289
# Get detected License
287290
for i, lic in enumerate(sorted(prj_report.used_licenses)):

0 commit comments

Comments
 (0)