2 """CLI wrapper for running QMK commands.
8 from time import strftime
9 from importlib import import_module
10 from importlib.util import find_spec
12 # Add the QMK python libs to our path
13 script_dir = os.path.dirname(os.path.realpath(__file__))
14 qmk_dir = os.path.abspath(os.path.join(script_dir, '..'))
15 python_lib_dir = os.path.abspath(os.path.join(qmk_dir, 'lib', 'python'))
16 sys.path.append(python_lib_dir)
18 # Change to the root of our checkout
19 os.environ['ORIG_CWD'] = os.getcwd()
22 # Make sure our modules have been setup
23 with open('requirements.txt', 'r') as fd:
24 for line in fd.readlines():
25 line = line.strip().replace('<', '=').replace('>', '=')
31 line = line.split('#')[0]
33 module = line.split('=')[0] if '=' in line else line
34 if not find_spec(module):
35 print('Your QMK build environment is not fully setup!\n')
36 print('Please run `./util/qmk_install.sh` to setup QMK.')
39 # Figure out our version
40 command = ['git', 'describe', '--abbrev=6', '--dirty', '--always', '--tags']
41 result = subprocess.run(command, text=True, capture_output=True)
43 if result.returncode == 0:
44 os.environ['QMK_VERSION'] = 'QMK ' + result.stdout.strip()
46 os.environ['QMK_VERSION'] = 'QMK ' + strftime('%Y-%m-%d-%H:%M:%S')
50 milc.EMOJI_LOGLEVELS['INFO'] = '{fg_blue}ψ{style_reset_all}'
52 # If we were invoked as `qmk <cmd>` massage sys.argv into `qmk-<cmd>`.
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')
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')
69 if sys.argv[1] in ['-V', '--version']:
70 milc.cli.echo(os.environ['QMK_VERSION'])
73 sys.argv[0] = script_name = '-'.join((script_name, sys.argv[1]))
76 # Look for which module to import
77 if script_name == 'qmk':
80 elif not script_name.startswith('qmk-'):
81 milc.cli.log.error('Invalid symlink, must start with "qmk-": %s', script_name)
83 subcommand = script_name.replace('-', '.').replace('_', '.').split('.')
84 subcommand.insert(1, 'cli')
85 subcommand = '.'.join(subcommand)
88 import_module(subcommand)
89 except ModuleNotFoundError as e:
90 if e.__class__.__name__ != subcommand:
93 milc.cli.log.error('Invalid subcommand! Could not import %s.', subcommand)
96 if __name__ == '__main__':