aboutsummaryrefslogtreecommitdiff
path: root/lib/python/qmk/cli/doctor/check.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/python/qmk/cli/doctor/check.py')
-rw-r--r--lib/python/qmk/cli/doctor/check.py164
1 files changed, 164 insertions, 0 deletions
diff --git a/lib/python/qmk/cli/doctor/check.py b/lib/python/qmk/cli/doctor/check.py
new file mode 100644
index 000000000..a0bbb2816
--- /dev/null
+++ b/lib/python/qmk/cli/doctor/check.py
@@ -0,0 +1,164 @@
1"""Check for specific programs.
2"""
3from enum import Enum
4import re
5import shutil
6from subprocess import DEVNULL
7
8from milc import cli
9from qmk import submodules
10from qmk.constants import QMK_FIRMWARE
11
12
13class CheckStatus(Enum):
14 OK = 1
15 WARNING = 2
16 ERROR = 3
17
18
19ESSENTIAL_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
33def _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
43def _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
53def _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
71def _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
80def _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
89def _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
98def 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
110def 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
119def 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
133def 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
156def 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