2021-06-22 11:50:53 -07:00
|
|
|
"""Check for specific programs.
|
2020-12-21 04:29:36 -08:00
|
|
|
"""
|
|
|
|
from enum import Enum
|
|
|
|
import re
|
|
|
|
import shutil
|
2023-01-09 01:27:41 -08:00
|
|
|
from subprocess import DEVNULL, TimeoutExpired
|
2020-12-21 04:29:36 -08:00
|
|
|
|
|
|
|
from milc import cli
|
|
|
|
from qmk import submodules
|
|
|
|
|
|
|
|
|
|
|
|
class CheckStatus(Enum):
|
|
|
|
OK = 1
|
|
|
|
WARNING = 2
|
|
|
|
ERROR = 3
|
|
|
|
|
|
|
|
|
|
|
|
ESSENTIAL_BINARIES = {
|
|
|
|
'dfu-programmer': {},
|
|
|
|
'avrdude': {},
|
|
|
|
'dfu-util': {},
|
|
|
|
'avr-gcc': {
|
|
|
|
'version_arg': '-dumpversion'
|
|
|
|
},
|
|
|
|
'arm-none-eabi-gcc': {
|
|
|
|
'version_arg': '-dumpversion'
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-22 11:50:53 -07:00
|
|
|
def _parse_gcc_version(version):
|
2020-12-21 04:29:36 -08:00
|
|
|
m = re.match(r"(\d+)(?:\.(\d+))?(?:\.(\d+))?", version)
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-22 11:50:53 -07:00
|
|
|
def _check_arm_gcc_version():
|
2020-12-21 04:29:36 -08:00
|
|
|
"""Returns True if the arm-none-eabi-gcc version is not known to cause problems.
|
|
|
|
"""
|
2023-01-09 01:27:41 -08:00
|
|
|
version_number = ESSENTIAL_BINARIES['arm-none-eabi-gcc']['output'].strip()
|
|
|
|
cli.log.info('Found arm-none-eabi-gcc version %s', version_number)
|
2020-12-21 04:29:36 -08:00
|
|
|
|
|
|
|
return CheckStatus.OK # Right now all known arm versions are ok
|
|
|
|
|
|
|
|
|
2021-06-22 11:50:53 -07:00
|
|
|
def _check_avr_gcc_version():
|
2020-12-21 04:29:36 -08:00
|
|
|
"""Returns True if the avr-gcc version is not known to cause problems.
|
|
|
|
"""
|
2023-01-09 01:27:41 -08:00
|
|
|
version_number = ESSENTIAL_BINARIES['avr-gcc']['output'].strip()
|
|
|
|
cli.log.info('Found avr-gcc version %s', version_number)
|
2020-12-21 04:29:36 -08:00
|
|
|
|
2023-01-09 01:27:41 -08:00
|
|
|
parsed_version = _parse_gcc_version(version_number)
|
|
|
|
if parsed_version['major'] > 8:
|
|
|
|
cli.log.warning('{fg_yellow}We do not recommend avr-gcc newer than 8. Downgrading to 8.x is recommended.')
|
|
|
|
return CheckStatus.WARNING
|
2020-12-21 04:29:36 -08:00
|
|
|
|
2023-01-09 01:27:41 -08:00
|
|
|
return CheckStatus.OK
|
2020-12-21 04:29:36 -08:00
|
|
|
|
|
|
|
|
2021-06-22 11:50:53 -07:00
|
|
|
def _check_avrdude_version():
|
2023-01-09 01:27:41 -08:00
|
|
|
last_line = ESSENTIAL_BINARIES['avrdude']['output'].split('\n')[-2]
|
|
|
|
version_number = last_line.split()[2][:-1]
|
|
|
|
cli.log.info('Found avrdude version %s', version_number)
|
2020-12-21 04:29:36 -08:00
|
|
|
|
|
|
|
return CheckStatus.OK
|
|
|
|
|
|
|
|
|
2021-06-22 11:50:53 -07:00
|
|
|
def _check_dfu_util_version():
|
2023-01-09 01:27:41 -08:00
|
|
|
first_line = ESSENTIAL_BINARIES['dfu-util']['output'].split('\n')[0]
|
|
|
|
version_number = first_line.split()[1]
|
|
|
|
cli.log.info('Found dfu-util version %s', version_number)
|
2020-12-21 04:29:36 -08:00
|
|
|
|
|
|
|
return CheckStatus.OK
|
|
|
|
|
|
|
|
|
2021-06-22 11:50:53 -07:00
|
|
|
def _check_dfu_programmer_version():
|
2023-01-09 01:27:41 -08:00
|
|
|
first_line = ESSENTIAL_BINARIES['dfu-programmer']['output'].split('\n')[0]
|
|
|
|
version_number = first_line.split()[1]
|
|
|
|
cli.log.info('Found dfu-programmer version %s', version_number)
|
2020-12-21 04:29:36 -08:00
|
|
|
|
|
|
|
return CheckStatus.OK
|
|
|
|
|
|
|
|
|
|
|
|
def check_binaries():
|
|
|
|
"""Iterates through ESSENTIAL_BINARIES and tests them.
|
|
|
|
"""
|
2023-01-09 01:27:41 -08:00
|
|
|
ok = CheckStatus.OK
|
2020-12-21 04:29:36 -08:00
|
|
|
|
|
|
|
for binary in sorted(ESSENTIAL_BINARIES):
|
2023-01-09 01:27:41 -08:00
|
|
|
try:
|
|
|
|
if not is_executable(binary):
|
|
|
|
ok = CheckStatus.ERROR
|
|
|
|
except TimeoutExpired:
|
|
|
|
cli.log.debug('Timeout checking %s', binary)
|
|
|
|
if ok != CheckStatus.ERROR:
|
|
|
|
ok = CheckStatus.WARNING
|
2020-12-21 04:29:36 -08:00
|
|
|
|
|
|
|
return ok
|
|
|
|
|
|
|
|
|
|
|
|
def check_binary_versions():
|
|
|
|
"""Check the versions of ESSENTIAL_BINARIES
|
|
|
|
"""
|
2023-01-09 01:27:41 -08:00
|
|
|
checks = {
|
|
|
|
'arm-none-eabi-gcc': _check_arm_gcc_version,
|
|
|
|
'avr-gcc': _check_avr_gcc_version,
|
|
|
|
'avrdude': _check_avrdude_version,
|
|
|
|
'dfu-util': _check_dfu_util_version,
|
|
|
|
'dfu-programmer': _check_dfu_programmer_version,
|
|
|
|
}
|
|
|
|
|
2020-12-21 04:29:36 -08:00
|
|
|
versions = []
|
2023-01-09 01:27:41 -08:00
|
|
|
for binary in sorted(ESSENTIAL_BINARIES):
|
|
|
|
if 'output' not in ESSENTIAL_BINARIES[binary]:
|
|
|
|
cli.log.warning('Unknown version for %s', binary)
|
|
|
|
versions.append(CheckStatus.WARNING)
|
|
|
|
continue
|
|
|
|
|
|
|
|
check = checks[binary]
|
2020-12-21 04:29:36 -08:00
|
|
|
versions.append(check())
|
|
|
|
return versions
|
|
|
|
|
|
|
|
|
|
|
|
def check_submodules():
|
|
|
|
"""Iterates through all submodules to make sure they're cloned and up to date.
|
|
|
|
"""
|
|
|
|
for submodule in submodules.status().values():
|
|
|
|
if submodule['status'] is None:
|
|
|
|
return CheckStatus.ERROR
|
|
|
|
elif not submodule['status']:
|
|
|
|
return CheckStatus.WARNING
|
|
|
|
|
|
|
|
return CheckStatus.OK
|
|
|
|
|
|
|
|
|
|
|
|
def is_executable(command):
|
|
|
|
"""Returns True if command exists and can be executed.
|
|
|
|
"""
|
|
|
|
# Make sure the command is in the path.
|
|
|
|
res = shutil.which(command)
|
|
|
|
if res is None:
|
|
|
|
cli.log.error("{fg_red}Can't find %s in your path.", command)
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Make sure the command can be executed
|
|
|
|
version_arg = ESSENTIAL_BINARIES[command].get('version_arg', '--version')
|
2021-05-19 15:24:46 -07:00
|
|
|
check = cli.run([command, version_arg], combined_output=True, stdin=DEVNULL, timeout=5)
|
2020-12-21 04:29:36 -08:00
|
|
|
|
|
|
|
ESSENTIAL_BINARIES[command]['output'] = check.stdout
|
|
|
|
|
|
|
|
if check.returncode in [0, 1]: # Older versions of dfu-programmer exit 1
|
|
|
|
cli.log.debug('Found {fg_cyan}%s', command)
|
|
|
|
return True
|
|
|
|
|
|
|
|
cli.log.error("{fg_red}Can't run `%s %s`", command, version_arg)
|
|
|
|
return False
|
2023-01-19 02:25:47 -08:00
|
|
|
|
|
|
|
|
|
|
|
def release_info(file='/etc/os-release'):
|
|
|
|
"""Parse release info to dict
|
|
|
|
"""
|
|
|
|
ret = {}
|
|
|
|
try:
|
|
|
|
with open(file) as f:
|
|
|
|
for line in f:
|
|
|
|
if '=' in line:
|
|
|
|
key, value = map(str.strip, line.split('=', 1))
|
|
|
|
if value.startswith('"') and value.endswith('"'):
|
|
|
|
value = value[1:-1]
|
|
|
|
ret[key] = value
|
|
|
|
except (PermissionError, FileNotFoundError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
return ret
|