aboutsummaryrefslogtreecommitdiff
path: root/lib/python/qmk/os_helpers/__init__.py
diff options
context:
space:
mode:
authorErovia <Erovia@users.noreply.github.com>2020-12-21 13:29:36 +0100
committerGitHub <noreply@github.com>2020-12-21 13:29:36 +0100
commita380a26ad2fa4cfa580134be81772153b697a1ac (patch)
treea49c5b4bd9fb930be343b82d15ac04d6c3c632ba /lib/python/qmk/os_helpers/__init__.py
parente3211e307ec14c326b03b39806ed7bb11b927d34 (diff)
downloadqmk_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/os_helpers/__init__.py')
-rw-r--r--lib/python/qmk/os_helpers/__init__.py165
1 files changed, 165 insertions, 0 deletions
diff --git a/lib/python/qmk/os_helpers/__init__.py b/lib/python/qmk/os_helpers/__init__.py
new file mode 100644
index 000000000..3f64a63a3
--- /dev/null
+++ b/lib/python/qmk/os_helpers/__init__.py
@@ -0,0 +1,165 @@
1"""OS-agnostic helper functions
2"""
3from enum import Enum
4import re
5import shutil
6import subprocess
7
8from milc import cli
9from qmk.commands import run
10from qmk import submodules
11from qmk.constants import QMK_FIRMWARE
12
13
14class CheckStatus(Enum):
15 OK = 1
16 WARNING = 2
17 ERROR = 3
18
19
20ESSENTIAL_BINARIES = {
21 'dfu-programmer': {},
22 'avrdude': {},
23 'dfu-util': {},
24 'avr-gcc': {
25 'version_arg': '-dumpversion'
26 },
27 'arm-none-eabi-gcc': {
28 'version_arg': '-dumpversion'
29 },
30 'bin/qmk': {},
31}
32
33
34def parse_gcc_version(version):
35 m = re.match(r"(\d+)(?:\.(\d+))?(?:\.(\d+))?", version)
36
37 return {
38 'major': int(m.group(1)),
39 'minor': int(m.group(2)) if m.group(2) else 0,
40 'patch': int(m.group(3)) if m.group(3) else 0,
41 }
42
43
44def check_arm_gcc_version():
45 """Returns True if the arm-none-eabi-gcc version is not known to cause problems.
46 """
47 if 'output' in ESSENTIAL_BINARIES['arm-none-eabi-gcc']:
48 version_number = ESSENTIAL_BINARIES['arm-none-eabi-gcc']['output'].strip()
49 cli.log.info('Found arm-none-eabi-gcc version %s', version_number)
50
51 return CheckStatus.OK # Right now all known arm versions are ok
52
53
54def check_avr_gcc_version():
55 """Returns True if the avr-gcc version is not known to cause problems.
56 """
57 rc = CheckStatus.ERROR
58 if 'output' in ESSENTIAL_BINARIES['avr-gcc']:
59 version_number = ESSENTIAL_BINARIES['avr-gcc']['output'].strip()
60
61 cli.log.info('Found avr-gcc version %s', version_number)
62 rc = CheckStatus.OK
63
64 parsed_version = parse_gcc_version(version_number)
65 if parsed_version['major'] > 8:
66 cli.log.warning('{fg_yellow}We do not recommend avr-gcc newer than 8. Downgrading to 8.x is recommended.')
67 rc = CheckStatus.WARNING
68
69 return rc
70
71
72def check_avrdude_version():
73 if 'output' in ESSENTIAL_BINARIES['avrdude']:
74 last_line = ESSENTIAL_BINARIES['avrdude']['output'].split('\n')[-2]
75 version_number = last_line.split()[2][:-1]
76 cli.log.info('Found avrdude version %s', version_number)
77
78 return CheckStatus.OK
79
80
81def check_dfu_util_version():
82 if 'output' in ESSENTIAL_BINARIES['dfu-util']:
83 first_line = ESSENTIAL_BINARIES['dfu-util']['output'].split('\n')[0]
84 version_number = first_line.split()[1]
85 cli.log.info('Found dfu-util version %s', version_number)
86
87 return CheckStatus.OK
88
89
90def check_dfu_programmer_version():
91 if 'output' in ESSENTIAL_BINARIES['dfu-programmer']:
92 first_line = ESSENTIAL_BINARIES['dfu-programmer']['output'].split('\n')[0]
93 version_number = first_line.split()[1]
94 cli.log.info('Found dfu-programmer version %s', version_number)
95
96 return CheckStatus.OK
97
98
99def check_binaries():
100 """Iterates through ESSENTIAL_BINARIES and tests them.
101 """
102 ok = True
103
104 for binary in sorted(ESSENTIAL_BINARIES):
105 if not is_executable(binary):
106 ok = False
107
108 return ok
109
110
111def check_binary_versions():
112 """Check the versions of ESSENTIAL_BINARIES
113 """
114 versions = []
115 for check in (check_arm_gcc_version, check_avr_gcc_version, check_avrdude_version, check_dfu_util_version, check_dfu_programmer_version):
116 versions.append(check())
117 return versions
118
119
120def check_submodules():
121 """Iterates through all submodules to make sure they're cloned and up to date.
122 """
123 for submodule in submodules.status().values():
124 if submodule['status'] is None:
125 cli.log.error('Submodule %s has not yet been cloned!', submodule['name'])
126 return CheckStatus.ERROR
127 elif not submodule['status']:
128 cli.log.warning('Submodule %s is not up to date!', submodule['name'])
129 return CheckStatus.WARNING
130
131 return CheckStatus.OK
132
133
134def is_executable(command):
135 """Returns True if command exists and can be executed.
136 """
137 # Make sure the command is in the path.
138 res = shutil.which(command)
139 if res is None:
140 cli.log.error("{fg_red}Can't find %s in your path.", command)
141 return False
142
143 # Make sure the command can be executed
144 version_arg = ESSENTIAL_BINARIES[command].get('version_arg', '--version')
145 check = run([command, version_arg], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, timeout=5, universal_newlines=True)
146
147 ESSENTIAL_BINARIES[command]['output'] = check.stdout
148
149 if check.returncode in [0, 1]: # Older versions of dfu-programmer exit 1
150 cli.log.debug('Found {fg_cyan}%s', command)
151 return True
152
153 cli.log.error("{fg_red}Can't run `%s %s`", command, version_arg)
154 return False
155
156
157def 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