diff options
Diffstat (limited to 'lib/python/qmk/cli/flash.py')
-rw-r--r-- | lib/python/qmk/cli/flash.py | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/lib/python/qmk/cli/flash.py b/lib/python/qmk/cli/flash.py new file mode 100644 index 000000000..8f7bb55a2 --- /dev/null +++ b/lib/python/qmk/cli/flash.py | |||
@@ -0,0 +1,87 @@ | |||
1 | """Compile and flash QMK Firmware | ||
2 | |||
3 | You can compile a keymap already in the repo or using a QMK Configurator export. | ||
4 | A bootloader must be specified. | ||
5 | """ | ||
6 | import os | ||
7 | import sys | ||
8 | import subprocess | ||
9 | from argparse import FileType | ||
10 | |||
11 | from milc import cli | ||
12 | from qmk.commands import create_make_command | ||
13 | from qmk.commands import parse_configurator_json | ||
14 | from qmk.commands import compile_configurator_json | ||
15 | |||
16 | import qmk.path | ||
17 | |||
18 | |||
19 | def print_bootloader_help(): | ||
20 | """Prints the available bootloaders listed in docs.qmk.fm. | ||
21 | """ | ||
22 | cli.log.info('Here are the available bootloaders:') | ||
23 | cli.echo('\tdfu') | ||
24 | cli.echo('\tdfu-ee') | ||
25 | cli.echo('\tdfu-split-left') | ||
26 | cli.echo('\tdfu-split-right') | ||
27 | cli.echo('\tavrdude') | ||
28 | cli.echo('\tBootloadHID') | ||
29 | cli.echo('\tdfu-util') | ||
30 | cli.echo('\tdfu-util-split-left') | ||
31 | cli.echo('\tdfu-util-split-right') | ||
32 | cli.echo('\tst-link-cli') | ||
33 | cli.echo('For more info, visit https://docs.qmk.fm/#/flashing') | ||
34 | |||
35 | @cli.argument('-bl', '--bootloader', default='flash', help='The flash command, corresponding to qmk\'s make options of bootloaders.') | ||
36 | @cli.argument('filename', nargs='?', arg_only=True, help='The configurator export JSON to compile. Use this if you dont want to specify a keymap and keyboard.') | ||
37 | @cli.argument('-km', '--keymap', help='The keymap to build a firmware for. Use this if you dont have a configurator file. Ignored when a configurator file is supplied.') | ||
38 | @cli.argument('-kb', '--keyboard', help='The keyboard to build a firmware for. Use this if you dont have a configurator file. Ignored when a configurator file is supplied.') | ||
39 | @cli.argument('-b', '--bootloaders', action='store_true', help='List the available bootloaders.') | ||
40 | @cli.subcommand('QMK Flash.') | ||
41 | def flash(cli): | ||
42 | """Compile and or flash QMK Firmware or keyboard/layout | ||
43 | |||
44 | If a Configurator JSON export is supplied this command will create a new keymap. Keymap and Keyboard arguments | ||
45 | will be ignored. | ||
46 | |||
47 | If no file is supplied, keymap and keyboard are expected. | ||
48 | |||
49 | If bootloader is omitted, the one according to the rules.mk will be used. | ||
50 | |||
51 | """ | ||
52 | command = [] | ||
53 | if cli.args.bootloaders: | ||
54 | # Provide usage and list bootloaders | ||
55 | cli.echo('usage: qmk flash [-h] [-b] [-kb KEYBOARD] [-km KEYMAP] [-bl BOOTLOADER] [filename]') | ||
56 | print_bootloader_help() | ||
57 | return False | ||
58 | |||
59 | elif cli.args.keymap and not cli.args.keyboard: | ||
60 | # If only a keymap was given but no keyboard, suggest listing keyboards | ||
61 | cli.echo('usage: qmk flash [-h] [-b] [-kb KEYBOARD] [-km KEYMAP] [-bl BOOTLOADER] [filename]') | ||
62 | cli.log.error('run \'qmk list_keyboards\' to find out the supported keyboards') | ||
63 | return False | ||
64 | |||
65 | elif cli.args.filename: | ||
66 | # Get keymap path to log info | ||
67 | user_keymap = parse_configurator_json(cli.args.filename) | ||
68 | keymap_path = qmk.path.keymap(user_keymap['keyboard']) | ||
69 | |||
70 | cli.log.info('Creating {fg_cyan}%s{style_reset_all} keymap in {fg_cyan}%s', user_keymap['keymap'], keymap_path) | ||
71 | |||
72 | # Convert the JSON into a C file and write it to disk. | ||
73 | command = compile_configurator_json(cli.args.filename, cli.args.bootloader) | ||
74 | |||
75 | cli.log.info('Wrote keymap to {fg_cyan}%s/%s/keymap.c', keymap_path, user_keymap['keymap']) | ||
76 | |||
77 | elif cli.args.keyboard and cli.args.keymap: | ||
78 | # Generate the make command for a specific keyboard/keymap. | ||
79 | command = create_make_command(cli.config.flash.keyboard, cli.config.flash.keymap, cli.args.bootloader) | ||
80 | |||
81 | else: | ||
82 | cli.echo('usage: qmk flash [-h] [-b] [-kb KEYBOARD] [-km KEYMAP] [-bl BOOTLOADER] [filename]') | ||
83 | cli.log.error('You must supply a configurator export or both `--keyboard` and `--keymap`. You can also specify a bootloader with --bootloader. Use --bootloaders to list the available bootloaders.') | ||
84 | return False | ||
85 | |||
86 | cli.log.info('Flashing keymap with {fg_cyan}%s\n\n', ' '.join(command)) | ||
87 | subprocess.run(command) \ No newline at end of file | ||