diff options
author | Erovia <Erovia@users.noreply.github.com> | 2020-12-21 13:29:36 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-21 13:29:36 +0100 |
commit | a380a26ad2fa4cfa580134be81772153b697a1ac (patch) | |
tree | a49c5b4bd9fb930be343b82d15ac04d6c3c632ba /lib/python/qmk/cli/doctor.py | |
parent | e3211e307ec14c326b03b39806ed7bb11b927d34 (diff) | |
download | qmk_firmware-a380a26ad2fa4cfa580134be81772153b697a1ac.tar.gz qmk_firmware-a380a26ad2fa4cfa580134be81772153b697a1ac.zip |
Split of the doctor codebase (#11255)
Co-authored-by: Ryan <fauxpark@gmail.com>
Diffstat (limited to 'lib/python/qmk/cli/doctor.py')
-rwxr-xr-x | lib/python/qmk/cli/doctor.py | 288 |
1 files changed, 3 insertions, 285 deletions
diff --git a/lib/python/qmk/cli/doctor.py b/lib/python/qmk/cli/doctor.py index ea1113c64..70f32911a 100755 --- a/lib/python/qmk/cli/doctor.py +++ b/lib/python/qmk/cli/doctor.py | |||
@@ -3,293 +3,13 @@ | |||
3 | Check out the user's QMK environment and make sure it's ready to compile. | 3 | Check out the user's QMK environment and make sure it's ready to compile. |
4 | """ | 4 | """ |
5 | import platform | 5 | import platform |
6 | import re | ||
7 | import shutil | ||
8 | import subprocess | ||
9 | from pathlib import Path | ||
10 | from enum import Enum | ||
11 | 6 | ||
12 | from milc import cli | 7 | from milc import cli |
13 | from milc.questions import yesno | 8 | from milc.questions import yesno |
14 | from qmk import submodules | 9 | from qmk import submodules |
15 | from qmk.constants import QMK_FIRMWARE | 10 | from qmk.constants import QMK_FIRMWARE |
16 | from qmk.commands import run | 11 | from qmk.commands import run |
17 | 12 | from qmk.os_helpers import CheckStatus, check_binaries, check_binary_versions, check_submodules, check_git_repo | |
18 | |||
19 | class CheckStatus(Enum): | ||
20 | OK = 1 | ||
21 | WARNING = 2 | ||
22 | ERROR = 3 | ||
23 | |||
24 | |||
25 | ESSENTIAL_BINARIES = { | ||
26 | 'dfu-programmer': {}, | ||
27 | 'avrdude': {}, | ||
28 | 'dfu-util': {}, | ||
29 | 'avr-gcc': { | ||
30 | 'version_arg': '-dumpversion' | ||
31 | }, | ||
32 | 'arm-none-eabi-gcc': { | ||
33 | 'version_arg': '-dumpversion' | ||
34 | }, | ||
35 | 'bin/qmk': {}, | ||
36 | } | ||
37 | |||
38 | |||
39 | def _udev_rule(vid, pid=None, *args): | ||
40 | """ Helper function that return udev rules | ||
41 | """ | ||
42 | rule = "" | ||
43 | if pid: | ||
44 | rule = 'SUBSYSTEMS=="usb", ATTRS{idVendor}=="%s", ATTRS{idProduct}=="%s", TAG+="uaccess"' % ( | ||
45 | vid, | ||
46 | pid, | ||
47 | ) | ||
48 | else: | ||
49 | rule = 'SUBSYSTEMS=="usb", ATTRS{idVendor}=="%s", TAG+="uaccess"' % vid | ||
50 | if args: | ||
51 | rule = ', '.join([rule, *args]) | ||
52 | return rule | ||
53 | |||
54 | |||
55 | def _deprecated_udev_rule(vid, pid=None): | ||
56 | """ Helper function that return udev rules | ||
57 | |||
58 | Note: these are no longer the recommended rules, this is just used to check for them | ||
59 | """ | ||
60 | if pid: | ||
61 | return 'SUBSYSTEMS=="usb", ATTRS{idVendor}=="%s", ATTRS{idProduct}=="%s", MODE:="0666"' % (vid, pid) | ||
62 | else: | ||
63 | return 'SUBSYSTEMS=="usb", ATTRS{idVendor}=="%s", MODE:="0666"' % vid | ||
64 | |||
65 | |||
66 | def parse_gcc_version(version): | ||
67 | m = re.match(r"(\d+)(?:\.(\d+))?(?:\.(\d+))?", version) | ||
68 | |||
69 | return { | ||
70 | 'major': int(m.group(1)), | ||
71 | 'minor': int(m.group(2)) if m.group(2) else 0, | ||
72 | 'patch': int(m.group(3)) if m.group(3) else 0, | ||
73 | } | ||
74 | |||
75 | |||
76 | def check_arm_gcc_version(): | ||
77 | """Returns True if the arm-none-eabi-gcc version is not known to cause problems. | ||
78 | """ | ||
79 | if 'output' in ESSENTIAL_BINARIES['arm-none-eabi-gcc']: | ||
80 | version_number = ESSENTIAL_BINARIES['arm-none-eabi-gcc']['output'].strip() | ||
81 | cli.log.info('Found arm-none-eabi-gcc version %s', version_number) | ||
82 | |||
83 | return CheckStatus.OK # Right now all known arm versions are ok | ||
84 | |||
85 | |||
86 | def check_avr_gcc_version(): | ||
87 | """Returns True if the avr-gcc version is not known to cause problems. | ||
88 | """ | ||
89 | rc = CheckStatus.ERROR | ||
90 | if 'output' in ESSENTIAL_BINARIES['avr-gcc']: | ||
91 | version_number = ESSENTIAL_BINARIES['avr-gcc']['output'].strip() | ||
92 | |||
93 | cli.log.info('Found avr-gcc version %s', version_number) | ||
94 | rc = CheckStatus.OK | ||
95 | |||
96 | parsed_version = parse_gcc_version(version_number) | ||
97 | if parsed_version['major'] > 8: | ||
98 | cli.log.warning('{fg_yellow}We do not recommend avr-gcc newer than 8. Downgrading to 8.x is recommended.') | ||
99 | rc = CheckStatus.WARNING | ||
100 | |||
101 | return rc | ||
102 | |||
103 | |||
104 | def check_avrdude_version(): | ||
105 | if 'output' in ESSENTIAL_BINARIES['avrdude']: | ||
106 | last_line = ESSENTIAL_BINARIES['avrdude']['output'].split('\n')[-2] | ||
107 | version_number = last_line.split()[2][:-1] | ||
108 | cli.log.info('Found avrdude version %s', version_number) | ||
109 | |||
110 | return CheckStatus.OK | ||
111 | |||
112 | |||
113 | def check_dfu_util_version(): | ||
114 | if 'output' in ESSENTIAL_BINARIES['dfu-util']: | ||
115 | first_line = ESSENTIAL_BINARIES['dfu-util']['output'].split('\n')[0] | ||
116 | version_number = first_line.split()[1] | ||
117 | cli.log.info('Found dfu-util version %s', version_number) | ||
118 | |||
119 | return CheckStatus.OK | ||
120 | |||
121 | |||
122 | def check_dfu_programmer_version(): | ||
123 | if 'output' in ESSENTIAL_BINARIES['dfu-programmer']: | ||
124 | first_line = ESSENTIAL_BINARIES['dfu-programmer']['output'].split('\n')[0] | ||
125 | version_number = first_line.split()[1] | ||
126 | cli.log.info('Found dfu-programmer version %s', version_number) | ||
127 | |||
128 | return CheckStatus.OK | ||
129 | |||
130 | |||
131 | def check_binaries(): | ||
132 | """Iterates through ESSENTIAL_BINARIES and tests them. | ||
133 | """ | ||
134 | ok = True | ||
135 | |||
136 | for binary in sorted(ESSENTIAL_BINARIES): | ||
137 | if not is_executable(binary): | ||
138 | ok = False | ||
139 | |||
140 | return ok | ||
141 | |||
142 | |||
143 | def check_submodules(): | ||
144 | """Iterates through all submodules to make sure they're cloned and up to date. | ||
145 | """ | ||
146 | for submodule in submodules.status().values(): | ||
147 | if submodule['status'] is None: | ||
148 | cli.log.error('Submodule %s has not yet been cloned!', submodule['name']) | ||
149 | return CheckStatus.ERROR | ||
150 | elif not submodule['status']: | ||
151 | cli.log.warning('Submodule %s is not up to date!', submodule['name']) | ||
152 | return CheckStatus.WARNING | ||
153 | |||
154 | return CheckStatus.OK | ||
155 | |||
156 | |||
157 | def check_git_repo(): | ||
158 | """Checks that the .git directory exists inside QMK_HOME. | ||
159 | |||
160 | This is a decent enough indicator that the qmk_firmware directory is a | ||
161 | proper Git repository, rather than a .zip download from GitHub. | ||
162 | """ | ||
163 | dot_git_dir = QMK_FIRMWARE / '.git' | ||
164 | |||
165 | return CheckStatus.OK if dot_git_dir.is_dir() else CheckStatus.WARNING | ||
166 | |||
167 | |||
168 | def check_udev_rules(): | ||
169 | """Make sure the udev rules look good. | ||
170 | """ | ||
171 | rc = CheckStatus.OK | ||
172 | udev_dir = Path("/etc/udev/rules.d/") | ||
173 | desired_rules = { | ||
174 | 'atmel-dfu': { | ||
175 | _udev_rule("03eb", "2fef"), # ATmega16U2 | ||
176 | _udev_rule("03eb", "2ff0"), # ATmega32U2 | ||
177 | _udev_rule("03eb", "2ff3"), # ATmega16U4 | ||
178 | _udev_rule("03eb", "2ff4"), # ATmega32U4 | ||
179 | _udev_rule("03eb", "2ff9"), # AT90USB64 | ||
180 | _udev_rule("03eb", "2ffb") # AT90USB128 | ||
181 | }, | ||
182 | 'kiibohd': {_udev_rule("1c11", "b007")}, | ||
183 | 'stm32': { | ||
184 | _udev_rule("1eaf", "0003"), # STM32duino | ||
185 | _udev_rule("0483", "df11") # STM32 DFU | ||
186 | }, | ||
187 | 'bootloadhid': {_udev_rule("16c0", "05df")}, | ||
188 | 'usbasploader': {_udev_rule("16c0", "05dc")}, | ||
189 | 'massdrop': {_udev_rule("03eb", "6124", 'ENV{ID_MM_DEVICE_IGNORE}="1"')}, | ||
190 | 'caterina': { | ||
191 | # Spark Fun Electronics | ||
192 | _udev_rule("1b4f", "9203", 'ENV{ID_MM_DEVICE_IGNORE}="1"'), # Pro Micro 3V3/8MHz | ||
193 | _udev_rule("1b4f", "9205", 'ENV{ID_MM_DEVICE_IGNORE}="1"'), # Pro Micro 5V/16MHz | ||
194 | _udev_rule("1b4f", "9207", 'ENV{ID_MM_DEVICE_IGNORE}="1"'), # LilyPad 3V3/8MHz (and some Pro Micro clones) | ||
195 | # Pololu EleCTRONICS | ||
196 | _udev_rule("1ffb", "0101", 'ENV{ID_MM_DEVICE_IGNORE}="1"'), # A-Star 32U4 | ||
197 | # Arduino SA | ||
198 | _udev_rule("2341", "0036", 'ENV{ID_MM_DEVICE_IGNORE}="1"'), # Leonardo | ||
199 | _udev_rule("2341", "0037", 'ENV{ID_MM_DEVICE_IGNORE}="1"'), # Micro | ||
200 | # Adafruit INDUSTRIES llC | ||
201 | _udev_rule("239a", "000c", 'ENV{ID_MM_DEVICE_IGNORE}="1"'), # Feather 32U4 | ||
202 | _udev_rule("239a", "000d", 'ENV{ID_MM_DEVICE_IGNORE}="1"'), # ItsyBitsy 32U4 3V3/8MHz | ||
203 | _udev_rule("239a", "000e", 'ENV{ID_MM_DEVICE_IGNORE}="1"'), # ItsyBitsy 32U4 5V/16MHz | ||
204 | # dog hunter ag | ||
205 | _udev_rule("2a03", "0036", 'ENV{ID_MM_DEVICE_IGNORE}="1"'), # Leonardo | ||
206 | _udev_rule("2a03", "0037", 'ENV{ID_MM_DEVICE_IGNORE}="1"') # Micro | ||
207 | } | ||
208 | } | ||
209 | |||
210 | # These rules are no longer recommended, only use them to check for their presence. | ||
211 | deprecated_rules = { | ||
212 | 'atmel-dfu': {_deprecated_udev_rule("03eb", "2ff4"), _deprecated_udev_rule("03eb", "2ffb"), _deprecated_udev_rule("03eb", "2ff0")}, | ||
213 | 'kiibohd': {_deprecated_udev_rule("1c11")}, | ||
214 | 'stm32': {_deprecated_udev_rule("1eaf", "0003"), _deprecated_udev_rule("0483", "df11")}, | ||
215 | 'bootloadhid': {_deprecated_udev_rule("16c0", "05df")}, | ||
216 | 'caterina': {'ATTRS{idVendor}=="2a03", ENV{ID_MM_DEVICE_IGNORE}="1"', 'ATTRS{idVendor}=="2341", ENV{ID_MM_DEVICE_IGNORE}="1"'}, | ||
217 | 'tmk': {_deprecated_udev_rule("feed")} | ||
218 | } | ||
219 | |||
220 | if udev_dir.exists(): | ||
221 | udev_rules = [rule_file for rule_file in udev_dir.glob('*.rules')] | ||
222 | current_rules = set() | ||
223 | |||
224 | # Collect all rules from the config files | ||
225 | for rule_file in udev_rules: | ||
226 | for line in rule_file.read_text().split('\n'): | ||
227 | line = line.strip() | ||
228 | if not line.startswith("#") and len(line): | ||
229 | current_rules.add(line) | ||
230 | |||
231 | # Check if the desired rules are among the currently present rules | ||
232 | for bootloader, rules in desired_rules.items(): | ||
233 | if not rules.issubset(current_rules): | ||
234 | deprecated_rule = deprecated_rules.get(bootloader) | ||
235 | if deprecated_rule and deprecated_rule.issubset(current_rules): | ||
236 | 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) | ||
237 | else: | ||
238 | # For caterina, check if ModemManager is running | ||
239 | if bootloader == "caterina": | ||
240 | if check_modem_manager(): | ||
241 | rc = CheckStatus.WARNING | ||
242 | 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.") | ||
243 | rc = CheckStatus.WARNING | ||
244 | 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) | ||
245 | |||
246 | else: | ||
247 | cli.log.warning("{fg_yellow}'%s' does not exist. Skipping udev rule checking...", udev_dir) | ||
248 | |||
249 | return rc | ||
250 | |||
251 | |||
252 | def check_systemd(): | ||
253 | """Check if it's a systemd system | ||
254 | """ | ||
255 | return bool(shutil.which("systemctl")) | ||
256 | |||
257 | |||
258 | def check_modem_manager(): | ||
259 | """Returns True if ModemManager is running. | ||
260 | |||
261 | """ | ||
262 | if check_systemd(): | ||
263 | mm_check = run(["systemctl", "--quiet", "is-active", "ModemManager.service"], timeout=10) | ||
264 | if mm_check.returncode == 0: | ||
265 | return True | ||
266 | else: | ||
267 | """(TODO): Add check for non-systemd systems | ||
268 | """ | ||
269 | return False | ||
270 | |||
271 | |||
272 | def is_executable(command): | ||
273 | """Returns True if command exists and can be executed. | ||
274 | """ | ||
275 | # Make sure the command is in the path. | ||
276 | res = shutil.which(command) | ||
277 | if res is None: | ||
278 | cli.log.error("{fg_red}Can't find %s in your path.", command) | ||
279 | return False | ||
280 | |||
281 | # Make sure the command can be executed | ||
282 | version_arg = ESSENTIAL_BINARIES[command].get('version_arg', '--version') | ||
283 | check = run([command, version_arg], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, timeout=5, universal_newlines=True) | ||
284 | |||
285 | ESSENTIAL_BINARIES[command]['output'] = check.stdout | ||
286 | |||
287 | if check.returncode in [0, 1]: # Older versions of dfu-programmer exit 1 | ||
288 | cli.log.debug('Found {fg_cyan}%s', command) | ||
289 | return True | ||
290 | |||
291 | cli.log.error("{fg_red}Can't run `%s %s`", command, version_arg) | ||
292 | return False | ||
293 | 13 | ||
294 | 14 | ||
295 | def os_tests(): | 15 | def os_tests(): |
@@ -312,6 +32,7 @@ def os_test_linux(): | |||
312 | """Run the Linux specific tests. | 32 | """Run the Linux specific tests. |
313 | """ | 33 | """ |
314 | cli.log.info("Detected {fg_cyan}Linux.") | 34 | cli.log.info("Detected {fg_cyan}Linux.") |
35 | from qmk.os_helpers.linux import check_udev_rules | ||
315 | 36 | ||
316 | return check_udev_rules() | 37 | return check_udev_rules() |
317 | 38 | ||
@@ -370,10 +91,7 @@ def doctor(cli): | |||
370 | status = CheckStatus.ERROR | 91 | status = CheckStatus.ERROR |
371 | 92 | ||
372 | # Make sure the tools are at the correct version | 93 | # Make sure the tools are at the correct version |
373 | ver_ok = [] | 94 | ver_ok = check_binary_versions() |
374 | for check in (check_arm_gcc_version, check_avr_gcc_version, check_avrdude_version, check_dfu_util_version, check_dfu_programmer_version): | ||
375 | ver_ok.append(check()) | ||
376 | |||
377 | if CheckStatus.ERROR in ver_ok: | 95 | if CheckStatus.ERROR in ver_ok: |
378 | status = CheckStatus.ERROR | 96 | status = CheckStatus.ERROR |
379 | elif CheckStatus.WARNING in ver_ok and status == CheckStatus.OK: | 97 | elif CheckStatus.WARNING in ver_ok and status == CheckStatus.OK: |