]> git.donarmstrong.com Git - qmk_firmware.git/blob - bin/qmk
QMK CLI and JSON keymap support (#6176)
[qmk_firmware.git] / bin / qmk
1 #!/usr/bin/env python3
2 """CLI wrapper for running QMK commands.
3 """
4 import os
5 import subprocess
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
11
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)
17
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
23 with open('requirements.txt', 'r') as fd:
24     for line in fd.readlines():
25         line = line.strip().replace('<', '=').replace('>', '=')
26
27         if line[0] == '#':
28             continue
29
30         if '#' in line:
31             line = line.split('#')[0]
32
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.')
37             exit(255)
38
39 # Figure out our version
40 command = ['git', 'describe', '--abbrev=6', '--dirty', '--always', '--tags']
41 result = subprocess.run(command, text=True, capture_output=True)
42
43 if result.returncode == 0:
44     os.environ['QMK_VERSION'] = 'QMK ' + result.stdout.strip()
45 else:
46     os.environ['QMK_VERSION'] = 'QMK ' + strftime('%Y-%m-%d-%H:%M:%S')
47
48 # Setup the CLI
49 import milc
50 milc.EMOJI_LOGLEVELS['INFO'] = '{fg_blue}ψ{style_reset_all}'
51
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')
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
69     if sys.argv[1] in ['-V', '--version']:
70         milc.cli.echo(os.environ['QMK_VERSION'])
71         exit(0)
72
73     sys.argv[0] = script_name = '-'.join((script_name, sys.argv[1]))
74     del sys.argv[1]
75
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
87     try:
88         import_module(subcommand)
89     except ModuleNotFoundError as e:
90         if e.__class__.__name__ != subcommand:
91             raise
92
93         milc.cli.log.error('Invalid subcommand! Could not import %s.', subcommand)
94         exit(1)
95
96 if __name__ == '__main__':
97     milc.cli()