CLI: More MSYS2 fixes (#8577)

* CLI: More MSYS2 fixes

Now I can fully setup and work with qmk_firmware on an MSYS2
installation without any errors or exceptions.

* Apply suggestions from code review

Co-Authored-By: skullydazed <skullydazed@users.noreply.github.com>

* Some improvements

* Remove unnecessary import

* Remove slow, unused code

Getting the version from GIT was slow on both Windows and Docker.
Until we find a better, faster way, this is removed.

* remove unused imports

* Implement @vomindoraan's suggestions

* refine how we pick the shell to use

* Apply @fauxpark's suggestions

fauxpark investigated the topic of shells in MSYS2 a bit and we come to the conclusion that the safest bet was to just use the user's shell.
Anything more just opens up more edge-cases than it solves.

Co-Authored-By: Ryan <fauxpark@gmail.com>

* Use `platform_id` in doctor

This will bring it in line with the new code.

Co-authored-by: skullydazed <skullydazed@users.noreply.github.com>
Co-authored-by: skullY <skullydazed@gmail.com>
Co-authored-by: Ryan <fauxpark@gmail.com>
This commit is contained in:
Erovia 2020-03-29 14:29:44 +02:00 committed by GitHub
parent 13fff52f6b
commit c89c084146
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 21 deletions

12
bin/qmk
View File

@ -2,10 +2,8 @@
"""CLI wrapper for running QMK commands. """CLI wrapper for running QMK commands.
""" """
import os import os
import subprocess
import sys import sys
from importlib.util import find_spec from importlib.util import find_spec
from time import strftime
# Add the QMK python libs to our path # Add the QMK python libs to our path
script_dir = os.path.dirname(os.path.realpath(__file__)) 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:
print('Please run `pip3 install -r requirements.txt` to install the python dependencies.') print('Please run `pip3 install -r requirements.txt` to install the python dependencies.')
exit(255) exit(255)
# Figure out our version
# TODO(skullydazed/anyone): Find a method that doesn't involve git. This is slow in docker and on windows.
command = ['git', 'describe', '--abbrev=6', '--dirty', '--always', '--tags']
result = subprocess.run(command, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
if result.returncode == 0:
os.environ['QMK_VERSION'] = result.stdout.strip()
else:
os.environ['QMK_VERSION'] = 'nogit-' + strftime('%Y-%m-%d-%H:%M:%S') + '-dirty'
# Setup the CLI # Setup the CLI
import milc # noqa import milc # noqa

View File

@ -10,6 +10,7 @@ from pathlib import Path
from milc import cli from milc import cli
from qmk import submodules from qmk import submodules
from qmk.questions import yesno from qmk.questions import yesno
from qmk.commands import run
ESSENTIAL_BINARIES = { ESSENTIAL_BINARIES = {
'dfu-programmer': {}, 'dfu-programmer': {},
@ -135,7 +136,7 @@ def check_modem_manager():
"""Returns True if ModemManager is running. """Returns True if ModemManager is running.
""" """
if shutil.which("systemctl"): if shutil.which("systemctl"):
mm_check = subprocess.run(["systemctl", "--quiet", "is-active", "ModemManager.service"], timeout=10) mm_check = run(["systemctl", "--quiet", "is-active", "ModemManager.service"], timeout=10)
if mm_check.returncode == 0: if mm_check.returncode == 0:
return True return True
@ -153,7 +154,7 @@ def is_executable(command):
return False return False
# Make sure the command can be executed # Make sure the command can be executed
check = subprocess.run([command, '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=5, universal_newlines=True) check = run([command, '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=5, universal_newlines=True)
ESSENTIAL_BINARIES[command]['output'] = check.stdout ESSENTIAL_BINARIES[command]['output'] = check.stdout
if check.returncode in [0, 1]: # Older versions of dfu-programmer exit 1 if check.returncode in [0, 1]: # Older versions of dfu-programmer exit 1
@ -207,19 +208,19 @@ def doctor(cli):
ok = True ok = True
# Determine our OS and run platform specific tests # Determine our OS and run platform specific tests
OS = platform.platform().lower() # noqa (N806), uppercase name is ok in this instance platform_id = platform.platform().lower()
if 'darwin' in OS or 'macos' in OS: if 'darwin' in platform_id or 'macos' in platform_id:
if not os_test_macos(): if not os_test_macos():
ok = False ok = False
elif 'linux' in OS: elif 'linux' in platform_id:
if not os_test_linux(): if not os_test_linux():
ok = False ok = False
elif 'windows' in OS: elif 'windows' in platform_id:
if not os_test_windows(): if not os_test_windows():
ok = False ok = False
else: else:
cli.log.error('Unsupported OS detected: %s', OS) cli.log.error('Unsupported OS detected: %s', platform_id)
ok = False ok = False
# Make sure the basic CLI tools we need are available and can be executed. # Make sure the basic CLI tools we need are available and can be executed.
@ -227,7 +228,7 @@ def doctor(cli):
if not bin_ok: if not bin_ok:
if yesno('Would you like to install dependencies?', default=True): if yesno('Would you like to install dependencies?', default=True):
subprocess.run(['util/qmk_install.sh']) run(['util/qmk_install.sh'])
bin_ok = check_binaries() bin_ok = check_binaries()
if bin_ok: if bin_ok:

View File

@ -1,6 +1,10 @@
"""Helper functions for commands. """Helper functions for commands.
""" """
import json import json
import os
import platform
import subprocess
import shlex
import qmk.keymap import qmk.keymap
@ -61,3 +65,19 @@ def parse_configurator_json(configurator_file):
user_keymap = json.load(configurator_file) user_keymap = json.load(configurator_file)
return user_keymap return user_keymap
def run(command, *args, **kwargs):
"""Run a command with subprocess.run
"""
platform_id = platform.platform().lower()
if isinstance(command, str):
raise TypeError('`command` must be a non-text sequence such as list or tuple.')
if 'windows' in platform_id:
safecmd = map(shlex.quote, command)
safecmd = ' '.join(safecmd)
command = [os.environ['SHELL'], '-c', safecmd]
return subprocess.run(command, *args, **kwargs)

View File

@ -1,9 +1,10 @@
import subprocess import subprocess
from qmk.commands import run
def check_subcommand(command, *args): def check_subcommand(command, *args):
cmd = ['bin/qmk', command] + list(args) cmd = ['bin/qmk', command] + list(args)
return subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) return run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
def test_cformat(): def test_cformat():