From 607f4e9a9564cf6a9fd36ad65ec893b12e50e919 Mon Sep 17 00:00:00 2001 From: Zach White Date: Sat, 12 Jun 2021 21:03:54 -0700 Subject: [PATCH 1/2] ensure that safe_commands always run --- lib/python/qmk/cli/__init__.py | 74 ++++++++++++++++--------- lib/python/qmk/cli/chibios/__init__.py | 1 - lib/python/qmk/cli/format/__init__.py | 1 - lib/python/qmk/cli/generate/__init__.py | 9 --- lib/python/qmk/cli/list/__init__.py | 2 - lib/python/qmk/cli/new/__init__.py | 2 - 6 files changed, 49 insertions(+), 40 deletions(-) diff --git a/lib/python/qmk/cli/__init__.py b/lib/python/qmk/cli/__init__.py index 48812ae4bad5..3ba3a4f0f194 100644 --- a/lib/python/qmk/cli/__init__.py +++ b/lib/python/qmk/cli/__init__.py @@ -26,6 +26,42 @@ 'setup', ] +subcommands = [ + 'bux', + 'c2json', + 'cformat', + 'chibios.confmigrate', + 'clean', + 'compile', + 'console', + 'docs', + 'doctor', + 'fileformat', + 'flash', + 'format.json', + 'generate.api', + 'generate.config_h', + 'generate.dfu_header', + 'generate.docs', + 'generate.info_json', + 'generate.keyboard_h', + 'generate.layouts', + 'generate.rgb_breathe_table', + 'generate.rules_mk', + 'hello', + 'info', + 'json2c', + 'lint', + 'list.keyboards', + 'list.keymaps', + 'kle2json', + 'multibuild', + 'new.keyboard', + 'new.keymap', + 'pyformat', + 'pytest', +] + def _run_cmd(*command): """Run a command in a subshell. @@ -113,7 +149,7 @@ def _broken_module_imports(requirements): milc_version = __VERSION__.split('.') -if int(milc_version[0]) < 2 and int(milc_version[1]) < 3: +if int(milc_version[0]) < 2 and int(milc_version[1]) < 4: requirements = Path('requirements.txt').resolve() print(f'Your MILC library is too old! Please upgrade: python3 -m pip install -U -r {str(requirements)}') @@ -125,7 +161,9 @@ def _broken_module_imports(requirements): while args and args[0][0] == '-': del args[0] -if not args or args[0] not in safe_commands: +safe_command = args and args[0] in safe_commands + +if not safe_command: if _broken_module_imports('requirements.txt'): if yesno('Would you like to install the required Python modules?'): _run_cmd(sys.executable, '-m', 'pip', 'install', '-r', 'requirements.txt') @@ -148,27 +186,13 @@ def _broken_module_imports(requirements): exit(1) # Import our subcommands -from . import bux # noqa -from . import c2json # noqa -from . import cformat # noqa -from . import chibios # noqa -from . import clean # noqa -from . import compile # noqa from milc.subcommand import config # noqa -from . import console # noqa -from . import docs # noqa -from . import doctor # noqa -from . import fileformat # noqa -from . import flash # noqa -from . import format # noqa -from . import generate # noqa -from . import hello # noqa -from . import info # noqa -from . import json2c # noqa -from . import lint # noqa -from . import list # noqa -from . import kle2json # noqa -from . import multibuild # noqa -from . import new # noqa -from . import pyformat # noqa -from . import pytest # noqa + +for subcommand in subcommands: + try: + __import__(f'qmk.cli.{subcommand}') + except ModuleNotFoundError as e: + if safe_command: + print(f'Warning: Could not import qmk.cli.{subcommand}: {e.__class__.__name__}, {e}') + else: + raise diff --git a/lib/python/qmk/cli/chibios/__init__.py b/lib/python/qmk/cli/chibios/__init__.py index 4301837defda..e69de29bb2d1 100644 --- a/lib/python/qmk/cli/chibios/__init__.py +++ b/lib/python/qmk/cli/chibios/__init__.py @@ -1 +0,0 @@ -from . import confmigrate diff --git a/lib/python/qmk/cli/format/__init__.py b/lib/python/qmk/cli/format/__init__.py index 741ec778b116..e69de29bb2d1 100644 --- a/lib/python/qmk/cli/format/__init__.py +++ b/lib/python/qmk/cli/format/__init__.py @@ -1 +0,0 @@ -from . import json diff --git a/lib/python/qmk/cli/generate/__init__.py b/lib/python/qmk/cli/generate/__init__.py index 0efca0022dff..e69de29bb2d1 100644 --- a/lib/python/qmk/cli/generate/__init__.py +++ b/lib/python/qmk/cli/generate/__init__.py @@ -1,9 +0,0 @@ -from . import api -from . import config_h -from . import dfu_header -from . import docs -from . import info_json -from . import keyboard_h -from . import layouts -from . import rgb_breathe_table -from . import rules_mk diff --git a/lib/python/qmk/cli/list/__init__.py b/lib/python/qmk/cli/list/__init__.py index d83cd20b5b49..e69de29bb2d1 100644 --- a/lib/python/qmk/cli/list/__init__.py +++ b/lib/python/qmk/cli/list/__init__.py @@ -1,2 +0,0 @@ -from . import keyboards -from . import keymaps diff --git a/lib/python/qmk/cli/new/__init__.py b/lib/python/qmk/cli/new/__init__.py index fe5d6fe48371..e69de29bb2d1 100644 --- a/lib/python/qmk/cli/new/__init__.py +++ b/lib/python/qmk/cli/new/__init__.py @@ -1,2 +0,0 @@ -from . import keyboard -from . import keymap From 33bf2fe4ccf95a96b5f38e42e155bfc262da0172 Mon Sep 17 00:00:00 2001 From: Zach White Date: Sun, 13 Jun 2021 14:45:22 -0700 Subject: [PATCH 2/2] import the config subcommand in bin/qmk --- bin/qmk | 1 + lib/python/qmk/cli/__init__.py | 73 +++++++++++++++++----------------- 2 files changed, 37 insertions(+), 37 deletions(-) diff --git a/bin/qmk b/bin/qmk index a6e8ce08daca..617f99282642 100755 --- a/bin/qmk +++ b/bin/qmk @@ -35,6 +35,7 @@ def main(): print('Warning: The bin/qmk script is being deprecated. Please install the QMK CLI: python3 -m pip install qmk', file=sys.stderr) # Import the subcommands + import milc.subcommand.config # noqa import qmk.cli # noqa # Execute diff --git a/lib/python/qmk/cli/__init__.py b/lib/python/qmk/cli/__init__.py index 3ba3a4f0f194..32da1a9b52e6 100644 --- a/lib/python/qmk/cli/__init__.py +++ b/lib/python/qmk/cli/__init__.py @@ -27,39 +27,39 @@ ] subcommands = [ - 'bux', - 'c2json', - 'cformat', - 'chibios.confmigrate', - 'clean', - 'compile', - 'console', - 'docs', - 'doctor', - 'fileformat', - 'flash', - 'format.json', - 'generate.api', - 'generate.config_h', - 'generate.dfu_header', - 'generate.docs', - 'generate.info_json', - 'generate.keyboard_h', - 'generate.layouts', - 'generate.rgb_breathe_table', - 'generate.rules_mk', - 'hello', - 'info', - 'json2c', - 'lint', - 'list.keyboards', - 'list.keymaps', - 'kle2json', - 'multibuild', - 'new.keyboard', - 'new.keymap', - 'pyformat', - 'pytest', + 'qmk.cli.bux', + 'qmk.cli.c2json', + 'qmk.cli.cformat', + 'qmk.cli.chibios.confmigrate', + 'qmk.cli.clean', + 'qmk.cli.compile', + 'qmk.cli.console', + 'qmk.cli.docs', + 'qmk.cli.doctor', + 'qmk.cli.fileformat', + 'qmk.cli.flash', + 'qmk.cli.format.json', + 'qmk.cli.generate.api', + 'qmk.cli.generate.config_h', + 'qmk.cli.generate.dfu_header', + 'qmk.cli.generate.docs', + 'qmk.cli.generate.info_json', + 'qmk.cli.generate.keyboard_h', + 'qmk.cli.generate.layouts', + 'qmk.cli.generate.rgb_breathe_table', + 'qmk.cli.generate.rules_mk', + 'qmk.cli.hello', + 'qmk.cli.info', + 'qmk.cli.json2c', + 'qmk.cli.lint', + 'qmk.cli.list.keyboards', + 'qmk.cli.list.keymaps', + 'qmk.cli.kle2json', + 'qmk.cli.multibuild', + 'qmk.cli.new.keyboard', + 'qmk.cli.new.keymap', + 'qmk.cli.pyformat', + 'qmk.cli.pytest', ] @@ -186,13 +186,12 @@ def _broken_module_imports(requirements): exit(1) # Import our subcommands -from milc.subcommand import config # noqa - for subcommand in subcommands: try: - __import__(f'qmk.cli.{subcommand}') + __import__(subcommand) + except ModuleNotFoundError as e: if safe_command: - print(f'Warning: Could not import qmk.cli.{subcommand}: {e.__class__.__name__}, {e}') + print(f'Warning: Could not import {subcommand}: {e.__class__.__name__}, {e}') else: raise