summaryrefslogtreecommitdiffstats
path: root/lib/python
diff options
context:
space:
mode:
Diffstat (limited to 'lib/python')
-rw-r--r--lib/python/milc.py28
-rw-r--r--lib/python/qmk/cli/cformat.py6
-rwxr-xr-xlib/python/qmk/cli/doctor.py241
-rw-r--r--lib/python/qmk/questions.py178
-rw-r--r--lib/python/qmk/submodules.py71
5 files changed, 452 insertions, 72 deletions
diff --git a/lib/python/milc.py b/lib/python/milc.py
index 4392c8376a..36072ca764 100644
--- a/lib/python/milc.py
+++ b/lib/python/milc.py
@@ -178,8 +178,9 @@ class ConfigurationSection(Configuration):
def __getitem__(self, key):
"""Returns a config value, pulling from the `user` section as a fallback.
+ This is called when the attribute is accessed either via the get method or through [ ] index.
"""
- if key in self._config:
+ if key in self._config and self._config.get(key) is not None:
return self._config[key]
elif key in self.parent.user:
@@ -187,6 +188,15 @@ class ConfigurationSection(Configuration):
return None
+ def __getattr__(self, key):
+ """Returns the config value from the `user` section.
+ This is called when the attribute is accessed via dot notation but does not exists.
+ """
+ if key in self.parent.user:
+ return self.parent.user[key]
+
+ return None
+
def handle_store_boolean(self, *args, **kwargs):
"""Does the add_argument for action='store_boolean'.
@@ -501,7 +511,10 @@ class MILC(object):
if argument not in self.arg_only:
# Find the argument's section
- if self._entrypoint.__name__ in self.default_arguments and argument in self.default_arguments[self._entrypoint.__name__]:
+ # Underscores in command's names are converted to dashes during initialization.
+ # TODO(Erovia) Find a better solution
+ entrypoint_name = self._entrypoint.__name__.replace("_", "-")
+ if entrypoint_name in self.default_arguments and argument in self.default_arguments[entrypoint_name]:
argument_found = True
section = self._entrypoint.__name__
if argument in self.default_arguments['general']:
@@ -513,13 +526,16 @@ class MILC(object):
exit(1)
# Merge this argument into self.config
- if argument in self.default_arguments:
+ if argument in self.default_arguments['general'] or argument in self.default_arguments[entrypoint_name]:
arg_value = getattr(self.args, argument)
- if arg_value:
+ if arg_value is not None:
self.config[section][argument] = arg_value
else:
- if argument not in self.config[section]:
- self.config[section][argument] = getattr(self.args, argument)
+ if argument not in self.config[entrypoint_name]:
+ # Check if the argument exist for this section
+ arg = getattr(self.args, argument)
+ if arg is not None:
+ self.config[section][argument] = arg
self.release_lock()
diff --git a/lib/python/qmk/cli/cformat.py b/lib/python/qmk/cli/cformat.py
index 17ca91b3b5..de55218ae9 100644
--- a/lib/python/qmk/cli/cformat.py
+++ b/lib/python/qmk/cli/cformat.py
@@ -24,13 +24,15 @@ def cformat(cli):
if cli.args.files:
cli.args.files = [os.path.join(os.environ['ORIG_CWD'], file) for file in cli.args.files]
else:
+ ignores = ['tmk_core/protocol/usb_hid', 'quantum/template']
for dir in ['drivers', 'quantum', 'tests', 'tmk_core']:
for dirpath, dirnames, filenames in os.walk(dir):
- if 'tmk_core/protocol/usb_hid' in dirpath:
+ if any(i in dirpath for i in ignores):
+ dirnames.clear()
continue
for name in filenames:
- if name.endswith('.c') or name.endswith('.h') or name.endswith('.cpp'):
+ if name.endswith(('.c', '.h', '.cpp')):
cli.args.files.append(os.path.join(dirpath, name))
# Run clang-format on the files we've found
diff --git a/lib/python/qmk/cli/doctor.py b/lib/python/qmk/cli/doctor.py
index 6ddc5571b4..0d6c1c5b06 100755
--- a/lib/python/qmk/cli/doctor.py
+++ b/lib/python/qmk/cli/doctor.py
@@ -1,14 +1,18 @@
-"""QMK Python Doctor
+"""QMK Doctor
-Check up for QMK environment.
+Check out the user's QMK environment and make sure it's ready to compile.
"""
-import os
import platform
import shutil
import subprocess
-import glob
+from pathlib import Path
from milc import cli
+from qmk import submodules
+from qmk.questions import yesno
+
+ESSENTIAL_BINARIES = ['dfu-programmer', 'avrdude', 'dfu-util', 'avr-gcc', 'arm-none-eabi-gcc', 'bin/qmk']
+ESSENTIAL_SUBMODULES = ['lib/chibios', 'lib/lufa']
def _udev_rule(vid, pid=None):
@@ -20,6 +24,137 @@ def _udev_rule(vid, pid=None):
return 'SUBSYSTEMS=="usb", ATTRS{idVendor}=="%s", MODE:="0666"' % vid
+def check_binaries():
+ """Iterates through ESSENTIAL_BINARIES and tests them.
+ """
+ ok = True
+
+ for binary in ESSENTIAL_BINARIES:
+ if not is_executable(binary):
+ ok = False
+
+ return ok
+
+
+def check_submodules():
+ """Iterates through all submodules to make sure they're cloned and up to date.
+ """
+ ok = True
+
+ for submodule in submodules.status().values():
+ if submodule['status'] is None:
+ if submodule['name'] in ESSENTIAL_SUBMODULES:
+ cli.log.error('Submodule %s has not yet been cloned!', submodule['name'])
+ ok = False
+ else:
+ cli.log.warn('Submodule %s is not available.', submodule['name'])
+ elif not submodule['status']:
+ if submodule['name'] in ESSENTIAL_SUBMODULES:
+ cli.log.warn('Submodule %s is not up to date!')
+
+ return ok
+
+
+def check_udev_rules():
+ """Make sure the udev rules look good.
+ """
+ ok = True
+ udev_dir = Path("/etc/udev/rules.d/")
+ desired_rules = {
+ 'dfu': {_udev_rule("03eb", "2ff4"), _udev_rule("03eb", "2ffb"), _udev_rule("03eb", "2ff0")},
+ 'tmk': {_udev_rule("feed")},
+ 'input_club': {_udev_rule("1c11")},
+ 'stm32': {_udev_rule("1eaf", "0003"), _udev_rule("0483", "df11")},
+ 'caterina': {'ATTRS{idVendor}=="2a03", ENV{ID_MM_DEVICE_IGNORE}="1"', 'ATTRS{idVendor}=="2341", ENV{ID_MM_DEVICE_IGNORE}="1"'},
+ }
+
+ if udev_dir.exists():
+ udev_rules = [str(rule_file) for rule_file in udev_dir.glob('*.rules')]
+ current_rules = set()
+
+ # Collect all rules from the config files
+ for rule_file in udev_rules:
+ with open(rule_file, "r") as fd:
+ for line in fd.readlines():
+ line = line.strip()
+ if not line.startswith("#") and len(line):
+ current_rules.add(line)
+
+ # Check if the desired rules are among the currently present rules
+ for bootloader, rules in desired_rules.items():
+ if not rules.issubset(current_rules):
+ # If the rules for catalina are not present, check if ModemManager is running
+ if bootloader == "caterina":
+ if check_modem_manager():
+ ok = False
+ cli.log.warn("{bg_yellow}Detected ModemManager without udev rules. Please either disable it or set the appropriate udev rules if you are using a Pro Micro.")
+ else:
+ cli.log.warn("{bg_yellow}Missing udev rules for '%s' boards. You'll need to use `sudo` in order to flash them.", bootloader)
+
+ return ok
+
+
+def check_modem_manager():
+ """Returns True if ModemManager is running.
+ """
+ if shutil.which("systemctl"):
+ mm_check = subprocess.run(["systemctl", "--quiet", "is-active", "ModemManager.service"], timeout=10)
+ if mm_check.returncode == 0:
+ return True
+
+ else:
+ cli.log.warn("Can't find systemctl to check for ModemManager.")
+
+
+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
+ check = subprocess.run([command, '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=5)
+ 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 --version`", command)
+ return False
+
+
+def os_test_linux():
+ """Run the Linux specific tests.
+ """
+ cli.log.info("Detected {fg_cyan}Linux.")
+ ok = True
+
+ if not check_udev_rules():
+ ok = False
+
+ return ok
+
+
+def os_test_macos():
+ """Run the Mac specific tests.
+ """
+ cli.log.info("Detected {fg_cyan}macOS.")
+
+ return True
+
+
+def os_test_windows():
+ """Run the Windows specific tests.
+ """
+ cli.log.info("Detected {fg_cyan}Windows.")
+
+ return True
+
+
+@cli.argument('-y', '--yes', action='store_true', arg_only=True, help='Answer yes to all questions.')
+@cli.argument('-n', '--no', action='store_true', arg_only=True, help='Answer no to all questions.')
@cli.subcommand('Basic QMK environment checks')
def doctor(cli):
"""Basic QMK environment checks.
@@ -30,75 +165,53 @@ def doctor(cli):
* [ ] Compile a trivial program with each compiler
"""
cli.log.info('QMK Doctor is checking your environment.')
-
- # Make sure the basic CLI tools we need are available and can be executed.
- binaries = ['dfu-programmer', 'avrdude', 'dfu-util', 'avr-gcc', 'arm-none-eabi-gcc', 'bin/qmk']
ok = True
- for binary in binaries:
- res = shutil.which(binary)
- if res is None:
- cli.log.error("{fg_red}QMK can't find %s in your path.", binary)
+ # Determine our OS and run platform specific tests
+ OS = platform.platform().lower() # noqa (N806), uppercase name is ok in this instance
+
+ if 'darwin' in OS:
+ if not os_test_macos():
ok = False
- else:
- check = subprocess.run([binary, '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=5)
- if check.returncode in [0, 1]:
- cli.log.info('Found {fg_cyan}%s', binary)
- else:
- cli.log.error("{fg_red}Can't run `%s --version`", binary)
- ok = False
+ elif 'linux' in OS:
+ if not os_test_linux():
+ ok = False
+ elif 'windows' in OS:
+ if not os_test_windows():
+ ok = False
+ else:
+ cli.log.error('Unsupported OS detected: %s', OS)
+ ok = False
- # Determine our OS and run platform specific tests
- OS = platform.system() # noqa (N806), uppercase name is ok in this instance
-
- if OS == "Darwin":
- cli.log.info("Detected {fg_cyan}macOS.")
-
- elif OS == "Linux":
- cli.log.info("Detected {fg_cyan}Linux.")
- # Checking for udev rules
- udev_dir = "/etc/udev/rules.d/"
- # These are the recommended udev rules
- desired_rules = {
- 'dfu': {_udev_rule("03eb", "2ff4"), _udev_rule("03eb", "2ffb"), _udev_rule("03eb", "2ff0")},
- 'tmk': {_udev_rule("feed")},
- 'input_club': {_udev_rule("1c11")},
- 'stm32': {_udev_rule("1eaf", "0003"), _udev_rule("0483", "df11")},
- 'caterina': {'ATTRS{idVendor}=="2a03", ENV{ID_MM_DEVICE_IGNORE}="1"', 'ATTRS{idVendor}=="2341", ENV{ID_MM_DEVICE_IGNORE}="1"'},
- }
-
- if os.path.exists(udev_dir):
- udev_rules = [rule for rule in glob.iglob(os.path.join(udev_dir, "*.rules")) if os.path.isfile(rule)]
- # Collect all rules from the config files
- current_rules = set()
- for rule in udev_rules:
- with open(rule, "r") as fd:
- for line in fd.readlines():
- line = line.strip()
- if not line.startswith("#") and len(line):
- current_rules.add(line)
-
- # Check if the desired rules are among the currently present rules
- for bootloader, rules in desired_rules.items():
- if not rules.issubset(current_rules):
- # If the rules for catalina are not present, check if ModemManager is running
- if bootloader == "caterina":
- if shutil.which("systemctl"):
- mm_check = subprocess.run(["systemctl", "--quiet", "is-active", "ModemManager.service"], timeout=10)
- if mm_check.returncode == 0:
- ok = False
- cli.log.warn("{bg_yellow}Detected ModemManager without udev rules. Please either disable it or set the appropriate udev rules if you are using a Pro Micro.")
- else:
- cli.log.warn("Can't find systemctl to check for ModemManager.")
- else:
- cli.log.warn("{bg_yellow}Missing udev rules for '%s' boards. You'll need to use `sudo` in order to flash them.", bootloader)
+ # Make sure the basic CLI tools we need are available and can be executed.
+ bin_ok = check_binaries()
+
+ if not bin_ok:
+ if yesno('Would you like to install dependencies?', default=True):
+ subprocess.run(['util/qmk_install.sh'])
+ bin_ok = check_binaries()
+ if bin_ok:
+ cli.log.info('All dependencies are installed.')
else:
- cli.log.info("Assuming {fg_cyan}Windows.")
+ ok = False
+
+ # Check out the QMK submodules
+ sub_ok = check_submodules()
+
+ if sub_ok:
+ cli.log.info('Submodules are up to date.')
+ else:
+ if yesno('Would you like to clone the submodules?', default=True):
+ submodules.update()
+ sub_ok = check_submodules()
+
+ if not sub_ok:
+ ok = False
# Report a summary of our findings to the user
if ok:
cli.log.info('{fg_green}QMK is ready to go')
else:
cli.log.info('{fg_yellow}Problems detected, please fix these problems before proceeding.')
- # FIXME(skullydazed): Link to a document about troubleshooting, or discord or something
+ # FIXME(skullydazed/unclaimed): Link to a document about troubleshooting, or discord or something
diff --git a/lib/python/qmk/questions.py b/lib/python/qmk/questions.py
new file mode 100644
index 0000000000..27f43ac1e9
--- /dev/null
+++ b/lib/python/qmk/questions.py
@@ -0,0 +1,178 @@
+"""Functions to collect user input.
+"""
+
+from milc import cli, format_ansi
+
+
+def yesno(prompt, *args, default=None, **kwargs):
+ """Displays prompt to the user and gets a yes or no response.
+
+ Returns True for a yes and False for a no.
+
+ If you add `--yes` and `--no` arguments to your program the user can answer questions by passing command line flags.
+
+ @add_argument('-y', '--yes', action='store_true', arg_only=True, help='Answer yes to all questions.')
+ @add_argument('-n', '--no', action='store_true', arg_only=True, help='Answer no to all questions.')
+
+ Arguments:
+ prompt
+ The prompt to present to the user. Can include ANSI and format strings like milc's `cli.echo()`.
+
+ default
+ Whether to default to a Yes or No when the user presses enter.
+
+ None- force the user to enter Y or N
+
+ True- Default to yes
+
+ False- Default to no
+ """
+ if not args and kwargs:
+ args = kwargs
+
+ if 'no' in cli.args and cli.args.no:
+ return False
+
+ if 'yes' in cli.args and cli.args.yes:
+ return True
+
+ if default is not None:
+ if default:
+ prompt = prompt + ' [Y/n] '
+ else:
+ prompt = prompt + ' [y/N] '
+
+ while True:
+ cli.echo('')
+ answer = input(format_ansi(prompt % args))
+ cli.echo('')
+
+ if not answer and prompt is not None:
+ return default
+
+ elif answer.lower() in ['y', 'yes']:
+ return True
+
+ elif answer.lower() in ['n', 'no']:
+ return False
+
+
+def question(prompt, *args, default=None, confirm=False, answer_type=str, validate=None, **kwargs):
+ """Prompt the user to answer a question with a free-form input.
+
+ Arguments:
+ prompt
+ The prompt to present to the user. Can include ANSI and format strings like milc's `cli.echo()`.
+
+ default
+ The value to return when the user doesn't enter any value. Use None to prompt until they enter a value.
+
+ confirm
+ Present the user with a confirmation dialog before accepting their answer.
+
+ answer_type
+ Specify a type function for the answer. Will re-prompt the user if the function raises any errors. Common choices here include int, float, and decimal.Decimal.
+
+ validate
+ This is an optional function that can be used to validate the answer. It should return True or False and have the following signature:
+
+ def function_name(answer, *args, **kwargs):
+ """
+ if not args and kwargs:
+ args = kwargs
+
+ if default is not None:
+ prompt = '%s [%s] ' % (prompt, default)
+
+ while True:
+ cli.echo('')
+ answer = input(format_ansi(prompt % args))
+ cli.echo('')
+
+ if answer:
+ if validate is not None and not validate(answer, *args, **kwargs):
+ continue
+
+ elif confirm:
+ if yesno('Is the answer "%s" correct?', answer, default=True):
+ try:
+ return answer_type(answer)
+ except Exception as e:
+ cli.log.error('Could not convert answer (%s) to type %s: %s', answer, answer_type.__name__, str(e))
+
+ else:
+ try:
+ return answer_type(answer)
+ except Exception as e:
+ cli.log.error('Could not convert answer (%s) to type %s: %s', answer, answer_type.__name__, str(e))
+
+ elif default is not None:
+ return default
+
+
+def choice(heading, options, *args, default=None, confirm=False, prompt='Please enter your choice: ', **kwargs):
+ """Present the user with a list of options and let them pick one.
+
+ Users can enter either the number or the text of their choice.
+
+ This will return the value of the item they choose, not the numerical index.
+
+ Arguments:
+ heading
+ The text to place above the list of options.
+
+ options
+ A sequence of items to choose from.
+
+ default
+ The index of the item to return when the user doesn't enter any value. Use None to prompt until they enter a value.
+
+ confirm
+ Present the user with a confirmation dialog before accepting their answer.
+
+ prompt
+ The prompt to present to the user. Can include ANSI and format strings like milc's `cli.echo()`.
+ """
+ if not args and kwargs:
+ args = kwargs
+
+ if prompt and default:
+ prompt = prompt + ' [%s] ' % (default + 1,)
+
+ while True:
+ # Prompt for an answer.
+ cli.echo('')
+ cli.echo(heading % args)
+ cli.echo('')
+ for i, option in enumerate(options, 1):
+ cli.echo('\t{fg_cyan}%d.{fg_reset} %s', i, option)
+
+ cli.echo('')
+ answer = input(format_ansi(prompt))
+ cli.echo('')
+
+ # If the user types in one of the options exactly use that
+ if answer in options:
+ return answer
+
+ # Massage the answer into a valid integer
+ if answer == '' and default:
+ answer = default
+ else:
+ try:
+ answer = int(answer) - 1
+ except Exception:
+ # Normally we would log the exception here, but in the interest of clean UI we do not.
+ cli.log.error('Invalid choice: %s', answer + 1)
+ continue
+
+ # Validate the answer
+ if answer >= len(options) or answer < 0:
+ cli.log.error('Invalid choice: %s', answer + 1)
+ continue
+
+ if confirm and not yesno('Is the answer "%s" correct?', answer + 1, default=True):
+ continue
+
+ # Return the answer they chose.
+ return options[answer]
diff --git a/lib/python/qmk/submodules.py b/lib/python/qmk/submodules.py
new file mode 100644
index 0000000000..be51a68043
--- /dev/null
+++ b/lib/python/qmk/submodules.py
@@ -0,0 +1,71 @@
+"""Functions for working with QMK's submodules.
+"""
+
+import subprocess
+
+
+def status():
+ """Returns a dictionary of submodules.
+
+ Each entry is a dict of the form:
+
+ {
+ 'name': 'submodule_name',
+ 'status': None/False/True,
+ 'githash': '<sha-1 hash for the submodule>
+ }
+
+ status is None when the submodule doesn't exist, False when it's out of date, and True when it's current
+ """
+ submodules = {}
+ git_cmd = subprocess.run(['git', 'submodule', 'status'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=30, universal_newlines=True)
+
+ for line in git_cmd.stdout.split('\n'):
+ if not line:
+ continue
+
+ status = line[0]
+ githash, submodule = line[1:].split()[:2]
+ submodules[submodule] = {'name': submodule, 'githash': githash}
+
+ if status == '-':
+ submodules[submodule]['status'] = None
+ elif status == '+':
+ submodules[submodule]['status'] = False
+ elif status == ' ':
+ submodules[submodule]['status'] = True
+ else:
+ raise ValueError('Unknown `git submodule status` sha-1 prefix character: "%s"' % status)
+
+ return submodules
+
+
+def update(submodules=None):
+ """Update the submodules.
+
+ submodules
+ A string containing a single submodule or a list of submodules.
+ """
+ git_sync_cmd = ['git', 'submodule', 'sync']
+ git_update_cmd = ['git', 'submodule', 'update', '--init']
+
+ if submodules is None:
+ # Update everything
+ git_sync_cmd.append('--recursive')
+ git_update_cmd.append('--recursive')
+ subprocess.run(git_sync_cmd, check=True)
+ subprocess.run(git_update_cmd, check=True)
+
+ else:
+ if isinstance(submodules, str):
+ # Update only a single submodule
+ git_sync_cmd.append(submodules)
+ git_update_cmd.append(submodules)
+ subprocess.run(git_sync_cmd, check=True)
+ subprocess.run(git_update_cmd, check=True)
+
+ else:
+ # Update submodules in a list
+ for submodule in submodules:
+ subprocess.run(git_sync_cmd + [submodule], check=True)
+ subprocess.run(git_update_cmd + [submodule], check=True)