diff options
author | skullydazed <skullydazed@users.noreply.github.com> | 2020-01-24 11:31:16 -0800 |
---|---|---|
committer | Erovia <Erovia@users.noreply.github.com> | 2020-01-24 20:31:16 +0100 |
commit | 5e65af3a76252bea6556d26add3061dea4918cc2 (patch) | |
tree | f44089f3f95e53e1b67d34db3c922a04b0a942f7 /lib/python/qmk/cli/doctor.py | |
parent | e4a0f841e19b4ddf86711cf79dc521d2f6f5e4ae (diff) | |
download | qmk_firmware-5e65af3a76252bea6556d26add3061dea4918cc2.tar.gz qmk_firmware-5e65af3a76252bea6556d26add3061dea4918cc2.zip |
Beef up how `qmk doctor` works. (#7375)
* Beef up how `qmk doctor` works.
* improve the `git submodule status` parsing. h/t @erovia
* Fix whitespace and imports
* yapf
* Add documentation for the new doctor functionality
* Replace type_unchanged() with str()
* remove unused modules
* Update lib/python/qmk/cli/doctor.py
Co-Authored-By: Erovia <Erovia@users.noreply.github.com>
Co-authored-by: Erovia <Erovia@users.noreply.github.com>
Diffstat (limited to 'lib/python/qmk/cli/doctor.py')
-rwxr-xr-x | lib/python/qmk/cli/doctor.py | 239 |
1 files changed, 176 insertions, 63 deletions
diff --git a/lib/python/qmk/cli/doctor.py b/lib/python/qmk/cli/doctor.py index 6ddc5571b..013bf7943 100755 --- a/lib/python/qmk/cli/doctor.py +++ b/lib/python/qmk/cli/doctor.py | |||
@@ -1,14 +1,18 @@ | |||
1 | """QMK Python Doctor | 1 | """QMK Doctor |
2 | 2 | ||
3 | Check up for QMK environment. | 3 | Check out the user's QMK environment and make sure it's ready to compile. |
4 | """ | 4 | """ |
5 | import os | ||
6 | import platform | 5 | import platform |
7 | import shutil | 6 | import shutil |
8 | import subprocess | 7 | import subprocess |
9 | import glob | 8 | from pathlib import Path |
10 | 9 | ||
11 | from milc import cli | 10 | from milc import cli |
11 | from qmk import submodules | ||
12 | from qmk.questions import yesno | ||
13 | |||
14 | ESSENTIAL_BINARIES = ['dfu-programmer', 'avrdude', 'dfu-util', 'avr-gcc', 'arm-none-eabi-gcc', 'bin/qmk'] | ||
15 | ESSENTIAL_SUBMODULES = ['lib/chibios', 'lib/lufa'] | ||
12 | 16 | ||
13 | 17 | ||
14 | def _udev_rule(vid, pid=None): | 18 | def _udev_rule(vid, pid=None): |
@@ -20,6 +24,137 @@ def _udev_rule(vid, pid=None): | |||
20 | return 'SUBSYSTEMS=="usb", ATTRS{idVendor}=="%s", MODE:="0666"' % vid | 24 | return 'SUBSYSTEMS=="usb", ATTRS{idVendor}=="%s", MODE:="0666"' % vid |
21 | 25 | ||
22 | 26 | ||
27 | def check_binaries(): | ||
28 | """Iterates through ESSENTIAL_BINARIES and tests them. | ||
29 | """ | ||
30 | ok = True | ||
31 | |||
32 | for binary in ESSENTIAL_BINARIES: | ||
33 | if not is_executable(binary): | ||
34 | ok = False | ||
35 | |||
36 | return ok | ||
37 | |||
38 | |||
39 | def check_submodules(): | ||
40 | """Iterates through all submodules to make sure they're cloned and up to date. | ||
41 | """ | ||
42 | ok = True | ||
43 | |||
44 | for submodule in submodules.status().values(): | ||
45 | if submodule['status'] is None: | ||
46 | if submodule['name'] in ESSENTIAL_SUBMODULES: | ||
47 | cli.log.error('Submodule %s has not yet been cloned!', submodule['name']) | ||
48 | ok = False | ||
49 | else: | ||
50 | cli.log.warn('Submodule %s is not available.', submodule['name']) | ||
51 | elif not submodule['status']: | ||
52 | if submodule['name'] in ESSENTIAL_SUBMODULES: | ||
53 | cli.log.warn('Submodule %s is not up to date!') | ||
54 | |||
55 | return ok | ||
56 | |||
57 | |||
58 | def check_udev_rules(): | ||
59 | """Make sure the udev rules look good. | ||
60 | """ | ||
61 | ok = True | ||
62 | udev_dir = Path("/etc/udev/rules.d/") | ||
63 | desired_rules = { | ||
64 | 'dfu': {_udev_rule("03eb", "2ff4"), _udev_rule("03eb", "2ffb"), _udev_rule("03eb", "2ff0")}, | ||
65 | 'tmk': {_udev_rule("feed")}, | ||
66 | 'input_club': {_udev_rule("1c11")}, | ||
67 | 'stm32': {_udev_rule("1eaf", "0003"), _udev_rule("0483", "df11")}, | ||
68 | 'caterina': {'ATTRS{idVendor}=="2a03", ENV{ID_MM_DEVICE_IGNORE}="1"', 'ATTRS{idVendor}=="2341", ENV{ID_MM_DEVICE_IGNORE}="1"'}, | ||
69 | } | ||
70 | |||
71 | if udev_dir.exists(): | ||
72 | udev_rules = [str(rule_file) for rule_file in udev_dir.glob('*.rules')] | ||
73 | current_rules = set() | ||
74 | |||
75 | # Collect all rules from the config files | ||
76 | for rule_file in udev_rules: | ||
77 | with open(rule_file, "r") as fd: | ||
78 | for line in fd.readlines(): | ||
79 | line = line.strip() | ||
80 | if not line.startswith("#") and len(line): | ||
81 | current_rules.add(line) | ||
82 | |||
83 | # Check if the desired rules are among the currently present rules | ||
84 | for bootloader, rules in desired_rules.items(): | ||
85 | if not rules.issubset(current_rules): | ||
86 | # If the rules for catalina are not present, check if ModemManager is running | ||
87 | if bootloader == "caterina": | ||
88 | if check_modem_manager(): | ||
89 | ok = False | ||
90 | cli.log.warn("{bg_yellow}Detected ModemManager without udev rules. Please either disable it or set the appropriate udev rules if you are using a Pro Micro.") | ||
91 | else: | ||
92 | cli.log.warn("{bg_yellow}Missing udev rules for '%s' boards. You'll need to use `sudo` in order to flash them.", bootloader) | ||
93 | |||
94 | return ok | ||
95 | |||
96 | |||
97 | def check_modem_manager(): | ||
98 | """Returns True if ModemManager is running. | ||
99 | """ | ||
100 | if shutil.which("systemctl"): | ||
101 | mm_check = subprocess.run(["systemctl", "--quiet", "is-active", "ModemManager.service"], timeout=10) | ||
102 | if mm_check.returncode == 0: | ||
103 | return True | ||
104 | |||
105 | else: | ||
106 | cli.log.warn("Can't find systemctl to check for ModemManager.") | ||
107 | |||
108 | |||
109 | def is_executable(command): | ||
110 | """Returns True if command exists and can be executed. | ||
111 | """ | ||
112 | # Make sure the command is in the path. | ||
113 | res = shutil.which(command) | ||
114 | if res is None: | ||
115 | cli.log.error("{fg_red}Can't find %s in your path.", command) | ||
116 | return False | ||
117 | |||
118 | # Make sure the command can be executed | ||
119 | check = subprocess.run([command, '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=5) | ||
120 | if check.returncode in [0, 1]: # Older versions of dfu-programmer exit 1 | ||
121 | cli.log.debug('Found {fg_cyan}%s', command) | ||
122 | return True | ||
123 | |||
124 | cli.log.error("{fg_red}Can't run `%s --version`", command) | ||
125 | return False | ||
126 | |||
127 | |||
128 | def os_test_linux(): | ||
129 | """Run the Linux specific tests. | ||
130 | """ | ||
131 | cli.log.info("Detected {fg_cyan}Linux.") | ||
132 | ok = True | ||
133 | |||
134 | if not check_udev_rules(): | ||
135 | ok = False | ||
136 | |||
137 | return ok | ||
138 | |||
139 | |||
140 | def os_test_macos(): | ||
141 | """Run the Mac specific tests. | ||
142 | """ | ||
143 | cli.log.info("Detected {fg_cyan}macOS.") | ||
144 | |||
145 | return True | ||
146 | |||
147 | |||
148 | def os_test_windows(): | ||
149 | """Run the Windows specific tests. | ||
150 | """ | ||
151 | cli.log.info("Detected {fg_cyan}Windows.") | ||
152 | |||
153 | return True | ||
154 | |||
155 | |||
156 | @cli.argument('-y', '--yes', action='store_true', arg_only=True, help='Answer yes to all questions.') | ||
157 | @cli.argument('-n', '--no', action='store_true', arg_only=True, help='Answer no to all questions.') | ||
23 | @cli.subcommand('Basic QMK environment checks') | 158 | @cli.subcommand('Basic QMK environment checks') |
24 | def doctor(cli): | 159 | def doctor(cli): |
25 | """Basic QMK environment checks. | 160 | """Basic QMK environment checks. |
@@ -30,75 +165,53 @@ def doctor(cli): | |||
30 | * [ ] Compile a trivial program with each compiler | 165 | * [ ] Compile a trivial program with each compiler |
31 | """ | 166 | """ |
32 | cli.log.info('QMK Doctor is checking your environment.') | 167 | cli.log.info('QMK Doctor is checking your environment.') |
33 | |||
34 | # Make sure the basic CLI tools we need are available and can be executed. | ||
35 | binaries = ['dfu-programmer', 'avrdude', 'dfu-util', 'avr-gcc', 'arm-none-eabi-gcc', 'bin/qmk'] | ||
36 | ok = True | 168 | ok = True |
37 | 169 | ||
38 | for binary in binaries: | ||
39 | res = shutil.which(binary) | ||
40 | if res is None: | ||
41 | cli.log.error("{fg_red}QMK can't find %s in your path.", binary) | ||
42 | ok = False | ||
43 | else: | ||
44 | check = subprocess.run([binary, '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=5) | ||
45 | if check.returncode in [0, 1]: | ||
46 | cli.log.info('Found {fg_cyan}%s', binary) | ||
47 | else: | ||
48 | cli.log.error("{fg_red}Can't run `%s --version`", binary) | ||
49 | ok = False | ||
50 | |||
51 | # Determine our OS and run platform specific tests | 170 | # Determine our OS and run platform specific tests |
52 | OS = platform.system() # noqa (N806), uppercase name is ok in this instance | 171 | OS = platform.system() # noqa (N806), uppercase name is ok in this instance |
53 | 172 | ||
54 | if OS == "Darwin": | 173 | if OS == 'Darwin': |
55 | cli.log.info("Detected {fg_cyan}macOS.") | 174 | if not os_test_macos(): |
56 | 175 | ok = False | |
57 | elif OS == "Linux": | 176 | elif OS == 'Linux': |
58 | cli.log.info("Detected {fg_cyan}Linux.") | 177 | if not os_test_linux(): |
59 | # Checking for udev rules | 178 | ok = False |
60 | udev_dir = "/etc/udev/rules.d/" | 179 | elif OS == 'Windows': |
61 | # These are the recommended udev rules | 180 | if not os_test_windows(): |
62 | desired_rules = { | 181 | ok = False |
63 | 'dfu': {_udev_rule("03eb", "2ff4"), _udev_rule("03eb", "2ffb"), _udev_rule("03eb", "2ff0")}, | 182 | else: |
64 | 'tmk': {_udev_rule("feed")}, | 183 | cli.log.error('Unsupported OS detected: %s', OS) |
65 | 'input_club': {_udev_rule("1c11")}, | 184 | ok = False |
66 | 'stm32': {_udev_rule("1eaf", "0003"), _udev_rule("0483", "df11")}, | ||
67 | 'caterina': {'ATTRS{idVendor}=="2a03", ENV{ID_MM_DEVICE_IGNORE}="1"', 'ATTRS{idVendor}=="2341", ENV{ID_MM_DEVICE_IGNORE}="1"'}, | ||
68 | } | ||
69 | |||
70 | if os.path.exists(udev_dir): | ||
71 | udev_rules = [rule for rule in glob.iglob(os.path.join(udev_dir, "*.rules")) if os.path.isfile(rule)] | ||
72 | # Collect all rules from the config files | ||
73 | current_rules = set() | ||
74 | for rule in udev_rules: | ||
75 | with open(rule, "r") as fd: | ||
76 | for line in fd.readlines(): | ||
77 | line = line.strip() | ||
78 | if not line.startswith("#") and len(line): | ||
79 | current_rules.add(line) | ||
80 | |||
81 | # Check if the desired rules are among the currently present rules | ||
82 | for bootloader, rules in desired_rules.items(): | ||
83 | if not rules.issubset(current_rules): | ||
84 | # If the rules for catalina are not present, check if ModemManager is running | ||
85 | if bootloader == "caterina": | ||
86 | if shutil.which("systemctl"): | ||
87 | mm_check = subprocess.run(["systemctl", "--quiet", "is-active", "ModemManager.service"], timeout=10) | ||
88 | if mm_check.returncode == 0: | ||
89 | ok = False | ||
90 | cli.log.warn("{bg_yellow}Detected ModemManager without udev rules. Please either disable it or set the appropriate udev rules if you are using a Pro Micro.") | ||
91 | else: | ||
92 | cli.log.warn("Can't find systemctl to check for ModemManager.") | ||
93 | else: | ||
94 | cli.log.warn("{bg_yellow}Missing udev rules for '%s' boards. You'll need to use `sudo` in order to flash them.", bootloader) | ||
95 | 185 | ||
186 | # Make sure the basic CLI tools we need are available and can be executed. | ||
187 | bin_ok = check_binaries() | ||
188 | |||
189 | if not bin_ok: | ||
190 | if yesno('Would you like to install dependencies?', default=True): | ||
191 | subprocess.run(['util/qmk_install.sh']) | ||
192 | bin_ok = check_binaries() | ||
193 | |||
194 | if bin_ok: | ||
195 | cli.log.info('All dependencies are installed.') | ||
96 | else: | 196 | else: |
97 | cli.log.info("Assuming {fg_cyan}Windows.") | 197 | ok = False |
198 | |||
199 | # Check out the QMK submodules | ||
200 | sub_ok = check_submodules() | ||
201 | |||
202 | if sub_ok: | ||
203 | cli.log.info('Submodules are up to date.') | ||
204 | else: | ||
205 | if yesno('Would you like to clone the submodules?', default=True): | ||
206 | submodules.update() | ||
207 | sub_ok = check_submodules() | ||
208 | |||
209 | if not sub_ok: | ||
210 | ok = False | ||
98 | 211 | ||
99 | # Report a summary of our findings to the user | 212 | # Report a summary of our findings to the user |
100 | if ok: | 213 | if ok: |
101 | cli.log.info('{fg_green}QMK is ready to go') | 214 | cli.log.info('{fg_green}QMK is ready to go') |
102 | else: | 215 | else: |
103 | cli.log.info('{fg_yellow}Problems detected, please fix these problems before proceeding.') | 216 | cli.log.info('{fg_yellow}Problems detected, please fix these problems before proceeding.') |
104 | # FIXME(skullydazed): Link to a document about troubleshooting, or discord or something | 217 | # FIXME(skullydazed/unclaimed): Link to a document about troubleshooting, or discord or something |