diff options
Diffstat (limited to 'lib/python/qmk/cli/lint.py')
| -rw-r--r-- | lib/python/qmk/cli/lint.py | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/lib/python/qmk/cli/lint.py b/lib/python/qmk/cli/lint.py new file mode 100644 index 000000000..74467021e --- /dev/null +++ b/lib/python/qmk/cli/lint.py | |||
| @@ -0,0 +1,70 @@ | |||
| 1 | """Command to look over a keyboard/keymap and check for common mistakes. | ||
| 2 | """ | ||
| 3 | from milc import cli | ||
| 4 | |||
| 5 | from qmk.decorators import automagic_keyboard, automagic_keymap | ||
| 6 | from qmk.info import info_json | ||
| 7 | from qmk.keymap import locate_keymap | ||
| 8 | from qmk.path import is_keyboard, keyboard | ||
| 9 | |||
| 10 | |||
| 11 | @cli.argument('--strict', action='store_true', help='Treat warnings as errors.') | ||
| 12 | @cli.argument('-kb', '--keyboard', help='The keyboard to check.') | ||
| 13 | @cli.argument('-km', '--keymap', help='The keymap to check.') | ||
| 14 | @cli.subcommand('Check keyboard and keymap for common mistakes.') | ||
| 15 | @automagic_keyboard | ||
| 16 | @automagic_keymap | ||
| 17 | def lint(cli): | ||
| 18 | """Check keyboard and keymap for common mistakes. | ||
| 19 | """ | ||
| 20 | if not cli.config.lint.keyboard: | ||
| 21 | cli.log.error('Missing required argument: --keyboard') | ||
| 22 | cli.print_help() | ||
| 23 | return False | ||
| 24 | |||
| 25 | if not is_keyboard(cli.config.lint.keyboard): | ||
| 26 | cli.log.error('No such keyboard: %s', cli.config.lint.keyboard) | ||
| 27 | return False | ||
| 28 | |||
| 29 | # Gather data about the keyboard. | ||
| 30 | ok = True | ||
| 31 | keyboard_path = keyboard(cli.config.lint.keyboard) | ||
| 32 | keyboard_info = info_json(cli.config.lint.keyboard) | ||
| 33 | readme_path = keyboard_path / 'readme.md' | ||
| 34 | |||
| 35 | # Check for errors in the info.json | ||
| 36 | if keyboard_info['parse_errors']: | ||
| 37 | ok = False | ||
| 38 | cli.log.error('Errors found when generating info.json.') | ||
| 39 | |||
| 40 | if cli.config.lint.strict and keyboard_info['parse_warnings']: | ||
| 41 | ok = False | ||
| 42 | cli.log.error('Warnings found when generating info.json (Strict mode enabled.)') | ||
| 43 | |||
| 44 | # Check for a readme.md and warn if it doesn't exist | ||
| 45 | if not readme_path.exists(): | ||
| 46 | ok = False | ||
| 47 | cli.log.error('Missing %s', readme_path) | ||
| 48 | |||
| 49 | # Keymap specific checks | ||
| 50 | if cli.config.lint.keymap: | ||
| 51 | keymap_path = locate_keymap(cli.config.lint.keyboard, cli.config.lint.keymap) | ||
| 52 | |||
| 53 | if not keymap_path: | ||
| 54 | ok = False | ||
| 55 | cli.log.error("Can't find %s keymap for %s keyboard.", cli.config.lint.keymap, cli.config.lint.keyboard) | ||
| 56 | else: | ||
| 57 | keymap_readme = keymap_path.parent / 'readme.md' | ||
| 58 | if not keymap_readme.exists(): | ||
| 59 | cli.log.warning('Missing %s', keymap_readme) | ||
| 60 | |||
| 61 | if cli.config.lint.strict: | ||
| 62 | ok = False | ||
| 63 | |||
| 64 | # Check and report the overall status | ||
| 65 | if ok: | ||
| 66 | cli.log.info('Lint check passed!') | ||
| 67 | return True | ||
| 68 | |||
| 69 | cli.log.error('Lint check failed!') | ||
| 70 | return False | ||
