QMK CLI Environment bootstrapper (#25038)
Co-authored-by: Joel Challis <git@zvecr.com> Co-authored-by: Pascal Getreuer <getreuer@google.com>
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
"""Check for specific programs.
|
||||
"""
|
||||
from enum import Enum
|
||||
import re
|
||||
import shutil
|
||||
from subprocess import DEVNULL, TimeoutExpired
|
||||
from tempfile import TemporaryDirectory
|
||||
@@ -9,6 +8,7 @@ from pathlib import Path
|
||||
|
||||
from milc import cli
|
||||
from qmk import submodules
|
||||
from qmk.commands import find_make
|
||||
|
||||
|
||||
class CheckStatus(Enum):
|
||||
@@ -17,7 +17,13 @@ class CheckStatus(Enum):
|
||||
ERROR = 3
|
||||
|
||||
|
||||
WHICH_MAKE = Path(find_make()).name
|
||||
|
||||
ESSENTIAL_BINARIES = {
|
||||
WHICH_MAKE: {},
|
||||
'git': {},
|
||||
'dos2unix': {},
|
||||
'diff': {},
|
||||
'dfu-programmer': {},
|
||||
'avrdude': {},
|
||||
'dfu-util': {},
|
||||
@@ -30,14 +36,39 @@ ESSENTIAL_BINARIES = {
|
||||
}
|
||||
|
||||
|
||||
def _parse_gcc_version(version):
|
||||
m = re.match(r"(\d+)(?:\.(\d+))?(?:\.(\d+))?", version)
|
||||
def _check_make_version():
|
||||
last_line = ESSENTIAL_BINARIES[WHICH_MAKE]['output'].split('\n')[0]
|
||||
version_number = last_line.split()[2]
|
||||
cli.log.info('Found %s version %s', WHICH_MAKE, version_number)
|
||||
|
||||
return {
|
||||
'major': int(m.group(1)),
|
||||
'minor': int(m.group(2)) if m.group(2) else 0,
|
||||
'patch': int(m.group(3)) if m.group(3) else 0,
|
||||
}
|
||||
return CheckStatus.OK
|
||||
|
||||
|
||||
def _check_git_version():
|
||||
last_line = ESSENTIAL_BINARIES['git']['output'].split('\n')[0]
|
||||
version_number = last_line.split()[2]
|
||||
cli.log.info('Found git version %s', version_number)
|
||||
|
||||
return CheckStatus.OK
|
||||
|
||||
|
||||
def _check_dos2unix_version():
|
||||
last_line = ESSENTIAL_BINARIES['dos2unix']['output'].split('\n')[0]
|
||||
version_number = last_line.split()[1]
|
||||
cli.log.info('Found dos2unix version %s', version_number)
|
||||
|
||||
return CheckStatus.OK
|
||||
|
||||
|
||||
def _check_diff_version():
|
||||
last_line = ESSENTIAL_BINARIES['diff']['output'].split('\n')[0]
|
||||
if 'Apple diff' in last_line:
|
||||
version_number = last_line
|
||||
else:
|
||||
version_number = last_line.split()[3]
|
||||
cli.log.info('Found diff version %s', version_number)
|
||||
|
||||
return CheckStatus.OK
|
||||
|
||||
|
||||
def _check_arm_gcc_version():
|
||||
@@ -148,16 +179,24 @@ def check_binaries():
|
||||
"""Iterates through ESSENTIAL_BINARIES and tests them.
|
||||
"""
|
||||
ok = CheckStatus.OK
|
||||
missing_from_path = []
|
||||
|
||||
for binary in sorted(ESSENTIAL_BINARIES):
|
||||
try:
|
||||
if not is_executable(binary):
|
||||
if not is_in_path(binary):
|
||||
ok = CheckStatus.ERROR
|
||||
missing_from_path.append(binary)
|
||||
elif not is_executable(binary):
|
||||
ok = CheckStatus.ERROR
|
||||
except TimeoutExpired:
|
||||
cli.log.debug('Timeout checking %s', binary)
|
||||
if ok != CheckStatus.ERROR:
|
||||
ok = CheckStatus.WARNING
|
||||
|
||||
if missing_from_path:
|
||||
location_noun = 'its location' if len(missing_from_path) == 1 else 'their locations'
|
||||
cli.log.error('{fg_red}' + ', '.join(missing_from_path) + f' may need to be installed, or {location_noun} added to your path.')
|
||||
|
||||
return ok
|
||||
|
||||
|
||||
@@ -165,6 +204,10 @@ def check_binary_versions():
|
||||
"""Check the versions of ESSENTIAL_BINARIES
|
||||
"""
|
||||
checks = {
|
||||
WHICH_MAKE: _check_make_version,
|
||||
'git': _check_git_version,
|
||||
'dos2unix': _check_dos2unix_version,
|
||||
'diff': _check_diff_version,
|
||||
'arm-none-eabi-gcc': _check_arm_gcc_version,
|
||||
'avr-gcc': _check_avr_gcc_version,
|
||||
'avrdude': _check_avrdude_version,
|
||||
@@ -196,15 +239,18 @@ def check_submodules():
|
||||
return CheckStatus.OK
|
||||
|
||||
|
||||
def is_executable(command):
|
||||
"""Returns True if command exists and can be executed.
|
||||
def is_in_path(command):
|
||||
"""Returns True if command is found in the path.
|
||||
"""
|
||||
# Make sure the command is in the path.
|
||||
res = shutil.which(command)
|
||||
if res is None:
|
||||
if shutil.which(command) is None:
|
||||
cli.log.error("{fg_red}Can't find %s in your path.", command)
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def is_executable(command):
|
||||
"""Returns True if command can be executed.
|
||||
"""
|
||||
# Make sure the command can be executed
|
||||
version_arg = ESSENTIAL_BINARIES[command].get('version_arg', '--version')
|
||||
check = cli.run([command, version_arg], combined_output=True, stdin=DEVNULL, timeout=5)
|
||||
|
||||
Reference in New Issue
Block a user