validate_aliases.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. """Validates the list of keyboard aliases.
  2. """
  3. from pathlib import Path
  4. from milc import cli
  5. from qmk.json_schema import json_load
  6. from qmk.keyboard import resolve_keyboard, keyboard_folder
  7. def _safe_keyboard_folder(target):
  8. try:
  9. return keyboard_folder(target) # throws ValueError if it's invalid
  10. except Exception:
  11. return None
  12. def _target_keyboard_exists(target):
  13. # If there's no target, then we can't build it.
  14. if not target:
  15. return False
  16. # If the target directory existed but there was no rules.mk or rules.mk was incorrectly parsed, then we can't build it.
  17. if not resolve_keyboard(target):
  18. return False
  19. # If the target directory exists but it itself has an invalid alias or invalid rules.mk, then we can't build it either.
  20. if not _safe_keyboard_folder(target):
  21. return False
  22. # As far as we can tell, we can build it!
  23. return True
  24. @cli.subcommand('Validates the list of keyboard aliases.', hidden=True)
  25. def ci_validate_aliases(cli):
  26. aliases = json_load(Path('data/mappings/keyboard_aliases.hjson'))
  27. success = True
  28. for alias in aliases.keys():
  29. target = aliases[alias].get('target', None)
  30. if not _target_keyboard_exists(target):
  31. cli.log.error(f'Keyboard alias {alias} has a target that doesn\'t exist: {target}')
  32. success = False
  33. return success