diff options
Diffstat (limited to 'lib/python/qmk/cli/lint.py')
-rw-r--r-- | lib/python/qmk/cli/lint.py | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/lib/python/qmk/cli/lint.py b/lib/python/qmk/cli/lint.py index 6b077c250f..897a6c4c0d 100644 --- a/lib/python/qmk/cli/lint.py +++ b/lib/python/qmk/cli/lint.py @@ -10,6 +10,9 @@ from qmk.keyboard import keyboard_completer, list_keyboards from qmk.keymap import locate_keymap, list_keymaps from qmk.path import is_keyboard, keyboard from qmk.git import git_get_ignored_files +from qmk.c_parse import c_source_files + +CHIBIOS_CONF_CHECKS = ['chconf.h', 'halconf.h', 'mcuconf.h', 'board.h'] def _list_defaultish_keymaps(kb): @@ -25,6 +28,29 @@ def _list_defaultish_keymaps(kb): return keymaps +def _get_code_files(kb, km=None): + """Return potential keyboard/keymap code files + """ + search_path = locate_keymap(kb, km).parent if km else keyboard(kb) + + code_files = [] + for file in c_source_files([search_path]): + # Ignore keymaps when only globing keyboard files + if not km and 'keymaps' in file.parts: + continue + code_files.append(file) + + return code_files + + +def _has_license(file): + """Check file has a license header + """ + # Crude assumption that first line of license header is a comment + fline = open(file).readline().rstrip() + return fline.startswith(("/*", "//")) + + def _handle_json_errors(kb, info): """Convert any json errors into lint errors """ @@ -40,6 +66,15 @@ def _handle_json_errors(kb, info): return ok +def _chibios_conf_includenext_check(target): + """Check the ChibiOS conf.h for the correct inclusion of the next conf.h + """ + for i, line in enumerate(target.open()): + if f'#include_next "{target.name}"' in line: + return f'Found `#include_next "{target.name}"` on line {i} of {target}, should be `#include_next <{target.name}>` (use angle brackets, not quotes)' + return None + + def _rules_mk_assignment_only(kb): """Check the keyboard-level rules.mk to ensure it only has assignments. """ @@ -92,6 +127,17 @@ def keymap_check(kb, km): cli.log.error(f'{kb}/{km}: The file "{file}" should not exist!') ok = False + for file in _get_code_files(kb, km): + if not _has_license(file): + cli.log.error(f'{kb}/{km}: The file "{file}" does not have a license header!') + ok = False + + if file.name in CHIBIOS_CONF_CHECKS: + check_error = _chibios_conf_includenext_check(file) + if check_error is not None: + cli.log.error(f'{kb}/{km}: {check_error}') + ok = False + return ok @@ -119,6 +165,17 @@ def keyboard_check(kb): cli.log.error(f'{kb}: The file "{file}" should not exist!') ok = False + for file in _get_code_files(kb): + if not _has_license(file): + cli.log.error(f'{kb}: The file "{file}" does not have a license header!') + ok = False + + if file.name in CHIBIOS_CONF_CHECKS: + check_error = _chibios_conf_includenext_check(file) + if check_error is not None: + cli.log.error(f'{kb}: {check_error}') + ok = False + return ok |