a25dd58b |
1 | #!/usr/bin/env python3 |
2 | """CLI wrapper for running QMK commands. |
3 | """ |
4 | import os |
5 | import subprocess |
6 | import sys |
a25dd58b |
7 | from importlib.util import find_spec |
d569f087 |
8 | from time import strftime |
a25dd58b |
9 | |
10 | # Add the QMK python libs to our path |
11 | script_dir = os.path.dirname(os.path.realpath(__file__)) |
12 | qmk_dir = os.path.abspath(os.path.join(script_dir, '..')) |
13 | python_lib_dir = os.path.abspath(os.path.join(qmk_dir, 'lib', 'python')) |
14 | sys.path.append(python_lib_dir) |
15 | |
a25dd58b |
16 | # Make sure our modules have been setup |
d569f087 |
17 | with open(os.path.join(qmk_dir, 'requirements.txt'), 'r') as fd: |
a25dd58b |
18 | for line in fd.readlines(): |
19 | line = line.strip().replace('<', '=').replace('>', '=') |
20 | |
21 | if line[0] == '#': |
22 | continue |
23 | |
24 | if '#' in line: |
25 | line = line.split('#')[0] |
26 | |
27 | module = line.split('=')[0] if '=' in line else line |
0ed49297 |
28 | |
29 | if module in ['pep8-naming']: |
30 | # Not every module is importable by its own name. |
31 | continue |
32 | |
a25dd58b |
33 | if not find_spec(module): |
74252e03 |
34 | print('Could not find module %s!' % module) |
d569f087 |
35 | print('Please run `pip3 install -r requirements.txt` to install the python dependencies.') |
a25dd58b |
36 | exit(255) |
37 | |
38 | # Figure out our version |
d569f087 |
39 | # TODO(skullydazed/anyone): Find a method that doesn't involve git. This is slow in docker and on windows. |
a25dd58b |
40 | command = ['git', 'describe', '--abbrev=6', '--dirty', '--always', '--tags'] |
d569f087 |
41 | result = subprocess.run(command, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
a25dd58b |
42 | |
43 | if result.returncode == 0: |
d569f087 |
44 | os.environ['QMK_VERSION'] = result.stdout.strip() |
a25dd58b |
45 | else: |
d569f087 |
46 | os.environ['QMK_VERSION'] = 'nogit-' + strftime('%Y-%m-%d-%H:%M:%S') + '-dirty' |
a25dd58b |
47 | |
48 | # Setup the CLI |
f7bdc54c |
49 | import milc # noqa |
a25dd58b |
50 | |
d569f087 |
51 | milc.EMOJI_LOGLEVELS['INFO'] = '{fg_blue}Ψ{style_reset_all}' |
a25dd58b |
52 | |
a25dd58b |
53 | |
d569f087 |
54 | @milc.cli.entrypoint('QMK Helper Script') |
55 | def qmk_main(cli): |
56 | """The function that gets run when no subcommand is provided. |
57 | """ |
58 | cli.print_help() |
a25dd58b |
59 | |
a25dd58b |
60 | |
d569f087 |
61 | def main(): |
62 | """Setup our environment and then call the CLI entrypoint. |
63 | """ |
64 | # Change to the root of our checkout |
65 | os.environ['ORIG_CWD'] = os.getcwd() |
66 | os.chdir(qmk_dir) |
a25dd58b |
67 | |
d569f087 |
68 | # Import the subcommands |
f7bdc54c |
69 | import qmk.cli # noqa |
a25dd58b |
70 | |
d569f087 |
71 | # Execute |
5b7a5b2a |
72 | return_code = milc.cli() |
d569f087 |
73 | |
5b7a5b2a |
74 | if return_code is False: |
75 | exit(1) |
d569f087 |
76 | |
77 | elif return_code is not True and isinstance(return_code, int): |
78 | if return_code < 0 or return_code > 255: |
79 | milc.cli.log.error('Invalid return_code: %d', return_code) |
80 | exit(255) |
81 | |
5b7a5b2a |
82 | exit(return_code) |
d569f087 |
83 | |
84 | exit(0) |
85 | |
86 | |
87 | if __name__ == '__main__': |
88 | main() |