diff options
author | skullydazed <skullydazed@users.noreply.github.com> | 2019-07-15 12:14:27 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-15 12:14:27 -0700 |
commit | a25dd58bc56b0c4010673723ac44eaff914979bb (patch) | |
tree | e4c08289df1b72db4ef8447ab7fdc13f604cffac /bin/qmk | |
parent | 7ba82cb5b751d69dda6cc77ec8877c89defad3e4 (diff) |
QMK CLI and JSON keymap support (#6176)
* Script to generate keymap.c from JSON file.
* Support for keymap.json
* Add a warning about the keymap.c getting overwritten.
* Fix keymap generating
* Install the python deps
* Flesh out more of the python environment
* Remove defunct json2keymap
* Style everything with yapf
* Polish up python support
* Hide json keymap.c into the .build dir
* Polish up qmk-compile-json
* Make milc work with positional arguments
* Fix a couple small things
* Fix some errors and make the CLI more understandable
* Make the qmk wrapper more robust
* Add basic QMK Doctor
* Clean up docstrings and flesh them out as needed
* remove unused compile_firmware() function
Diffstat (limited to 'bin/qmk')
-rwxr-xr-x | bin/qmk | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/bin/qmk b/bin/qmk new file mode 100755 index 0000000000..c34365bed4 --- /dev/null +++ b/bin/qmk @@ -0,0 +1,97 @@ +#!/usr/bin/env python3 +"""CLI wrapper for running QMK commands. +""" +import os +import subprocess +import sys +from glob import glob +from time import strftime +from importlib import import_module +from importlib.util import find_spec + +# Add the QMK python libs to our path +script_dir = os.path.dirname(os.path.realpath(__file__)) +qmk_dir = os.path.abspath(os.path.join(script_dir, '..')) +python_lib_dir = os.path.abspath(os.path.join(qmk_dir, 'lib', 'python')) +sys.path.append(python_lib_dir) + +# Change to the root of our checkout +os.environ['ORIG_CWD'] = os.getcwd() +os.chdir(qmk_dir) + +# Make sure our modules have been setup +with open('requirements.txt', 'r') as fd: + for line in fd.readlines(): + line = line.strip().replace('<', '=').replace('>', '=') + + if line[0] == '#': + continue + + if '#' in line: + line = line.split('#')[0] + + module = line.split('=')[0] if '=' in line else line + if not find_spec(module): + print('Your QMK build environment is not fully setup!\n') + print('Please run `./util/qmk_install.sh` to setup QMK.') + exit(255) + +# Figure out our version +command = ['git', 'describe', '--abbrev=6', '--dirty', '--always', '--tags'] +result = subprocess.run(command, text=True, capture_output=True) + +if result.returncode == 0: + os.environ['QMK_VERSION'] = 'QMK ' + result.stdout.strip() +else: + os.environ['QMK_VERSION'] = 'QMK ' + strftime('%Y-%m-%d-%H:%M:%S') + +# Setup the CLI +import milc +milc.EMOJI_LOGLEVELS['INFO'] = '{fg_blue}ψ{style_reset_all}' + +# If we were invoked as `qmk <cmd>` massage sys.argv into `qmk-<cmd>`. +# This means we can't accept arguments to the qmk script itself. +script_name = os.path.basename(sys.argv[0]) +if script_name == 'qmk': + if len(sys.argv) == 1: + milc.cli.log.error('No subcommand specified!\n') + + if len(sys.argv) == 1 or sys.argv[1] in ['-h', '--help']: + milc.cli.echo('usage: qmk <subcommand> [...]') + milc.cli.echo('\nsubcommands:') + subcommands = glob(os.path.join(qmk_dir, 'bin', 'qmk-*')) + for subcommand in sorted(subcommands): + subcommand = os.path.basename(subcommand).split('-', 1)[1] + milc.cli.echo('\t%s', subcommand) + milc.cli.echo('\nqmk <subcommand> --help for more information') + exit(1) + + if sys.argv[1] in ['-V', '--version']: + milc.cli.echo(os.environ['QMK_VERSION']) + exit(0) + + sys.argv[0] = script_name = '-'.join((script_name, sys.argv[1])) + del sys.argv[1] + +# Look for which module to import +if script_name == 'qmk': + milc.cli.print_help() + exit(0) +elif not script_name.startswith('qmk-'): + milc.cli.log.error('Invalid symlink, must start with "qmk-": %s', script_name) +else: + subcommand = script_name.replace('-', '.').replace('_', '.').split('.') + subcommand.insert(1, 'cli') + subcommand = '.'.join(subcommand) + + try: + import_module(subcommand) + except ModuleNotFoundError as e: + if e.__class__.__name__ != subcommand: + raise + + milc.cli.log.error('Invalid subcommand! Could not import %s.', subcommand) + exit(1) + +if __name__ == '__main__': + milc.cli() |