validate_aliases.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. """Validates the list of keyboard aliases.
  2. """
  3. from milc import cli
  4. from qmk.keyboard import keyboard_folder, keyboard_alias_definitions
  5. def _safe_keyboard_folder(target):
  6. try:
  7. return keyboard_folder(target) # throws ValueError if it's invalid
  8. except Exception:
  9. return None
  10. def _target_keyboard_exists(target):
  11. # If there's no target, then we can't build it.
  12. if not target:
  13. return False
  14. # If the target directory exists but it itself has an invalid alias or invalid rules.mk, then we can't build it either.
  15. if not _safe_keyboard_folder(target):
  16. return False
  17. # As far as we can tell, we can build it!
  18. return True
  19. def _alias_not_self(alias):
  20. """Check if alias points to itself, either directly or within a circular reference
  21. """
  22. aliases = keyboard_alias_definitions()
  23. found = set()
  24. while alias in aliases:
  25. found.add(alias)
  26. alias = aliases[alias].get('target', alias)
  27. if alias in found:
  28. return False
  29. return True
  30. @cli.subcommand('Validates the list of keyboard aliases.', hidden=True)
  31. def ci_validate_aliases(cli):
  32. aliases = keyboard_alias_definitions()
  33. success = True
  34. for alias in aliases.keys():
  35. target = aliases[alias].get('target', None)
  36. if not _alias_not_self(alias):
  37. cli.log.error(f'Keyboard alias {alias} should not point to itself')
  38. success = False
  39. elif not _target_keyboard_exists(target):
  40. cli.log.error(f'Keyboard alias {alias} has a target that doesn\'t exist: {target}')
  41. success = False
  42. return success