Add check for non-assignment code in rules.mk (#12108)
* Add check for non-assignment code in rules.mk * fix lint check * fix lint * fixup to reflect the final state of #8422 * fix lintqmk_compile_improvements
parent
f155865804
commit
566d598516
@ -1,72 +1,129 @@ |
|||||||
"""Command to look over a keyboard/keymap and check for common mistakes. |
"""Command to look over a keyboard/keymap and check for common mistakes. |
||||||
""" |
""" |
||||||
|
from pathlib import Path |
||||||
|
|
||||||
from milc import cli |
from milc import cli |
||||||
|
|
||||||
from qmk.decorators import automagic_keyboard, automagic_keymap |
from qmk.decorators import automagic_keyboard, automagic_keymap |
||||||
from qmk.info import info_json |
from qmk.info import info_json |
||||||
from qmk.keyboard import find_readme, keyboard_completer |
from qmk.keyboard import keyboard_completer, list_keyboards |
||||||
from qmk.keymap import locate_keymap |
from qmk.keymap import locate_keymap |
||||||
from qmk.path import is_keyboard, keyboard |
from qmk.path import is_keyboard, keyboard |
||||||
|
|
||||||
|
|
||||||
|
def keymap_check(kb, km): |
||||||
|
"""Perform the keymap level checks. |
||||||
|
""" |
||||||
|
ok = True |
||||||
|
keymap_path = locate_keymap(kb, km) |
||||||
|
|
||||||
|
if not keymap_path: |
||||||
|
ok = False |
||||||
|
cli.log.error("%s: Can't find %s keymap.", kb, km) |
||||||
|
|
||||||
|
return ok |
||||||
|
|
||||||
|
|
||||||
|
def rules_mk_assignment_only(keyboard_path): |
||||||
|
"""Check the keyboard-level rules.mk to ensure it only has assignments. |
||||||
|
""" |
||||||
|
current_path = Path() |
||||||
|
errors = [] |
||||||
|
|
||||||
|
for path_part in keyboard_path.parts: |
||||||
|
current_path = current_path / path_part |
||||||
|
rules_mk = current_path / 'rules.mk' |
||||||
|
|
||||||
|
if rules_mk.exists(): |
||||||
|
continuation = None |
||||||
|
|
||||||
|
for i, line in enumerate(rules_mk.open()): |
||||||
|
line = line.strip() |
||||||
|
|
||||||
|
if '#' in line: |
||||||
|
line = line[:line.index('#')] |
||||||
|
|
||||||
|
if continuation: |
||||||
|
line = continuation + line |
||||||
|
continuation = None |
||||||
|
|
||||||
|
if line: |
||||||
|
if line[-1] == '\\': |
||||||
|
continuation = line[:-1] |
||||||
|
continue |
||||||
|
|
||||||
|
if line and '=' not in line: |
||||||
|
errors.append(f'Non-assignment code on line +{i} {rules_mk}: {line}') |
||||||
|
|
||||||
|
return errors |
||||||
|
|
||||||
|
|
||||||
@cli.argument('--strict', action='store_true', help='Treat warnings as errors.') |
@cli.argument('--strict', action='store_true', help='Treat warnings as errors.') |
||||||
@cli.argument('-kb', '--keyboard', completer=keyboard_completer, help='The keyboard to check.') |
@cli.argument('-kb', '--keyboard', completer=keyboard_completer, help='The keyboard to check.') |
||||||
@cli.argument('-km', '--keymap', help='The keymap to check.') |
@cli.argument('-km', '--keymap', help='The keymap to check.') |
||||||
|
@cli.argument('--all-kb', action='store_true', arg_only=True, help='Check all keyboards.') |
||||||
@cli.subcommand('Check keyboard and keymap for common mistakes.') |
@cli.subcommand('Check keyboard and keymap for common mistakes.') |
||||||
@automagic_keyboard |
@automagic_keyboard |
||||||
@automagic_keymap |
@automagic_keymap |
||||||
def lint(cli): |
def lint(cli): |
||||||
"""Check keyboard and keymap for common mistakes. |
"""Check keyboard and keymap for common mistakes. |
||||||
""" |
""" |
||||||
if not cli.config.lint.keyboard: |
failed = [] |
||||||
cli.log.error('Missing required argument: --keyboard') |
|
||||||
cli.print_help() |
|
||||||
return False |
|
||||||
|
|
||||||
if not is_keyboard(cli.config.lint.keyboard): |
# Determine our keyboard list |
||||||
cli.log.error('No such keyboard: %s', cli.config.lint.keyboard) |
if cli.args.all_kb: |
||||||
return False |
if cli.args.keyboard: |
||||||
|
cli.log.warning('Both --all-kb and --keyboard passed, --all-kb takes presidence.') |
||||||
|
|
||||||
# Gather data about the keyboard. |
keyboard_list = list_keyboards() |
||||||
ok = True |
elif not cli.config.lint.keyboard: |
||||||
keyboard_path = keyboard(cli.config.lint.keyboard) |
cli.log.error('Missing required arguments: --keyboard or --all-kb') |
||||||
keyboard_info = info_json(cli.config.lint.keyboard) |
cli.print_help() |
||||||
readme_path = find_readme(cli.config.lint.keyboard) |
return False |
||||||
missing_readme_path = keyboard_path / 'readme.md' |
else: |
||||||
|
keyboard_list = cli.args.keyboard.split(',') |
||||||
|
|
||||||
# Check for errors in the info.json |
# Lint each keyboard |
||||||
if keyboard_info['parse_errors']: |
for kb in keyboard_list: |
||||||
ok = False |
if not is_keyboard(kb): |
||||||
cli.log.error('Errors found when generating info.json.') |
cli.log.error('No such keyboard: %s', kb) |
||||||
|
continue |
||||||
|
|
||||||
if cli.config.lint.strict and keyboard_info['parse_warnings']: |
# Gather data about the keyboard. |
||||||
ok = False |
ok = True |
||||||
cli.log.error('Warnings found when generating info.json (Strict mode enabled.)') |
keyboard_path = keyboard(kb) |
||||||
|
keyboard_info = info_json(kb) |
||||||
|
|
||||||
# Check for a readme.md and warn if it doesn't exist |
# Check for errors in the info.json |
||||||
if not readme_path: |
if keyboard_info['parse_errors']: |
||||||
ok = False |
ok = False |
||||||
cli.log.error('Missing %s', missing_readme_path) |
cli.log.error('%s: Errors found when generating info.json.', kb) |
||||||
|
|
||||||
# Keymap specific checks |
if cli.config.lint.strict and keyboard_info['parse_warnings']: |
||||||
if cli.config.lint.keymap: |
ok = False |
||||||
keymap_path = locate_keymap(cli.config.lint.keyboard, cli.config.lint.keymap) |
cli.log.error('%s: Warnings found when generating info.json (Strict mode enabled.)', kb) |
||||||
|
|
||||||
if not keymap_path: |
# Check the rules.mk file(s) |
||||||
|
rules_mk_assignment_errors = rules_mk_assignment_only(keyboard_path) |
||||||
|
if rules_mk_assignment_errors: |
||||||
ok = False |
ok = False |
||||||
cli.log.error("Can't find %s keymap for %s keyboard.", cli.config.lint.keymap, cli.config.lint.keyboard) |
cli.log.error('%s: Non-assignment code found in rules.mk. Move it to post_rules.mk instead.', kb) |
||||||
else: |
for assignment_error in rules_mk_assignment_errors: |
||||||
keymap_readme = keymap_path.parent / 'readme.md' |
cli.log.error(assignment_error) |
||||||
if not keymap_readme.exists(): |
|
||||||
cli.log.warning('Missing %s', keymap_readme) |
|
||||||
|
|
||||||
if cli.config.lint.strict: |
# Keymap specific checks |
||||||
ok = False |
if cli.config.lint.keymap: |
||||||
|
if not keymap_check(kb, cli.config.lint.keymap): |
||||||
|
ok = False |
||||||
|
|
||||||
|
# Report status |
||||||
|
if not ok: |
||||||
|
failed.append(kb) |
||||||
|
|
||||||
# Check and report the overall status |
# Check and report the overall status |
||||||
if ok: |
if failed: |
||||||
cli.log.info('Lint check passed!') |
cli.log.error('Lint check failed for: %s', ', '.join(failed)) |
||||||
return True |
return False |
||||||
|
|
||||||
cli.log.error('Lint check failed!') |
cli.log.info('Lint check passed!') |
||||||
return False |
return True |
||||||
|
Loading…
Reference in new issue