unify the compile and flash commands

This commit is contained in:
Zach White 2021-06-26 19:46:00 -07:00
parent ea862e24f6
commit 4f20c94b97
3 changed files with 49 additions and 79 deletions

View file

@ -13,6 +13,7 @@ from qmk.decorators import automagic_keyboard, automagic_keymap
from qmk.commands import do_compile
from qmk.keyboard import keyboard_completer, is_keyboard_target
from qmk.keymap import keymap_completer
from qmk.metadata import true_values, false_values
@cli.argument('filename', nargs='?', arg_only=True, type=qmk.path.FileType('r'), completer=FilesCompleter('.json'), help='The configurator export to compile')
@ -34,4 +35,31 @@ def compile(cli):
If a keyboard and keymap are provided this command will build a firmware based on that.
"""
do_compile(cli.config.compile.keyboard, cli.config.compile.keymap, cli.config.compile.parallel, cli.config.compile.target)
# If -f has been specified without a keyboard target, assume -kb all
keyboard = cli.config.compile.keyboard or ''
if cli.args.filter and not cli.args.keyboard:
cli.log.debug('--filter supplied without --keyboard, assuming --keyboard all.')
keyboard = 'all'
if cli.args.filename and cli.args.filter:
cli.log.warning('Ignoring --filter because a keymap.json was provided.')
filters = {}
for filter in cli.args.filter:
if '=' in filter:
key, value = filter.split('=', 1)
if value in true_values:
value = True
elif value in false_values:
value = False
elif value.isdigit():
value = int(value)
elif '.' in value and value.replace('.').isdigit():
value = float(value)
filters[key] = value
return do_compile(keyboard, cli.config.compile.keymap, cli.config.compile.parallel, cli.config.compile.target, filters)

View file

@ -10,7 +10,7 @@ from milc import cli
import qmk.path
from qmk.decorators import automagic_keyboard, automagic_keymap
from qmk.commands import compile_configurator_json, create_make_command, parse_configurator_json
from qmk.commands import do_compile
from qmk.keyboard import keyboard_completer, is_keyboard_target
@ -54,55 +54,10 @@ def flash(cli):
If bootloader is omitted the make system will use the configured bootloader for that keyboard.
"""
if cli.args.clean and not cli.args.filename and not cli.args.dry_run:
command = create_make_command(cli.config.flash.keyboard, cli.config.flash.keymap, 'clean')
cli.run(command, capture_output=False, stdin=DEVNULL)
# Build the environment vars
envs = {}
for env in cli.args.env:
if '=' in env:
key, value = env.split('=', 1)
envs[key] = value
else:
cli.log.warning('Invalid environment variable: %s', env)
# Determine the compile command
command = ''
if cli.args.bootloaders:
# Provide usage and list bootloaders
cli.echo('usage: qmk flash [-h] [-b] [-n] [-kb KEYBOARD] [-km KEYMAP] [-bl BOOTLOADER] [filename]')
cli.print_usage()
print_bootloader_help()
return False
if cli.args.filename:
# Handle compiling a configurator JSON
user_keymap = parse_configurator_json(cli.args.filename)
keymap_path = qmk.path.keymap(user_keymap['keyboard'])
command = compile_configurator_json(user_keymap, cli.args.bootloader, parallel=cli.config.flash.parallel, **envs)
cli.log.info('Wrote keymap to {fg_cyan}%s/%s/keymap.c', keymap_path, user_keymap['keymap'])
else:
if cli.config.flash.keyboard and cli.config.flash.keymap:
# Generate the make command for a specific keyboard/keymap.
command = create_make_command(cli.config.flash.keyboard, cli.config.flash.keymap, cli.args.bootloader, parallel=cli.config.flash.parallel, **envs)
elif not cli.config.flash.keyboard:
cli.log.error('Could not determine keyboard!')
elif not cli.config.flash.keymap:
cli.log.error('Could not determine keymap!')
# Compile the firmware, if we're able to
if command:
cli.log.info('Compiling keymap with {fg_cyan}%s', ' '.join(command))
if not cli.args.dry_run:
cli.echo('\n')
compile = cli.run(command, capture_output=False, stdin=DEVNULL)
return compile.returncode
else:
cli.log.error('You must supply a configurator export, both `--keyboard` and `--keymap`, or be in a directory for a keyboard or keymap.')
cli.echo('usage: qmk flash [-h] [-b] [-n] [-kb KEYBOARD] [-km KEYMAP] [-bl BOOTLOADER] [filename]')
return False
return do_compile(cli.config.flash.keyboard, cli.config.flash.keymap, cli.config.flash.parallel, cli.config.flash.bootloader)

View file

@ -353,17 +353,20 @@ def in_virtualenv():
return active_prefix != sys.prefix
def do_compile(keyboard, keymap, parallel, target=None):
def do_compile(keyboard, keymap, parallel, target=None, filters=None, environment=None):
"""Shared code between `qmk compile` and `qmk flash`.
"""
if keyboard is None:
keyboard = ''
if environment is None:
environment = {}
envs = {'REQUIRE_PLATFORM_KEY': ''}
silent = keyboard == 'all' or keyboard.startswith('all-') or keymap == 'all'
# Setup the environment
for env in cli.args.env:
for env in environment:
if '=' in env:
key, value = env.split('=', 1)
if key in envs:
@ -382,18 +385,10 @@ def do_compile(keyboard, keymap, parallel, target=None):
_, _, make_cmd = create_make_command(keyboard, keymap, 'clean', parallel, silent, **envs)
cli.run(make_cmd, capture_output=False, stdin=DEVNULL)
# If -f has been specified without a keyboard target, assume -kb all
if cli.args.filter and not cli.args.keyboard:
cli.log.debug('--filter supplied without --keyboard, assuming --keyboard all.')
keyboard = 'all'
# Determine the compile command(s)
commands = None
if cli.args.filename:
if cli.args.filter:
cli.log.warning('Ignoring --filter because a keymap.json was provided.')
if cli.args.keyboard:
cli.log.warning('Ignoring --keyboard because a keymap.json was provided.')
@ -405,10 +400,10 @@ def do_compile(keyboard, keymap, parallel, target=None):
commands = [compile_configurator_json(user_keymap, parallel=parallel, **envs)]
elif keyboard and keymap:
if cli.args.filter:
if filters:
cli.log.info('Generating the list of keyboards to compile, this may take some time.')
commands = [create_make_command(keyboard, keymap, target=target, parallel=parallel, silent=silent, **envs) for keyboard, keymap in keyboard_keymap_iter(keyboard, keymap)]
commands = [create_make_command(keyboard, keymap, target=target, parallel=parallel, silent=silent, **envs) for keyboard, keymap in keyboard_keymap_iter(keyboard, keymap, filters)]
elif not keyboard:
cli.log.error('Could not determine keyboard!')
@ -448,7 +443,7 @@ def do_compile(keyboard, keymap, parallel, target=None):
keyboard, keymap, command = commands[i]
cli.echo('\tkeyboard: {fg_cyan}%s{fg_reset} keymap: {fg_cyan}%s', keyboard, keymap)
elif cli.args.filter:
elif filters:
cli.log.error('No keyboards found after applying filter(s)!')
return False
@ -468,28 +463,19 @@ def _keyboard_list(keyboard):
return [keyboard]
def keyboard_keymap_iter(cli_keyboard, cli_keymap):
def keyboard_keymap_iter(cli_keyboard, cli_keymap, filters):
"""Iterates over the keyboard/keymap for this command and yields a pairing of each.
"""
for keyboard in _keyboard_list(cli_keyboard):
continue_flag = False
if cli.args.filter:
info_data = dotty(info_json(keyboard))
for filter in cli.args.filter:
if '=' in filter:
key, value = filter.split('=', 1)
if value in true_values:
value = True
elif value in false_values:
value = False
elif value.isdigit():
value = int(value)
elif '.' in value and value.replace('.').isdigit():
value = float(value)
if info_data.get(key) != value:
continue_flag = True
break
if filters:
info_data = dotty(info_json(keyboard))
for key, value in filters.items():
if info_data.get(key) != value:
continue_flag = True
break
if continue_flag:
continue
@ -497,5 +483,6 @@ def keyboard_keymap_iter(cli_keyboard, cli_keymap):
if cli_keymap == 'all':
for keymap in qmk.keymap.list_keymaps(keyboard):
yield keyboard, keymap
else:
yield keyboard, cli_keymap