Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix up license check path. #23571

Merged
merged 3 commits into from
Apr 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 49 additions & 34 deletions lib/python/qmk/cli/license_check.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Copyright 2023 Nick Brassel (@tzarc)
# SPDX-License-Identifier: GPL-2.0-or-later
import re
from pathlib import Path
from milc import cli
from qmk.constants import LICENSE_TEXTS
from qmk.path import normpath

L_PAREN = re.compile(r'\(\[\{\<')
R_PAREN = re.compile(r'\)\]\}\>')
Expand All @@ -27,7 +27,45 @@ def _simplify_text(input):
return ' '.join(lines)


def _detect_license_from_file_contents(filename, absolute=False):
def _preformat_license_texts():
# Pre-format all the licenses
for _, long_licenses in LICENSE_TEXTS:
for i in range(len(long_licenses)):
long_licenses[i] = _simplify_text(long_licenses[i])


def _determine_suffix_condition(extensions):
def _default_suffix_condition(s):
return s in SUFFIXES

conditional = _default_suffix_condition

if extensions is not None and len(extensions) > 0:
suffixes = [f'.{s}' if not s.startswith('.') else s for s in extensions]

def _specific_suffix_condition(s):
return s in suffixes

conditional = _specific_suffix_condition

return conditional


def _determine_file_list(inputs, conditional):
check_list = set()
for filename in inputs:
if filename.is_dir():
for file in sorted(filename.rglob('*')):
if file.is_file() and conditional(file.suffix):
check_list.add(file)
elif filename.is_file():
if conditional(filename.suffix):
check_list.add(filename)

return list(sorted(check_list))


def _detect_license_from_file_contents(filename, absolute=False, short=False):
data = filename.read_text(encoding='utf-8', errors='ignore')
filename_out = str(filename.absolute()) if absolute else str(filename)

Expand All @@ -42,13 +80,13 @@ def _detect_license_from_file_contents(filename, absolute=False):
break

if not found:
if cli.args.short:
if short:
print(f'{filename_out} UNKNOWN')
else:
cli.log.error(f'{{fg_cyan}}{filename_out}{{fg_reset}} -- unknown license, or no license detected!')
return False

if cli.args.short:
if short:
print(f'{filename_out} {license}')
else:
cli.log.info(f'{{fg_cyan}}{filename_out}{{fg_reset}} -- license detected: {license} (SPDX License Identifier)')
Expand All @@ -59,57 +97,34 @@ def _detect_license_from_file_contents(filename, absolute=False):
for short_license, long_licenses in LICENSE_TEXTS:
for long_license in long_licenses:
if long_license in simple_text:
if cli.args.short:
if short:
print(f'{filename_out} {short_license}')
else:
cli.log.info(f'{{fg_cyan}}{filename_out}{{fg_reset}} -- license detected: {short_license} (Full text)')
return True

if cli.args.short:
if short:
print(f'{filename_out} UNKNOWN')
else:
cli.log.error(f'{{fg_cyan}}{filename_out}{{fg_reset}} -- unknown license, or no license detected!')

return False


@cli.argument('inputs', nargs='*', arg_only=True, type=Path, help='List of input files or directories.')
@cli.argument('inputs', nargs='*', arg_only=True, type=normpath, help='List of input files or directories.')
@cli.argument('-s', '--short', action='store_true', help='Short output.')
@cli.argument('-a', '--absolute', action='store_true', help='Print absolute paths.')
@cli.argument('-e', '--extension', arg_only=True, action='append', default=[], help='Override list of extensions. Can be specified multiple times for multiple extensions.')
@cli.subcommand('File license check.', hidden=False if cli.config.user.developer else True)
def license_check(cli):
def _default_suffix_condition(s):
return s in SUFFIXES

conditional = _default_suffix_condition

if len(cli.args.extension) > 0:
suffixes = [f'.{s}' if not s.startswith('.') else s for s in cli.args.extension]

def _specific_suffix_condition(s):
return s in suffixes

conditional = _specific_suffix_condition
_preformat_license_texts()

# Pre-format all the licenses
for _, long_licenses in LICENSE_TEXTS:
for i in range(len(long_licenses)):
long_licenses[i] = _simplify_text(long_licenses[i])

check_list = set()
for filename in sorted(cli.args.inputs):
if filename.is_dir():
for file in sorted(filename.rglob('*')):
if file.is_file() and conditional(file.suffix):
check_list.add(file)
elif filename.is_file():
if conditional(filename.suffix):
check_list.add(filename)
conditional = _determine_suffix_condition(cli.args.extension)
check_list = _determine_file_list(cli.args.inputs, conditional)

failed = False
for filename in sorted(check_list):
if not _detect_license_from_file_contents(filename, absolute=cli.args.absolute):
if not _detect_license_from_file_contents(filename, absolute=cli.args.absolute, short=cli.args.short):
failed = True

if failed:
Expand Down
Loading