lint.py 2.5 KB

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