diff options
| author | QMK Bot <hello@qmk.fm> | 2020-12-21 12:30:13 +0000 |
|---|---|---|
| committer | QMK Bot <hello@qmk.fm> | 2020-12-21 12:30:13 +0000 |
| commit | 210e614d2a6999727497557845e91b063620a8c4 (patch) | |
| tree | 140d6e1b984f5094e655df27f9e7828bbe23121c /lib/python/qmk/os_helpers/__init__.py | |
| parent | 296a6cf4ea32d2aabc8fa1b09f9552fc541ab13b (diff) | |
| parent | a380a26ad2fa4cfa580134be81772153b697a1ac (diff) | |
| download | qmk_firmware-210e614d2a6999727497557845e91b063620a8c4.tar.gz qmk_firmware-210e614d2a6999727497557845e91b063620a8c4.zip | |
Merge remote-tracking branch 'origin/master' into develop
Diffstat (limited to 'lib/python/qmk/os_helpers/__init__.py')
| -rw-r--r-- | lib/python/qmk/os_helpers/__init__.py | 165 |
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 | """ | ||
| 3 | from enum import Enum | ||
| 4 | import re | ||
| 5 | import shutil | ||
| 6 | import subprocess | ||
| 7 | |||
| 8 | from milc import cli | ||
| 9 | from qmk.commands import run | ||
| 10 | from qmk import submodules | ||
| 11 | from qmk.constants import QMK_FIRMWARE | ||
| 12 | |||
| 13 | |||
| 14 | class CheckStatus(Enum): | ||
| 15 | OK = 1 | ||
| 16 | WARNING = 2 | ||
| 17 | ERROR = 3 | ||
| 18 | |||
| 19 | |||
| 20 | ESSENTIAL_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 | |||
| 34 | def 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 | |||
| 44 | def 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 | |||
| 54 | def 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 | |||
| 72 | def 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 | |||
| 81 | def 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 | |||
| 90 | def 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 | |||
| 99 | def 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 | |||
| 111 | def 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 | |||
| 120 | def 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 | |||
| 134 | def 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 | |||
| 157 | def 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 | ||
