diff options
Diffstat (limited to 'bin/qmk')
| -rwxr-xr-x | bin/qmk | 92 |
1 files changed, 36 insertions, 56 deletions
| @@ -4,10 +4,8 @@ | |||
| 4 | import os | 4 | import os |
| 5 | import subprocess | 5 | import subprocess |
| 6 | import sys | 6 | import sys |
| 7 | from glob import glob | ||
| 8 | from time import strftime | ||
| 9 | from importlib import import_module | ||
| 10 | from importlib.util import find_spec | 7 | from importlib.util import find_spec |
| 8 | from time import strftime | ||
| 11 | 9 | ||
| 12 | # Add the QMK python libs to our path | 10 | # Add the QMK python libs to our path |
| 13 | script_dir = os.path.dirname(os.path.realpath(__file__)) | 11 | script_dir = os.path.dirname(os.path.realpath(__file__)) |
| @@ -15,12 +13,8 @@ qmk_dir = os.path.abspath(os.path.join(script_dir, '..')) | |||
| 15 | python_lib_dir = os.path.abspath(os.path.join(qmk_dir, 'lib', 'python')) | 13 | python_lib_dir = os.path.abspath(os.path.join(qmk_dir, 'lib', 'python')) |
| 16 | sys.path.append(python_lib_dir) | 14 | sys.path.append(python_lib_dir) |
| 17 | 15 | ||
| 18 | # Change to the root of our checkout | ||
| 19 | os.environ['ORIG_CWD'] = os.getcwd() | ||
| 20 | os.chdir(qmk_dir) | ||
| 21 | |||
| 22 | # Make sure our modules have been setup | 16 | # Make sure our modules have been setup |
| 23 | with open('requirements.txt', 'r') as fd: | 17 | with open(os.path.join(qmk_dir, 'requirements.txt'), 'r') as fd: |
| 24 | for line in fd.readlines(): | 18 | for line in fd.readlines(): |
| 25 | line = line.strip().replace('<', '=').replace('>', '=') | 19 | line = line.strip().replace('<', '=').replace('>', '=') |
| 26 | 20 | ||
| @@ -32,72 +26,58 @@ with open('requirements.txt', 'r') as fd: | |||
| 32 | 26 | ||
| 33 | module = line.split('=')[0] if '=' in line else line | 27 | module = line.split('=')[0] if '=' in line else line |
| 34 | if not find_spec(module): | 28 | if not find_spec(module): |
| 35 | print('Your QMK build environment is not fully setup!\n') | 29 | print('Could not find module %s!', module) |
| 36 | print('Please run `./util/qmk_install.sh` to setup QMK.') | 30 | print('Please run `pip3 install -r requirements.txt` to install the python dependencies.') |
| 37 | exit(255) | 31 | exit(255) |
| 38 | 32 | ||
| 39 | # Figure out our version | 33 | # Figure out our version |
| 34 | # 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'] | 35 | command = ['git', 'describe', '--abbrev=6', '--dirty', '--always', '--tags'] |
| 41 | result = subprocess.run(command, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | 36 | result = subprocess.run(command, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 42 | 37 | ||
| 43 | if result.returncode == 0: | 38 | if result.returncode == 0: |
| 44 | os.environ['QMK_VERSION'] = 'QMK ' + result.stdout.strip() | 39 | os.environ['QMK_VERSION'] = result.stdout.strip() |
| 45 | else: | 40 | else: |
| 46 | os.environ['QMK_VERSION'] = 'QMK ' + strftime('%Y-%m-%d-%H:%M:%S') | 41 | os.environ['QMK_VERSION'] = 'nogit-' + strftime('%Y-%m-%d-%H:%M:%S') + '-dirty' |
| 47 | 42 | ||
| 48 | # Setup the CLI | 43 | # Setup the CLI |
| 49 | import milc | 44 | import milc |
| 50 | milc.EMOJI_LOGLEVELS['INFO'] = '{fg_blue}Ψ{style_reset_all}' | ||
| 51 | 45 | ||
| 52 | # If we were invoked as `qmk <cmd>` massage sys.argv into `qmk-<cmd>`. | 46 | milc.EMOJI_LOGLEVELS['INFO'] = '{fg_blue}Ψ{style_reset_all}' |
| 53 | # This means we can't accept arguments to the qmk script itself. | ||
| 54 | script_name = os.path.basename(sys.argv[0]) | ||
| 55 | if script_name == 'qmk': | ||
| 56 | if len(sys.argv) == 1: | ||
| 57 | milc.cli.log.error('No subcommand specified!\n') | ||
| 58 | |||
| 59 | if len(sys.argv) == 1 or sys.argv[1] in ['-h', '--help']: | ||
| 60 | milc.cli.echo('usage: qmk <subcommand> [...]') | ||
| 61 | milc.cli.echo('\nsubcommands:') | ||
| 62 | subcommands = glob(os.path.join(qmk_dir, 'bin', 'qmk-*')) | ||
| 63 | for subcommand in sorted(subcommands): | ||
| 64 | subcommand = os.path.basename(subcommand).split('-', 1)[1] | ||
| 65 | milc.cli.echo('\t%s', subcommand) | ||
| 66 | milc.cli.echo('\nqmk <subcommand> --help for more information') | ||
| 67 | exit(1) | ||
| 68 | 47 | ||
| 69 | if sys.argv[1] in ['-V', '--version']: | ||
| 70 | milc.cli.echo(os.environ['QMK_VERSION']) | ||
| 71 | exit(0) | ||
| 72 | 48 | ||
| 73 | sys.argv[0] = script_name = '-'.join((script_name, sys.argv[1])) | 49 | @milc.cli.entrypoint('QMK Helper Script') |
| 74 | del sys.argv[1] | 50 | def qmk_main(cli): |
| 51 | """The function that gets run when no subcommand is provided. | ||
| 52 | """ | ||
| 53 | cli.print_help() | ||
| 75 | 54 | ||
| 76 | # Look for which module to import | ||
| 77 | if script_name == 'qmk': | ||
| 78 | milc.cli.print_help() | ||
| 79 | exit(0) | ||
| 80 | elif not script_name.startswith('qmk-'): | ||
| 81 | milc.cli.log.error('Invalid symlink, must start with "qmk-": %s', script_name) | ||
| 82 | else: | ||
| 83 | subcommand = script_name.replace('-', '.').replace('_', '.').split('.') | ||
| 84 | subcommand.insert(1, 'cli') | ||
| 85 | subcommand = '.'.join(subcommand) | ||
| 86 | 55 | ||
| 87 | try: | 56 | def main(): |
| 88 | import_module(subcommand) | 57 | """Setup our environment and then call the CLI entrypoint. |
| 89 | except ModuleNotFoundError as e: | 58 | """ |
| 90 | if e.__class__.__name__ != subcommand: | 59 | # Change to the root of our checkout |
| 91 | raise | 60 | os.environ['ORIG_CWD'] = os.getcwd() |
| 61 | os.chdir(qmk_dir) | ||
| 92 | 62 | ||
| 93 | milc.cli.log.error('Invalid subcommand! Could not import %s.', subcommand) | 63 | # Import the subcommands |
| 94 | exit(1) | 64 | import qmk.cli |
| 95 | 65 | ||
| 96 | if __name__ == '__main__': | 66 | # Execute |
| 97 | return_code = milc.cli() | 67 | return_code = milc.cli() |
| 68 | |||
| 98 | if return_code is False: | 69 | if return_code is False: |
| 99 | exit(1) | 70 | exit(1) |
| 100 | elif return_code is not True and isinstance(return_code, int) and return_code < 256: | 71 | |
| 72 | elif return_code is not True and isinstance(return_code, int): | ||
| 73 | if return_code < 0 or return_code > 255: | ||
| 74 | milc.cli.log.error('Invalid return_code: %d', return_code) | ||
| 75 | exit(255) | ||
| 76 | |||
| 101 | exit(return_code) | 77 | exit(return_code) |
| 102 | else: | 78 | |
| 103 | exit(0) | 79 | exit(0) |
| 80 | |||
| 81 | |||
| 82 | if __name__ == '__main__': | ||
| 83 | main() | ||
