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