diff options
| author | Zach White <skullydazed@gmail.com> | 2021-06-22 11:50:53 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-06-22 19:50:53 +0100 |
| commit | e87d23164522371b0c9560e81f36ed08caadc0ff (patch) | |
| tree | 481bff93f77418ac7603d8ad4af492b4c86a02c2 /lib/python/qmk/os_helpers | |
| parent | d61e5c0027e289ccf48652afa4c442342e7ccf04 (diff) | |
| download | qmk_firmware-e87d23164522371b0c9560e81f36ed08caadc0ff.tar.gz qmk_firmware-e87d23164522371b0c9560e81f36ed08caadc0ff.zip | |
Refactor doctor.py into a directory (#13298)
Diffstat (limited to 'lib/python/qmk/os_helpers')
| -rw-r--r-- | lib/python/qmk/os_helpers/__init__.py | 164 | ||||
| -rw-r--r-- | lib/python/qmk/os_helpers/linux/__init__.py | 140 |
2 files changed, 0 insertions, 304 deletions
diff --git a/lib/python/qmk/os_helpers/__init__.py b/lib/python/qmk/os_helpers/__init__.py deleted file mode 100644 index 3e98db3c3..000000000 --- a/lib/python/qmk/os_helpers/__init__.py +++ /dev/null | |||
| @@ -1,164 +0,0 @@ | |||
| 1 | """OS-agnostic helper functions | ||
| 2 | """ | ||
| 3 | from enum import Enum | ||
| 4 | import re | ||
| 5 | import shutil | ||
| 6 | from subprocess import DEVNULL | ||
| 7 | |||
| 8 | from milc import cli | ||
| 9 | from qmk import submodules | ||
| 10 | from qmk.constants import QMK_FIRMWARE | ||
| 11 | |||
| 12 | |||
| 13 | class CheckStatus(Enum): | ||
| 14 | OK = 1 | ||
| 15 | WARNING = 2 | ||
| 16 | ERROR = 3 | ||
| 17 | |||
| 18 | |||
| 19 | ESSENTIAL_BINARIES = { | ||
| 20 | 'dfu-programmer': {}, | ||
| 21 | 'avrdude': {}, | ||
| 22 | 'dfu-util': {}, | ||
| 23 | 'avr-gcc': { | ||
| 24 | 'version_arg': '-dumpversion' | ||
| 25 | }, | ||
| 26 | 'arm-none-eabi-gcc': { | ||
| 27 | 'version_arg': '-dumpversion' | ||
| 28 | }, | ||
| 29 | 'bin/qmk': {}, | ||
| 30 | } | ||
| 31 | |||
| 32 | |||
| 33 | def parse_gcc_version(version): | ||
| 34 | m = re.match(r"(\d+)(?:\.(\d+))?(?:\.(\d+))?", version) | ||
| 35 | |||
| 36 | return { | ||
| 37 | 'major': int(m.group(1)), | ||
| 38 | 'minor': int(m.group(2)) if m.group(2) else 0, | ||
| 39 | 'patch': int(m.group(3)) if m.group(3) else 0, | ||
| 40 | } | ||
| 41 | |||
| 42 | |||
| 43 | def check_arm_gcc_version(): | ||
| 44 | """Returns True if the arm-none-eabi-gcc version is not known to cause problems. | ||
| 45 | """ | ||
| 46 | if 'output' in ESSENTIAL_BINARIES['arm-none-eabi-gcc']: | ||
| 47 | version_number = ESSENTIAL_BINARIES['arm-none-eabi-gcc']['output'].strip() | ||
| 48 | cli.log.info('Found arm-none-eabi-gcc version %s', version_number) | ||
| 49 | |||
| 50 | return CheckStatus.OK # Right now all known arm versions are ok | ||
| 51 | |||
| 52 | |||
| 53 | def check_avr_gcc_version(): | ||
| 54 | """Returns True if the avr-gcc version is not known to cause problems. | ||
| 55 | """ | ||
| 56 | rc = CheckStatus.ERROR | ||
| 57 | if 'output' in ESSENTIAL_BINARIES['avr-gcc']: | ||
| 58 | version_number = ESSENTIAL_BINARIES['avr-gcc']['output'].strip() | ||
| 59 | |||
| 60 | cli.log.info('Found avr-gcc version %s', version_number) | ||
| 61 | rc = CheckStatus.OK | ||
| 62 | |||
| 63 | parsed_version = parse_gcc_version(version_number) | ||
| 64 | if parsed_version['major'] > 8: | ||
| 65 | cli.log.warning('{fg_yellow}We do not recommend avr-gcc newer than 8. Downgrading to 8.x is recommended.') | ||
| 66 | rc = CheckStatus.WARNING | ||
| 67 | |||
| 68 | return rc | ||
| 69 | |||
| 70 | |||
| 71 | def check_avrdude_version(): | ||
| 72 | if 'output' in ESSENTIAL_BINARIES['avrdude']: | ||
| 73 | last_line = ESSENTIAL_BINARIES['avrdude']['output'].split('\n')[-2] | ||
| 74 | version_number = last_line.split()[2][:-1] | ||
| 75 | cli.log.info('Found avrdude version %s', version_number) | ||
| 76 | |||
| 77 | return CheckStatus.OK | ||
| 78 | |||
| 79 | |||
| 80 | def check_dfu_util_version(): | ||
| 81 | if 'output' in ESSENTIAL_BINARIES['dfu-util']: | ||
| 82 | first_line = ESSENTIAL_BINARIES['dfu-util']['output'].split('\n')[0] | ||
| 83 | version_number = first_line.split()[1] | ||
| 84 | cli.log.info('Found dfu-util version %s', version_number) | ||
| 85 | |||
| 86 | return CheckStatus.OK | ||
| 87 | |||
| 88 | |||
| 89 | def check_dfu_programmer_version(): | ||
| 90 | if 'output' in ESSENTIAL_BINARIES['dfu-programmer']: | ||
| 91 | first_line = ESSENTIAL_BINARIES['dfu-programmer']['output'].split('\n')[0] | ||
| 92 | version_number = first_line.split()[1] | ||
| 93 | cli.log.info('Found dfu-programmer version %s', version_number) | ||
| 94 | |||
| 95 | return CheckStatus.OK | ||
| 96 | |||
| 97 | |||
| 98 | def check_binaries(): | ||
| 99 | """Iterates through ESSENTIAL_BINARIES and tests them. | ||
| 100 | """ | ||
| 101 | ok = True | ||
| 102 | |||
| 103 | for binary in sorted(ESSENTIAL_BINARIES): | ||
| 104 | if not is_executable(binary): | ||
| 105 | ok = False | ||
| 106 | |||
| 107 | return ok | ||
| 108 | |||
| 109 | |||
| 110 | def check_binary_versions(): | ||
| 111 | """Check the versions of ESSENTIAL_BINARIES | ||
| 112 | """ | ||
| 113 | versions = [] | ||
| 114 | for check in (check_arm_gcc_version, check_avr_gcc_version, check_avrdude_version, check_dfu_util_version, check_dfu_programmer_version): | ||
| 115 | versions.append(check()) | ||
| 116 | return versions | ||
| 117 | |||
| 118 | |||
| 119 | def check_submodules(): | ||
| 120 | """Iterates through all submodules to make sure they're cloned and up to date. | ||
| 121 | """ | ||
| 122 | for submodule in submodules.status().values(): | ||
| 123 | if submodule['status'] is None: | ||
| 124 | cli.log.error('Submodule %s has not yet been cloned!', submodule['name']) | ||
| 125 | return CheckStatus.ERROR | ||
| 126 | elif not submodule['status']: | ||
| 127 | cli.log.warning('Submodule %s is not up to date!', submodule['name']) | ||
| 128 | return CheckStatus.WARNING | ||
| 129 | |||
| 130 | return CheckStatus.OK | ||
| 131 | |||
| 132 | |||
| 133 | def is_executable(command): | ||
| 134 | """Returns True if command exists and can be executed. | ||
| 135 | """ | ||
| 136 | # Make sure the command is in the path. | ||
| 137 | res = shutil.which(command) | ||
| 138 | if res is None: | ||
| 139 | cli.log.error("{fg_red}Can't find %s in your path.", command) | ||
| 140 | return False | ||
| 141 | |||
| 142 | # Make sure the command can be executed | ||
| 143 | version_arg = ESSENTIAL_BINARIES[command].get('version_arg', '--version') | ||
| 144 | check = cli.run([command, version_arg], combined_output=True, stdin=DEVNULL, timeout=5) | ||
| 145 | |||
| 146 | ESSENTIAL_BINARIES[command]['output'] = check.stdout | ||
| 147 | |||
| 148 | if check.returncode in [0, 1]: # Older versions of dfu-programmer exit 1 | ||
| 149 | cli.log.debug('Found {fg_cyan}%s', command) | ||
| 150 | return True | ||
| 151 | |||
| 152 | cli.log.error("{fg_red}Can't run `%s %s`", command, version_arg) | ||
| 153 | return False | ||
| 154 | |||
| 155 | |||
| 156 | def check_git_repo(): | ||
| 157 | """Checks that the .git directory exists inside QMK_HOME. | ||
| 158 | |||
| 159 | This is a decent enough indicator that the qmk_firmware directory is a | ||
| 160 | proper Git repository, rather than a .zip download from GitHub. | ||
| 161 | """ | ||
| 162 | dot_git_dir = QMK_FIRMWARE / '.git' | ||
| 163 | |||
| 164 | return CheckStatus.OK if dot_git_dir.is_dir() else CheckStatus.WARNING | ||
diff --git a/lib/python/qmk/os_helpers/linux/__init__.py b/lib/python/qmk/os_helpers/linux/__init__.py deleted file mode 100644 index 008654ab0..000000000 --- a/lib/python/qmk/os_helpers/linux/__init__.py +++ /dev/null | |||
| @@ -1,140 +0,0 @@ | |||
| 1 | """OS-specific functions for: Linux | ||
| 2 | """ | ||
| 3 | from pathlib import Path | ||
| 4 | import shutil | ||
| 5 | |||
| 6 | from milc import cli | ||
| 7 | from qmk.constants import QMK_FIRMWARE | ||
| 8 | from qmk.os_helpers import CheckStatus | ||
| 9 | |||
| 10 | |||
| 11 | def _udev_rule(vid, pid=None, *args): | ||
| 12 | """ Helper function that return udev rules | ||
| 13 | """ | ||
| 14 | rule = "" | ||
| 15 | if pid: | ||
| 16 | rule = 'SUBSYSTEMS=="usb", ATTRS{idVendor}=="%s", ATTRS{idProduct}=="%s", TAG+="uaccess"' % ( | ||
| 17 | vid, | ||
| 18 | pid, | ||
| 19 | ) | ||
| 20 | else: | ||
| 21 | rule = 'SUBSYSTEMS=="usb", ATTRS{idVendor}=="%s", TAG+="uaccess"' % vid | ||
| 22 | if args: | ||
| 23 | rule = ', '.join([rule, *args]) | ||
| 24 | return rule | ||
| 25 | |||
| 26 | |||
| 27 | def _deprecated_udev_rule(vid, pid=None): | ||
| 28 | """ Helper function that return udev rules | ||
| 29 | |||
| 30 | Note: these are no longer the recommended rules, this is just used to check for them | ||
| 31 | """ | ||
| 32 | if pid: | ||
| 33 | return 'SUBSYSTEMS=="usb", ATTRS{idVendor}=="%s", ATTRS{idProduct}=="%s", MODE:="0666"' % (vid, pid) | ||
| 34 | else: | ||
| 35 | return 'SUBSYSTEMS=="usb", ATTRS{idVendor}=="%s", MODE:="0666"' % vid | ||
| 36 | |||
| 37 | |||
| 38 | def check_udev_rules(): | ||
| 39 | """Make sure the udev rules look good. | ||
| 40 | """ | ||
| 41 | rc = CheckStatus.OK | ||
| 42 | udev_dir = Path("/etc/udev/rules.d/") | ||
| 43 | desired_rules = { | ||
| 44 | 'atmel-dfu': { | ||
| 45 | _udev_rule("03eb", "2fef"), # ATmega16U2 | ||
| 46 | _udev_rule("03eb", "2ff0"), # ATmega32U2 | ||
| 47 | _udev_rule("03eb", "2ff3"), # ATmega16U4 | ||
| 48 | _udev_rule("03eb", "2ff4"), # ATmega32U4 | ||
| 49 | _udev_rule("03eb", "2ff9"), # AT90USB64 | ||
| 50 | _udev_rule("03eb", "2ffa"), # AT90USB162 | ||
| 51 | _udev_rule("03eb", "2ffb") # AT90USB128 | ||
| 52 | }, | ||
| 53 | 'kiibohd': {_udev_rule("1c11", "b007")}, | ||
| 54 | 'stm32': { | ||
| 55 | _udev_rule("1eaf", "0003"), # STM32duino | ||
| 56 | _udev_rule("0483", "df11") # STM32 DFU | ||
| 57 | }, | ||
| 58 | 'bootloadhid': {_udev_rule("16c0", "05df")}, | ||
| 59 | 'usbasploader': {_udev_rule("16c0", "05dc")}, | ||
| 60 | 'massdrop': {_udev_rule("03eb", "6124", 'ENV{ID_MM_DEVICE_IGNORE}="1"')}, | ||
| 61 | 'caterina': { | ||
| 62 | # Spark Fun Electronics | ||
| 63 | _udev_rule("1b4f", "9203", 'ENV{ID_MM_DEVICE_IGNORE}="1"'), # Pro Micro 3V3/8MHz | ||
| 64 | _udev_rule("1b4f", "9205", 'ENV{ID_MM_DEVICE_IGNORE}="1"'), # Pro Micro 5V/16MHz | ||
| 65 | _udev_rule("1b4f", "9207", 'ENV{ID_MM_DEVICE_IGNORE}="1"'), # LilyPad 3V3/8MHz (and some Pro Micro clones) | ||
| 66 | # Pololu Electronics | ||
| 67 | _udev_rule("1ffb", "0101", 'ENV{ID_MM_DEVICE_IGNORE}="1"'), # A-Star 32U4 | ||
| 68 | # Arduino SA | ||
| 69 | _udev_rule("2341", "0036", 'ENV{ID_MM_DEVICE_IGNORE}="1"'), # Leonardo | ||
| 70 | _udev_rule("2341", "0037", 'ENV{ID_MM_DEVICE_IGNORE}="1"'), # Micro | ||
| 71 | # Adafruit Industries LLC | ||
| 72 | _udev_rule("239a", "000c", 'ENV{ID_MM_DEVICE_IGNORE}="1"'), # Feather 32U4 | ||
| 73 | _udev_rule("239a", "000d", 'ENV{ID_MM_DEVICE_IGNORE}="1"'), # ItsyBitsy 32U4 3V3/8MHz | ||
| 74 | _udev_rule("239a", "000e", 'ENV{ID_MM_DEVICE_IGNORE}="1"'), # ItsyBitsy 32U4 5V/16MHz | ||
| 75 | # dog hunter AG | ||
| 76 | _udev_rule("2a03", "0036", 'ENV{ID_MM_DEVICE_IGNORE}="1"'), # Leonardo | ||
| 77 | _udev_rule("2a03", "0037", 'ENV{ID_MM_DEVICE_IGNORE}="1"') # Micro | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 81 | # These rules are no longer recommended, only use them to check for their presence. | ||
| 82 | deprecated_rules = { | ||
| 83 | 'atmel-dfu': {_deprecated_udev_rule("03eb", "2ff4"), _deprecated_udev_rule("03eb", "2ffb"), _deprecated_udev_rule("03eb", "2ff0")}, | ||
| 84 | 'kiibohd': {_deprecated_udev_rule("1c11")}, | ||
| 85 | 'stm32': {_deprecated_udev_rule("1eaf", "0003"), _deprecated_udev_rule("0483", "df11")}, | ||
| 86 | 'bootloadhid': {_deprecated_udev_rule("16c0", "05df")}, | ||
| 87 | 'caterina': {'ATTRS{idVendor}=="2a03", ENV{ID_MM_DEVICE_IGNORE}="1"', 'ATTRS{idVendor}=="2341", ENV{ID_MM_DEVICE_IGNORE}="1"'}, | ||
| 88 | 'tmk': {_deprecated_udev_rule("feed")} | ||
| 89 | } | ||
| 90 | |||
| 91 | if udev_dir.exists(): | ||
| 92 | udev_rules = [rule_file for rule_file in udev_dir.glob('*.rules')] | ||
| 93 | current_rules = set() | ||
| 94 | |||
| 95 | # Collect all rules from the config files | ||
| 96 | for rule_file in udev_rules: | ||
| 97 | for line in rule_file.read_text(encoding='utf-8').split('\n'): | ||
| 98 | line = line.strip() | ||
| 99 | if not line.startswith("#") and len(line): | ||
| 100 | current_rules.add(line) | ||
| 101 | |||
| 102 | # Check if the desired rules are among the currently present rules | ||
| 103 | for bootloader, rules in desired_rules.items(): | ||
| 104 | if not rules.issubset(current_rules): | ||
| 105 | deprecated_rule = deprecated_rules.get(bootloader) | ||
| 106 | if deprecated_rule and deprecated_rule.issubset(current_rules): | ||
| 107 | cli.log.warning("{fg_yellow}Found old, deprecated udev rules for '%s' boards. The new rules on https://docs.qmk.fm/#/faq_build?id=linux-udev-rules offer better security with the same functionality.", bootloader) | ||
| 108 | else: | ||
| 109 | # For caterina, check if ModemManager is running | ||
| 110 | if bootloader == "caterina": | ||
| 111 | if check_modem_manager(): | ||
| 112 | rc = CheckStatus.WARNING | ||
| 113 | cli.log.warning("{fg_yellow}Detected ModemManager without the necessary udev rules. Please either disable it or set the appropriate udev rules if you are using a Pro Micro.") | ||
| 114 | rc = CheckStatus.WARNING | ||
| 115 | cli.log.warning("{fg_yellow}Missing or outdated udev rules for '%s' boards. Run 'sudo cp %s/util/udev/50-qmk.rules /etc/udev/rules.d/'.", bootloader, QMK_FIRMWARE) | ||
| 116 | |||
| 117 | else: | ||
| 118 | cli.log.warning("{fg_yellow}'%s' does not exist. Skipping udev rule checking...", udev_dir) | ||
| 119 | |||
| 120 | return rc | ||
| 121 | |||
| 122 | |||
| 123 | def check_systemd(): | ||
| 124 | """Check if it's a systemd system | ||
| 125 | """ | ||
| 126 | return bool(shutil.which("systemctl")) | ||
| 127 | |||
| 128 | |||
| 129 | def check_modem_manager(): | ||
| 130 | """Returns True if ModemManager is running. | ||
| 131 | |||
| 132 | """ | ||
| 133 | if check_systemd(): | ||
| 134 | mm_check = cli.run(["systemctl", "--quiet", "is-active", "ModemManager.service"], timeout=10) | ||
| 135 | if mm_check.returncode == 0: | ||
| 136 | return True | ||
| 137 | else: | ||
| 138 | """(TODO): Add check for non-systemd systems | ||
| 139 | """ | ||
| 140 | return False | ||
