diff options
| author | jorgemanzo <jmanzo203689@gmail.com> | 2019-10-04 23:38:34 -0700 |
|---|---|---|
| committer | skullY <skullydazed@gmail.com> | 2019-11-15 23:06:07 -0800 |
| commit | 897888db419239f013561b155de5993b1966820e (patch) | |
| tree | dc9041ea683d9188961749aec7f511719fbde241 /lib | |
| parent | 4f5b34af565d00e069d3f37b3faa8091608ed21f (diff) | |
| download | qmk_firmware-897888db419239f013561b155de5993b1966820e.tar.gz qmk_firmware-897888db419239f013561b155de5993b1966820e.zip | |
Add CLI command for flashing a keyboard
A new CLI subcommand was added, flash, which behaves very similar to the already present compile CLI comamnd, but with the added ability to target a bootloader. The command is used like so: qmk flash [-h] [-b] [-kb KEYBOARD] [-km KEYMAP] [-bl BOOTLOADER] [filename].
A -kb <keyboard> and -km <keymap> is expected, or a configurator export JSON filename. A bootloader can be specified using -bl <target>, and if left unspecified, the target is assumed to be :flash. -bl can be used to list the available bootloaders.
If -km <keymap> is provided, but no -kb <keyboard>, then a message is printed suggesting the user to run qmk list_keyboards.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/python/qmk/cli/__init__.py | 1 | ||||
| -rwxr-xr-x | lib/python/qmk/cli/compile.py | 14 | ||||
| -rw-r--r-- | lib/python/qmk/cli/flash.py | 87 | ||||
| -rw-r--r-- | lib/python/qmk/commands.py | 57 | ||||
| -rw-r--r-- | lib/python/qmk/tests/test_cli_commands.py | 3 |
5 files changed, 157 insertions, 5 deletions
diff --git a/lib/python/qmk/cli/__init__.py b/lib/python/qmk/cli/__init__.py index 1b83e78c7..72ee38f56 100644 --- a/lib/python/qmk/cli/__init__.py +++ b/lib/python/qmk/cli/__init__.py | |||
| @@ -7,6 +7,7 @@ from . import compile | |||
| 7 | from . import config | 7 | from . import config |
| 8 | from . import docs | 8 | from . import docs |
| 9 | from . import doctor | 9 | from . import doctor |
| 10 | from . import flash | ||
| 10 | from . import hello | 11 | from . import hello |
| 11 | from . import json | 12 | from . import json |
| 12 | from . import list | 13 | from . import list |
diff --git a/lib/python/qmk/cli/compile.py b/lib/python/qmk/cli/compile.py index 6646891b3..c7093d421 100755 --- a/lib/python/qmk/cli/compile.py +++ b/lib/python/qmk/cli/compile.py | |||
| @@ -9,6 +9,9 @@ import subprocess | |||
| 9 | from argparse import FileType | 9 | from argparse import FileType |
| 10 | 10 | ||
| 11 | from milc import cli | 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 | ||
| 12 | 15 | ||
| 13 | import qmk.keymap | 16 | import qmk.keymap |
| 14 | import qmk.path | 17 | import qmk.path |
| @@ -30,20 +33,21 @@ def compile(cli): | |||
| 30 | """ | 33 | """ |
| 31 | if cli.args.filename: | 34 | if cli.args.filename: |
| 32 | # Parse the configurator json | 35 | # Parse the configurator json |
| 33 | user_keymap = json.load(cli.args.filename) | 36 | user_keymap = parse_configurator_json(cli.args.filename) |
| 34 | 37 | ||
| 35 | # Generate the keymap | 38 | # Generate the keymap |
| 36 | keymap_path = qmk.path.keymap(user_keymap['keyboard']) | 39 | keymap_path = qmk.path.keymap(user_keymap['keyboard']) |
| 37 | cli.log.info('Creating {fg_cyan}%s{style_reset_all} keymap in {fg_cyan}%s', user_keymap['keymap'], keymap_path) | 40 | cli.log.info('Creating {fg_cyan}%s{style_reset_all} keymap in {fg_cyan}%s', user_keymap['keymap'], keymap_path) |
| 38 | qmk.keymap.write(user_keymap['keyboard'], user_keymap['keymap'], user_keymap['layout'], user_keymap['layers']) | 41 | |
| 42 | # Compile the keymap | ||
| 43 | command = compile_configurator_json(cli.args.filename) | ||
| 44 | |||
| 39 | cli.log.info('Wrote keymap to {fg_cyan}%s/%s/keymap.c', keymap_path, user_keymap['keymap']) | 45 | cli.log.info('Wrote keymap to {fg_cyan}%s/%s/keymap.c', keymap_path, user_keymap['keymap']) |
| 40 | 46 | ||
| 41 | # Compile the keymap | ||
| 42 | command = ['make', ':'.join((user_keymap['keyboard'], user_keymap['keymap']))] | ||
| 43 | 47 | ||
| 44 | elif cli.config.compile.keyboard and cli.config.compile.keymap: | 48 | elif cli.config.compile.keyboard and cli.config.compile.keymap: |
| 45 | # Generate the make command for a specific keyboard/keymap. | 49 | # Generate the make command for a specific keyboard/keymap. |
| 46 | command = ['make', ':'.join((cli.config.compile.keyboard, cli.config.compile.keymap))] | 50 | command = create_make_command(cli.config.compile.keyboard, cli.config.compile.keymap) |
| 47 | 51 | ||
| 48 | else: | 52 | else: |
| 49 | cli.log.error('You must supply a configurator export or both `--keyboard` and `--keymap`.') | 53 | cli.log.error('You must supply a configurator export or both `--keyboard` and `--keymap`.') |
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 | ||
diff --git a/lib/python/qmk/commands.py b/lib/python/qmk/commands.py new file mode 100644 index 000000000..9fbf00f16 --- /dev/null +++ b/lib/python/qmk/commands.py | |||
| @@ -0,0 +1,57 @@ | |||
| 1 | """Functions that build make commands | ||
| 2 | """ | ||
| 3 | import json | ||
| 4 | import qmk.keymap | ||
| 5 | |||
| 6 | def create_make_command(keyboard, keymap, target=None): | ||
| 7 | """Create a make compile command | ||
| 8 | |||
| 9 | Args: | ||
| 10 | keyboard | ||
| 11 | The path of the keyboard, for example 'plank' | ||
| 12 | |||
| 13 | keymap | ||
| 14 | The name of the keymap, for example 'algernon' | ||
| 15 | |||
| 16 | target | ||
| 17 | Usually a bootloader. | ||
| 18 | |||
| 19 | Returns: | ||
| 20 | A command that can be run to make the specified keyboard and keymap | ||
| 21 | """ | ||
| 22 | if target is None: | ||
| 23 | return ['make', ':'.join((keyboard, keymap))] | ||
| 24 | return ['make', ':'.join((keyboard, keymap, target))] | ||
| 25 | |||
| 26 | def parse_configurator_json(configurator_filename): | ||
| 27 | """Open and parse a configurator json export | ||
| 28 | """ | ||
| 29 | file = open(configurator_filename) | ||
| 30 | user_keymap = json.load(file) | ||
| 31 | file.close() | ||
| 32 | return user_keymap | ||
| 33 | |||
| 34 | def compile_configurator_json(configurator_filename, bootloader=None): | ||
| 35 | """Convert a configurator export JSON file into a C file | ||
| 36 | |||
| 37 | Args: | ||
| 38 | configurator_filename | ||
| 39 | The configurator JSON export file | ||
| 40 | |||
| 41 | bootloader | ||
| 42 | A bootloader to flash | ||
| 43 | |||
| 44 | Returns: | ||
| 45 | A command to run to compile and flash the C file. | ||
| 46 | """ | ||
| 47 | # Parse the configurator json | ||
| 48 | user_keymap = parse_configurator_json(configurator_filename) | ||
| 49 | |||
| 50 | # Write the keymap C file | ||
| 51 | qmk.keymap.write(user_keymap['keyboard'], user_keymap['keymap'], user_keymap['layout'], user_keymap['layers']) | ||
| 52 | |||
| 53 | # Return a command that can be run to make the keymap and flash if given | ||
| 54 | if bootloader is None: | ||
| 55 | return create_make_command(user_keymap['keyboard'], user_keymap['keymap']) | ||
| 56 | return create_make_command(user_keymap['keyboard'], user_keymap['keymap'], bootloader) | ||
| 57 | |||
diff --git a/lib/python/qmk/tests/test_cli_commands.py b/lib/python/qmk/tests/test_cli_commands.py index 85d4d91af..dcab8bdae 100644 --- a/lib/python/qmk/tests/test_cli_commands.py +++ b/lib/python/qmk/tests/test_cli_commands.py | |||
| @@ -13,6 +13,9 @@ def test_cformat(): | |||
| 13 | def test_compile(): | 13 | def test_compile(): |
| 14 | assert check_subcommand('compile', '-kb', 'handwired/onekey/pytest', '-km', 'default').returncode == 0 | 14 | assert check_subcommand('compile', '-kb', 'handwired/onekey/pytest', '-km', 'default').returncode == 0 |
| 15 | 15 | ||
| 16 | def test_flash(): | ||
| 17 | assert check_subcommand('flash', '-b').returncode == 1 | ||
| 18 | assert check_subcommand('flash').returncode == 1 | ||
| 16 | 19 | ||
| 17 | def test_config(): | 20 | def test_config(): |
| 18 | result = check_subcommand('config') | 21 | result = check_subcommand('config') |
