lint.py 2.4 KB

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