diff options
-rwxr-xr-x | bin/qmk | 12 | ||||
-rwxr-xr-x | lib/python/qmk/cli/doctor.py | 17 | ||||
-rw-r--r-- | lib/python/qmk/commands.py | 20 | ||||
-rw-r--r-- | lib/python/qmk/tests/test_cli_commands.py | 3 |
4 files changed, 31 insertions, 21 deletions
@@ -2,10 +2,8 @@ | |||
2 | """CLI wrapper for running QMK commands. | 2 | """CLI wrapper for running QMK commands. |
3 | """ | 3 | """ |
4 | import os | 4 | import os |
5 | import subprocess | ||
6 | import sys | 5 | import sys |
7 | from importlib.util import find_spec | 6 | from importlib.util import find_spec |
8 | from time import strftime | ||
9 | 7 | ||
10 | # Add the QMK python libs to our path | 8 | # Add the QMK python libs to our path |
11 | script_dir = os.path.dirname(os.path.realpath(__file__)) | 9 | script_dir = os.path.dirname(os.path.realpath(__file__)) |
@@ -35,16 +33,6 @@ with open(os.path.join(qmk_dir, 'requirements.txt'), 'r') as fd: | |||
35 | print('Please run `pip3 install -r requirements.txt` to install the python dependencies.') | 33 | print('Please run `pip3 install -r requirements.txt` to install the python dependencies.') |
36 | exit(255) | 34 | exit(255) |
37 | 35 | ||
38 | # Figure out our version | ||
39 | # TODO(skullydazed/anyone): Find a method that doesn't involve git. This is slow in docker and on windows. | ||
40 | command = ['git', 'describe', '--abbrev=6', '--dirty', '--always', '--tags'] | ||
41 | result = subprocess.run(command, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | ||
42 | |||
43 | if result.returncode == 0: | ||
44 | os.environ['QMK_VERSION'] = result.stdout.strip() | ||
45 | else: | ||
46 | os.environ['QMK_VERSION'] = 'nogit-' + strftime('%Y-%m-%d-%H:%M:%S') + '-dirty' | ||
47 | |||
48 | # Setup the CLI | 36 | # Setup the CLI |
49 | import milc # noqa | 37 | import milc # noqa |
50 | 38 | ||
diff --git a/lib/python/qmk/cli/doctor.py b/lib/python/qmk/cli/doctor.py index 9b81c8508..65b6f0a96 100755 --- a/lib/python/qmk/cli/doctor.py +++ b/lib/python/qmk/cli/doctor.py | |||
@@ -10,6 +10,7 @@ from pathlib import Path | |||
10 | from milc import cli | 10 | from milc import cli |
11 | from qmk import submodules | 11 | from qmk import submodules |
12 | from qmk.questions import yesno | 12 | from qmk.questions import yesno |
13 | from qmk.commands import run | ||
13 | 14 | ||
14 | ESSENTIAL_BINARIES = { | 15 | ESSENTIAL_BINARIES = { |
15 | 'dfu-programmer': {}, | 16 | 'dfu-programmer': {}, |
@@ -135,7 +136,7 @@ def check_modem_manager(): | |||
135 | """Returns True if ModemManager is running. | 136 | """Returns True if ModemManager is running. |
136 | """ | 137 | """ |
137 | if shutil.which("systemctl"): | 138 | if shutil.which("systemctl"): |
138 | mm_check = subprocess.run(["systemctl", "--quiet", "is-active", "ModemManager.service"], timeout=10) | 139 | mm_check = run(["systemctl", "--quiet", "is-active", "ModemManager.service"], timeout=10) |
139 | if mm_check.returncode == 0: | 140 | if mm_check.returncode == 0: |
140 | return True | 141 | return True |
141 | 142 | ||
@@ -153,7 +154,7 @@ def is_executable(command): | |||
153 | return False | 154 | return False |
154 | 155 | ||
155 | # Make sure the command can be executed | 156 | # Make sure the command can be executed |
156 | check = subprocess.run([command, '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=5, universal_newlines=True) | 157 | check = run([command, '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=5, universal_newlines=True) |
157 | ESSENTIAL_BINARIES[command]['output'] = check.stdout | 158 | ESSENTIAL_BINARIES[command]['output'] = check.stdout |
158 | 159 | ||
159 | if check.returncode in [0, 1]: # Older versions of dfu-programmer exit 1 | 160 | if check.returncode in [0, 1]: # Older versions of dfu-programmer exit 1 |
@@ -207,19 +208,19 @@ def doctor(cli): | |||
207 | ok = True | 208 | ok = True |
208 | 209 | ||
209 | # Determine our OS and run platform specific tests | 210 | # Determine our OS and run platform specific tests |
210 | OS = platform.platform().lower() # noqa (N806), uppercase name is ok in this instance | 211 | platform_id = platform.platform().lower() |
211 | 212 | ||
212 | if 'darwin' in OS or 'macos' in OS: | 213 | if 'darwin' in platform_id or 'macos' in platform_id: |
213 | if not os_test_macos(): | 214 | if not os_test_macos(): |
214 | ok = False | 215 | ok = False |
215 | elif 'linux' in OS: | 216 | elif 'linux' in platform_id: |
216 | if not os_test_linux(): | 217 | if not os_test_linux(): |
217 | ok = False | 218 | ok = False |
218 | elif 'windows' in OS: | 219 | elif 'windows' in platform_id: |
219 | if not os_test_windows(): | 220 | if not os_test_windows(): |
220 | ok = False | 221 | ok = False |
221 | else: | 222 | else: |
222 | cli.log.error('Unsupported OS detected: %s', OS) | 223 | cli.log.error('Unsupported OS detected: %s', platform_id) |
223 | ok = False | 224 | ok = False |
224 | 225 | ||
225 | # Make sure the basic CLI tools we need are available and can be executed. | 226 | # Make sure the basic CLI tools we need are available and can be executed. |
@@ -227,7 +228,7 @@ def doctor(cli): | |||
227 | 228 | ||
228 | if not bin_ok: | 229 | if not bin_ok: |
229 | if yesno('Would you like to install dependencies?', default=True): | 230 | if yesno('Would you like to install dependencies?', default=True): |
230 | subprocess.run(['util/qmk_install.sh']) | 231 | run(['util/qmk_install.sh']) |
231 | bin_ok = check_binaries() | 232 | bin_ok = check_binaries() |
232 | 233 | ||
233 | if bin_ok: | 234 | if bin_ok: |
diff --git a/lib/python/qmk/commands.py b/lib/python/qmk/commands.py index 3d4ed1616..3424cdf08 100644 --- a/lib/python/qmk/commands.py +++ b/lib/python/qmk/commands.py | |||
@@ -1,6 +1,10 @@ | |||
1 | """Helper functions for commands. | 1 | """Helper functions for commands. |
2 | """ | 2 | """ |
3 | import json | 3 | import json |
4 | import os | ||
5 | import platform | ||
6 | import subprocess | ||
7 | import shlex | ||
4 | 8 | ||
5 | import qmk.keymap | 9 | import qmk.keymap |
6 | 10 | ||
@@ -61,3 +65,19 @@ def parse_configurator_json(configurator_file): | |||
61 | user_keymap = json.load(configurator_file) | 65 | user_keymap = json.load(configurator_file) |
62 | 66 | ||
63 | return user_keymap | 67 | return user_keymap |
68 | |||
69 | |||
70 | def run(command, *args, **kwargs): | ||
71 | """Run a command with subprocess.run | ||
72 | """ | ||
73 | platform_id = platform.platform().lower() | ||
74 | |||
75 | if isinstance(command, str): | ||
76 | raise TypeError('`command` must be a non-text sequence such as list or tuple.') | ||
77 | |||
78 | if 'windows' in platform_id: | ||
79 | safecmd = map(shlex.quote, command) | ||
80 | safecmd = ' '.join(safecmd) | ||
81 | command = [os.environ['SHELL'], '-c', safecmd] | ||
82 | |||
83 | return subprocess.run(command, *args, **kwargs) | ||
diff --git a/lib/python/qmk/tests/test_cli_commands.py b/lib/python/qmk/tests/test_cli_commands.py index a2595eb78..3b4e66a21 100644 --- a/lib/python/qmk/tests/test_cli_commands.py +++ b/lib/python/qmk/tests/test_cli_commands.py | |||
@@ -1,9 +1,10 @@ | |||
1 | import subprocess | 1 | import subprocess |
2 | from qmk.commands import run | ||
2 | 3 | ||
3 | 4 | ||
4 | def check_subcommand(command, *args): | 5 | def check_subcommand(command, *args): |
5 | cmd = ['bin/qmk', command] + list(args) | 6 | cmd = ['bin/qmk', command] + list(args) |
6 | return subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) | 7 | return run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) |
7 | 8 | ||
8 | 9 | ||
9 | def test_cformat(): | 10 | def test_cformat(): |