2020-02-17 20:42:11 +01:00
|
|
|
"""Helper functions for commands.
|
2019-10-05 08:38:34 +02:00
|
|
|
"""
|
|
|
|
import json
|
2020-03-29 14:29:44 +02:00
|
|
|
import os
|
|
|
|
import platform
|
|
|
|
import subprocess
|
|
|
|
import shlex
|
2020-04-13 18:44:27 +02:00
|
|
|
import shutil
|
2021-01-17 00:13:04 +01:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
from milc import cli
|
2020-02-17 20:42:11 +01:00
|
|
|
|
2019-10-05 08:38:34 +02:00
|
|
|
import qmk.keymap
|
2021-01-17 00:13:04 +01:00
|
|
|
from qmk.constants import KEYBOARD_OUTPUT_PREFIX
|
|
|
|
|
|
|
|
|
|
|
|
def _find_make():
|
|
|
|
"""Returns the correct make command for this environment.
|
|
|
|
"""
|
|
|
|
make_cmd = os.environ.get('MAKE')
|
|
|
|
|
|
|
|
if not make_cmd:
|
|
|
|
make_cmd = 'gmake' if shutil.which('gmake') else 'make'
|
2019-10-05 08:38:34 +02:00
|
|
|
|
2021-01-17 00:13:04 +01:00
|
|
|
return make_cmd
|
2019-11-16 08:10:19 +01:00
|
|
|
|
2021-01-17 00:13:04 +01:00
|
|
|
|
|
|
|
def create_make_command(keyboard, keymap, target=None, parallel=1, **env_vars):
|
2019-10-05 08:38:34 +02:00
|
|
|
"""Create a make compile command
|
|
|
|
|
|
|
|
Args:
|
2020-02-17 20:42:11 +01:00
|
|
|
|
2019-10-05 08:38:34 +02:00
|
|
|
keyboard
|
|
|
|
The path of the keyboard, for example 'plank'
|
|
|
|
|
|
|
|
keymap
|
|
|
|
The name of the keymap, for example 'algernon'
|
|
|
|
|
|
|
|
target
|
|
|
|
Usually a bootloader.
|
|
|
|
|
2021-01-17 00:13:04 +01:00
|
|
|
parallel
|
|
|
|
The number of make jobs to run in parallel
|
|
|
|
|
|
|
|
**env_vars
|
|
|
|
Environment variables to be passed to make.
|
|
|
|
|
2019-10-05 08:38:34 +02:00
|
|
|
Returns:
|
2020-02-17 20:42:11 +01:00
|
|
|
|
2019-10-05 08:38:34 +02:00
|
|
|
A command that can be run to make the specified keyboard and keymap
|
|
|
|
"""
|
2021-01-17 00:13:04 +01:00
|
|
|
env = []
|
2020-02-17 20:42:11 +01:00
|
|
|
make_args = [keyboard, keymap]
|
2021-01-17 00:13:04 +01:00
|
|
|
make_cmd = _find_make()
|
2019-10-05 08:38:34 +02:00
|
|
|
|
2020-02-17 20:42:11 +01:00
|
|
|
if target:
|
|
|
|
make_args.append(target)
|
2019-11-16 08:10:19 +01:00
|
|
|
|
2021-01-17 00:13:04 +01:00
|
|
|
for key, value in env_vars.items():
|
|
|
|
env.append(f'{key}={value}')
|
2019-10-05 08:38:34 +02:00
|
|
|
|
2021-01-17 00:13:04 +01:00
|
|
|
return [make_cmd, '-j', str(parallel), *env, ':'.join(make_args)]
|
2019-11-16 08:10:19 +01:00
|
|
|
|
2021-01-17 00:13:04 +01:00
|
|
|
|
|
|
|
def compile_configurator_json(user_keymap, parallel=1, **env_vars):
|
|
|
|
"""Convert a configurator export JSON file into a C file and then compile it.
|
2019-10-05 08:38:34 +02:00
|
|
|
|
|
|
|
Args:
|
2020-02-17 20:42:11 +01:00
|
|
|
|
2021-01-17 00:13:04 +01:00
|
|
|
user_keymap
|
|
|
|
A deserialized keymap export
|
2019-10-05 08:38:34 +02:00
|
|
|
|
|
|
|
bootloader
|
|
|
|
A bootloader to flash
|
|
|
|
|
2021-01-17 00:13:04 +01:00
|
|
|
parallel
|
|
|
|
The number of make jobs to run in parallel
|
|
|
|
|
2019-10-05 08:38:34 +02:00
|
|
|
Returns:
|
2020-02-17 20:42:11 +01:00
|
|
|
|
2019-10-05 08:38:34 +02:00
|
|
|
A command to run to compile and flash the C file.
|
|
|
|
"""
|
2021-01-17 00:13:04 +01:00
|
|
|
# Write the keymap.c file
|
|
|
|
keyboard_filesafe = user_keymap['keyboard'].replace('/', '_')
|
|
|
|
target = f'{keyboard_filesafe}_{user_keymap["keymap"]}'
|
|
|
|
keyboard_output = Path(f'{KEYBOARD_OUTPUT_PREFIX}{keyboard_filesafe}')
|
|
|
|
keymap_output = Path(f'{keyboard_output}_{user_keymap["keymap"]}')
|
|
|
|
c_text = qmk.keymap.generate_c(user_keymap['keyboard'], user_keymap['layout'], user_keymap['layers'])
|
|
|
|
keymap_dir = keymap_output / 'src'
|
|
|
|
keymap_c = keymap_dir / 'keymap.c'
|
|
|
|
|
|
|
|
keymap_dir.mkdir(exist_ok=True, parents=True)
|
|
|
|
keymap_c.write_text(c_text)
|
2019-10-05 08:38:34 +02:00
|
|
|
|
|
|
|
# Return a command that can be run to make the keymap and flash if given
|
2021-01-17 00:13:04 +01:00
|
|
|
verbose = 'true' if cli.config.general.verbose else 'false'
|
|
|
|
color = 'true' if cli.config.general.color else 'false'
|
|
|
|
make_command = [
|
|
|
|
_find_make(),
|
|
|
|
'-j',
|
|
|
|
str(parallel),
|
|
|
|
'-r',
|
|
|
|
'-R',
|
|
|
|
'-f',
|
|
|
|
'build_keyboard.mk',
|
|
|
|
]
|
|
|
|
|
|
|
|
for key, value in env_vars.items():
|
|
|
|
make_command.append(f'{key}={value}')
|
|
|
|
|
|
|
|
make_command.extend([
|
|
|
|
f'KEYBOARD={user_keymap["keyboard"]}',
|
|
|
|
f'KEYMAP={user_keymap["keymap"]}',
|
|
|
|
f'KEYBOARD_FILESAFE={keyboard_filesafe}',
|
|
|
|
f'TARGET={target}',
|
|
|
|
f'KEYBOARD_OUTPUT={keyboard_output}',
|
|
|
|
f'KEYMAP_OUTPUT={keymap_output}',
|
|
|
|
f'MAIN_KEYMAP_PATH_1={keymap_output}',
|
|
|
|
f'MAIN_KEYMAP_PATH_2={keymap_output}',
|
|
|
|
f'MAIN_KEYMAP_PATH_3={keymap_output}',
|
|
|
|
f'MAIN_KEYMAP_PATH_4={keymap_output}',
|
|
|
|
f'MAIN_KEYMAP_PATH_5={keymap_output}',
|
|
|
|
f'KEYMAP_C={keymap_c}',
|
|
|
|
f'KEYMAP_PATH={keymap_dir}',
|
|
|
|
f'VERBOSE={verbose}',
|
|
|
|
f'COLOR={color}',
|
|
|
|
'SILENT=false',
|
|
|
|
])
|
|
|
|
|
|
|
|
return make_command
|
2020-02-17 20:42:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
def parse_configurator_json(configurator_file):
|
|
|
|
"""Open and parse a configurator json export
|
|
|
|
"""
|
2020-05-26 22:05:41 +02:00
|
|
|
# FIXME(skullydazed/anyone): Add validation here
|
2020-02-17 20:42:11 +01:00
|
|
|
user_keymap = json.load(configurator_file)
|
|
|
|
|
|
|
|
return user_keymap
|
2020-03-29 14:29:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
def run(command, *args, **kwargs):
|
|
|
|
"""Run a command with subprocess.run
|
|
|
|
"""
|
|
|
|
platform_id = platform.platform().lower()
|
|
|
|
|
|
|
|
if isinstance(command, str):
|
|
|
|
raise TypeError('`command` must be a non-text sequence such as list or tuple.')
|
|
|
|
|
|
|
|
if 'windows' in platform_id:
|
2020-10-23 03:21:32 +02:00
|
|
|
safecmd = map(str, command)
|
|
|
|
safecmd = map(shlex.quote, safecmd)
|
2020-03-29 14:29:44 +02:00
|
|
|
safecmd = ' '.join(safecmd)
|
|
|
|
command = [os.environ['SHELL'], '-c', safecmd]
|
|
|
|
|
|
|
|
return subprocess.run(command, *args, **kwargs)
|