diff options
author | skullydazed <skullydazed@users.noreply.github.com> | 2020-02-17 11:42:11 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-17 11:42:11 -0800 |
commit | c66930445f7d5941eb847568288046d51f853786 (patch) | |
tree | 273f290ca81a27bbbe7bdbf90221c02ac11f42cd /lib/python/qmk/cli/compile.py | |
parent | 58724f8dcb9eccb1c132b8ddab422790ddd26be0 (diff) | |
download | qmk_firmware-c66930445f7d5941eb847568288046d51f853786.tar.gz qmk_firmware-c66930445f7d5941eb847568288046d51f853786.zip |
Use pathlib everywhere we can (#7872)
* Use pathlib everywhere we can
* Update lib/python/qmk/path.py
Co-Authored-By: Erovia <Erovia@users.noreply.github.com>
* Update lib/python/qmk/path.py
Co-Authored-By: Erovia <Erovia@users.noreply.github.com>
* Improvements based on @erovia's feedback
* rework qmk compile and qmk flash to use pathlib
* style
* Remove the subcommand_name argument from find_keyboard_keymap()
Co-authored-by: Erovia <Erovia@users.noreply.github.com>
Diffstat (limited to 'lib/python/qmk/cli/compile.py')
-rwxr-xr-x | lib/python/qmk/cli/compile.py | 99 |
1 files changed, 12 insertions, 87 deletions
diff --git a/lib/python/qmk/cli/compile.py b/lib/python/qmk/cli/compile.py index 826b969ef..3068c97d8 100755 --- a/lib/python/qmk/cli/compile.py +++ b/lib/python/qmk/cli/compile.py | |||
@@ -3,16 +3,12 @@ | |||
3 | You can compile a keymap already in the repo or using a QMK Configurator export. | 3 | You can compile a keymap already in the repo or using a QMK Configurator export. |
4 | """ | 4 | """ |
5 | import subprocess | 5 | import subprocess |
6 | import os | ||
7 | from argparse import FileType | 6 | from argparse import FileType |
8 | 7 | ||
9 | from milc import cli | 8 | from milc import cli |
10 | from qmk.commands import create_make_command | ||
11 | from qmk.commands import parse_configurator_json | ||
12 | from qmk.commands import compile_configurator_json | ||
13 | 9 | ||
14 | import qmk.keymap | ||
15 | import qmk.path | 10 | import qmk.path |
11 | from qmk.commands import compile_configurator_json, create_make_command, find_keyboard_keymap, parse_configurator_json | ||
16 | 12 | ||
17 | 13 | ||
18 | @cli.argument('filename', nargs='?', arg_only=True, type=FileType('r'), help='The configurator export to compile') | 14 | @cli.argument('filename', nargs='?', arg_only=True, type=FileType('r'), help='The configurator export to compile') |
@@ -24,99 +20,28 @@ def compile(cli): | |||
24 | 20 | ||
25 | If a Configurator export is supplied this command will create a new keymap, overwriting an existing keymap if one exists. | 21 | If a Configurator export is supplied this command will create a new keymap, overwriting an existing keymap if one exists. |
26 | 22 | ||
27 | FIXME(skullydazed): add code to check and warn if the keymap already exists | 23 | If a keyboard and keymap are provided this command will build a firmware based on that. |
28 | |||
29 | If --keyboard and --keymap are provided this command will build a firmware based on that. | ||
30 | |||
31 | """ | 24 | """ |
32 | # Set CWD as directory command was issued from | ||
33 | cwd = os.environ['ORIG_CWD'] | ||
34 | qmk_path = os.getcwd() | ||
35 | current_folder = os.path.basename(cwd) | ||
36 | # Initialize boolean to check for being in a keyboard directory and initialize keyboard string | ||
37 | in_keyboard = False | ||
38 | in_layout = False | ||
39 | keyboard = "" | ||
40 | keymap = "" | ||
41 | user_keymap = "" | ||
42 | user_keyboard = "" | ||
43 | |||
44 | # Set path for '/keyboards/' directory | ||
45 | keyboards_path = os.path.join(qmk_path, "keyboards") | ||
46 | layouts_path = os.path.join(qmk_path, "layouts") | ||
47 | |||
48 | # If below 'keyboards' and not in 'keyboards' or 'keymaps', get current keyboard name | ||
49 | if cwd.startswith(keyboards_path): | ||
50 | if current_folder != "keyboards" and current_folder != "keymaps": | ||
51 | if os.path.basename(os.path.abspath(os.path.join(cwd, ".."))) == "keymaps": | ||
52 | # If in a keymap folder, set relative path, get everything before /keymaps, and the keymap name | ||
53 | relative_path = cwd[len(keyboards_path):][1:] | ||
54 | keyboard = str(relative_path).split("/keymaps", 1)[0] | ||
55 | keymap = str(relative_path.rsplit("/", 1)[-1]) | ||
56 | else: | ||
57 | keyboard = str(cwd[len(keyboards_path):])[1:] | ||
58 | |||
59 | in_keyboard = True | ||
60 | |||
61 | # If in layouts dir | ||
62 | if cwd.startswith(layouts_path): | ||
63 | if current_folder != "layouts": | ||
64 | in_layout = True | ||
65 | |||
66 | # If user keyboard/keymap or compile keyboard/keymap are supplied, assign those | ||
67 | if cli.config.compile.keyboard: | ||
68 | user_keyboard = cli.config.compile.keyboard | ||
69 | if cli.config.compile.keymap and not in_layout: | ||
70 | user_keymap = cli.config.compile.keymap | ||
71 | |||
72 | if cli.args.filename: | 25 | if cli.args.filename: |
73 | # Parse the configurator json | 26 | # If a configurator JSON was provided skip straight to compiling it |
27 | # FIXME(skullydazed): add code to check and warn if the keymap already exists when compiling a json keymap. | ||
74 | user_keymap = parse_configurator_json(cli.args.filename) | 28 | user_keymap = parse_configurator_json(cli.args.filename) |
75 | |||
76 | # Generate the keymap | ||
77 | keymap_path = qmk.path.keymap(user_keymap['keyboard']) | 29 | keymap_path = qmk.path.keymap(user_keymap['keyboard']) |
78 | cli.log.info('Creating {fg_cyan}%s{style_reset_all} keymap in {fg_cyan}%s', user_keymap['keymap'], keymap_path) | ||
79 | |||
80 | # Compile the keymap | ||
81 | command = compile_configurator_json(user_keymap) | 30 | command = compile_configurator_json(user_keymap) |
82 | 31 | ||
83 | cli.log.info('Wrote keymap to {fg_cyan}%s/%s/keymap.c', keymap_path, user_keymap['keymap']) | 32 | cli.log.info('Wrote keymap to {fg_cyan}%s/%s/keymap.c', keymap_path, user_keymap['keymap']) |
84 | 33 | ||
85 | elif user_keyboard and user_keymap: | 34 | else: |
86 | # Generate the make command for a specific keyboard/keymap. | 35 | # Perform the action the user specified |
87 | command = create_make_command(user_keyboard, user_keymap) | 36 | user_keyboard, user_keymap = find_keyboard_keymap() |
88 | 37 | if user_keyboard and user_keymap: | |
89 | elif in_keyboard: | 38 | # Generate the make command for a specific keyboard/keymap. |
90 | keyboard = user_keyboard if user_keyboard else keyboard | 39 | command = create_make_command(user_keyboard, user_keymap) |
91 | keymap = user_keymap if user_keymap else keymap | ||
92 | |||
93 | if not os.path.exists(os.path.join(keyboards_path, keyboard, "rules.mk")): | ||
94 | cli.log.error('This directory does not contain a rules.mk file. Change directory or supply --keyboard with optional --keymap') | ||
95 | return False | ||
96 | |||
97 | # Get path for keyboard directory | ||
98 | keymap_path = qmk.path.keymap(keyboard) | ||
99 | |||
100 | # Check for global keymap config first | ||
101 | if keymap: | ||
102 | command = create_make_command(keyboard, keymap) | ||
103 | |||
104 | else: | ||
105 | # If no default keymap exists and none provided | ||
106 | cli.log.error('This directory does not contain a keymap. Set one with `qmk config` or supply `--keymap` ') | ||
107 | return False | ||
108 | 40 | ||
109 | elif in_layout: | ||
110 | if user_keyboard: | ||
111 | keymap = current_folder | ||
112 | command = create_make_command(user_keyboard, keymap) | ||
113 | else: | 41 | else: |
114 | cli.log.error('You must supply a keyboard to compile a layout keymap. Set one with `qmk config` or supply `--keyboard` ') | 42 | cli.log.error('You must supply a configurator export, both `--keyboard` and `--keymap`, or be in a directory for a keyboard or keymap.') |
43 | cli.echo('usage: qmk compile [-h] [-b] [-kb KEYBOARD] [-km KEYMAP] [filename]') | ||
115 | return False | 44 | return False |
116 | 45 | ||
117 | else: | ||
118 | cli.log.error('You must supply a configurator export, both `--keyboard` and `--keymap`, or be in a directory for a keyboard or keymap.') | ||
119 | return False | ||
120 | |||
121 | cli.log.info('Compiling keymap with {fg_cyan}%s\n\n', ' '.join(command)) | 46 | cli.log.info('Compiling keymap with {fg_cyan}%s\n\n', ' '.join(command)) |
122 | subprocess.run(command) | 47 | subprocess.run(command) |