lint.py 6.5 KB

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