aboutsummaryrefslogtreecommitdiff
path: root/lib/python/qmk/cli/ci/validate_aliases.py
blob: 4f2fe6c9412c50739d2055b7f22512af79f31272 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""Validates the list of keyboard aliases.
"""
from milc import cli

from qmk.keyboard import keyboard_folder, keyboard_alias_definitions


def _safe_keyboard_folder(target):
    try:
        return keyboard_folder(target)  # throws ValueError if it's invalid
    except Exception:
        return None


def _target_keyboard_exists(target):
    # If there's no target, then we can't build it.
    if not target:
        return False

    # If the target directory exists but it itself has an invalid alias or invalid rules.mk, then we can't build it either.
    if not _safe_keyboard_folder(target):
        return False

    # As far as we can tell, we can build it!
    return True


def _alias_not_self(alias):
    """Check if alias points to itself, either directly or within a circular reference
    """
    aliases = keyboard_alias_definitions()

    found = set()
    while alias in aliases:
        found.add(alias)
        alias = aliases[alias].get('target', alias)
        if alias in found:
            return False

    return True


@cli.subcommand('Validates the list of keyboard aliases.', hidden=True)
def ci_validate_aliases(cli):
    aliases = keyboard_alias_definitions()

    success = True
    for alias in aliases.keys():
        target = aliases[alias].get('target', None)
        if not _alias_not_self(alias):
            cli.log.error(f'Keyboard alias {alias} should not point to itself')
            success = False

        elif not _target_keyboard_exists(target):
            cli.log.error(f'Keyboard alias {alias} has a target that doesn\'t exist: {target}')
            success = False

    return success