lint.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. """Command to look over a keyboard/keymap and check for common mistakes.
  2. """
  3. from pathlib import Path
  4. from milc import cli
  5. from qmk.decorators import automagic_keyboard, automagic_keymap
  6. from qmk.info import info_json
  7. from qmk.keyboard import keyboard_completer, list_keyboards
  8. from qmk.keymap import locate_keymap, list_keymaps
  9. from qmk.path import is_keyboard, keyboard
  10. from qmk.git import git_get_ignored_files
  11. def _list_defaultish_keymaps(kb):
  12. """Return default like keymaps for a given keyboard
  13. """
  14. defaultish = ['ansi', 'iso', 'via']
  15. keymaps = set()
  16. for x in list_keymaps(kb):
  17. if x in defaultish or x.startswith('default'):
  18. keymaps.add(x)
  19. return keymaps
  20. def _handle_json_errors(kb, info):
  21. """Convert any json errors into lint errors
  22. """
  23. ok = True
  24. # Check for errors in the json
  25. if info['parse_errors']:
  26. ok = False
  27. cli.log.error(f'{kb}: Errors found when generating info.json.')
  28. if cli.config.lint.strict and info['parse_warnings']:
  29. ok = False
  30. cli.log.error(f'{kb}: Warnings found when generating info.json (Strict mode enabled.)')
  31. return ok
  32. def _rules_mk_assignment_only(kb):
  33. """Check the keyboard-level rules.mk to ensure it only has assignments.
  34. """
  35. keyboard_path = keyboard(kb)
  36. current_path = Path()
  37. errors = []
  38. for path_part in keyboard_path.parts:
  39. current_path = current_path / path_part
  40. rules_mk = current_path / 'rules.mk'
  41. if rules_mk.exists():
  42. continuation = None
  43. for i, line in enumerate(rules_mk.open()):
  44. line = line.strip()
  45. if '#' in line:
  46. line = line[:line.index('#')]
  47. if continuation:
  48. line = continuation + line
  49. continuation = None
  50. if line:
  51. if line[-1] == '\\':
  52. continuation = line[:-1]
  53. continue
  54. if line and '=' not in line:
  55. errors.append(f'Non-assignment code on line +{i} {rules_mk}: {line}')
  56. return errors
  57. def keymap_check(kb, km):
  58. """Perform the keymap level checks.
  59. """
  60. ok = True
  61. keymap_path = locate_keymap(kb, km)
  62. if not keymap_path:
  63. ok = False
  64. cli.log.error("%s: Can't find %s keymap.", kb, km)
  65. return ok
  66. # Additional checks
  67. invalid_files = git_get_ignored_files(keymap_path.parent.as_posix())
  68. for file in invalid_files:
  69. cli.log.error(f'{kb}/{km}: The file "{file}" should not exist!')
  70. ok = False
  71. return ok
  72. def keyboard_check(kb):
  73. """Perform the keyboard level checks.
  74. """
  75. ok = True
  76. kb_info = info_json(kb)
  77. if not _handle_json_errors(kb, kb_info):
  78. ok = False
  79. # Additional checks
  80. rules_mk_assignment_errors = _rules_mk_assignment_only(kb)
  81. if rules_mk_assignment_errors:
  82. ok = False
  83. cli.log.error('%s: Non-assignment code found in rules.mk. Move it to post_rules.mk instead.', kb)
  84. for assignment_error in rules_mk_assignment_errors:
  85. cli.log.error(assignment_error)
  86. invalid_files = git_get_ignored_files(f'keyboards/{kb}/')
  87. for file in invalid_files:
  88. if 'keymap' in file:
  89. continue
  90. cli.log.error(f'{kb}: The file "{file}" should not exist!')
  91. ok = False
  92. return ok
  93. @cli.argument('--strict', action='store_true', help='Treat warnings as errors')
  94. @cli.argument('-kb', '--keyboard', completer=keyboard_completer, help='Comma separated list of keyboards to check')
  95. @cli.argument('-km', '--keymap', help='The keymap to check')
  96. @cli.argument('--all-kb', action='store_true', arg_only=True, help='Check all keyboards')
  97. @cli.argument('--all-km', action='store_true', arg_only=True, help='Check all keymaps')
  98. @cli.subcommand('Check keyboard and keymap for common mistakes.')
  99. @automagic_keyboard
  100. @automagic_keymap
  101. def lint(cli):
  102. """Check keyboard and keymap for common mistakes.
  103. """
  104. failed = []
  105. # Determine our keyboard list
  106. if cli.args.all_kb:
  107. if cli.args.keyboard:
  108. cli.log.warning('Both --all-kb and --keyboard passed, --all-kb takes precedence.')
  109. keyboard_list = list_keyboards()
  110. elif not cli.config.lint.keyboard:
  111. cli.log.error('Missing required arguments: --keyboard or --all-kb')
  112. cli.print_help()
  113. return False
  114. else:
  115. keyboard_list = cli.config.lint.keyboard.split(',')
  116. # Lint each keyboard
  117. for kb in keyboard_list:
  118. if not is_keyboard(kb):
  119. cli.log.error('No such keyboard: %s', kb)
  120. continue
  121. # Determine keymaps to also check
  122. if cli.args.all_km:
  123. keymaps = list_keymaps(kb)
  124. elif cli.config.lint.keymap:
  125. keymaps = {cli.config.lint.keymap}
  126. else:
  127. keymaps = _list_defaultish_keymaps(kb)
  128. # Ensure that at least a 'default' keymap always exists
  129. keymaps.add('default')
  130. ok = True
  131. # keyboard level checks
  132. if not keyboard_check(kb):
  133. ok = False
  134. # Keymap specific checks
  135. for keymap in keymaps:
  136. if not keymap_check(kb, keymap):
  137. ok = False
  138. # Report status
  139. if not ok:
  140. failed.append(kb)
  141. # Check and report the overall status
  142. if failed:
  143. cli.log.error('Lint check failed for: %s', ', '.join(failed))
  144. return False
  145. cli.log.info('Lint check passed!')
  146. return True