2020-02-18 01:42:11 +06:00
|
|
|
"""Helper functions for commands.
|
2019-10-05 12:38:34 +06:00
|
|
|
"""
|
|
|
|
import json
|
2020-03-29 18:29:44 +06:00
|
|
|
import os
|
|
|
|
import platform
|
|
|
|
import subprocess
|
|
|
|
import shlex
|
2020-04-13 22:44:27 +06:00
|
|
|
import shutil
|
2020-02-18 01:42:11 +06:00
|
|
|
|
2019-10-05 12:38:34 +06:00
|
|
|
import qmk.keymap
|
|
|
|
|
2019-11-16 13:10:19 +06:00
|
|
|
|
2019-10-05 12:38:34 +06:00
|
|
|
def create_make_command(keyboard, keymap, target=None):
|
|
|
|
"""Create a make compile command
|
|
|
|
|
|
|
|
Args:
|
2020-02-18 01:42:11 +06:00
|
|
|
|
2019-10-05 12:38:34 +06:00
|
|
|
keyboard
|
|
|
|
The path of the keyboard, for example 'plank'
|
|
|
|
|
|
|
|
keymap
|
|
|
|
The name of the keymap, for example 'algernon'
|
|
|
|
|
|
|
|
target
|
|
|
|
Usually a bootloader.
|
|
|
|
|
|
|
|
Returns:
|
2020-02-18 01:42:11 +06:00
|
|
|
|
2019-10-05 12:38:34 +06:00
|
|
|
A command that can be run to make the specified keyboard and keymap
|
|
|
|
"""
|
2020-02-18 01:42:11 +06:00
|
|
|
make_args = [keyboard, keymap]
|
2020-04-13 22:44:27 +06:00
|
|
|
make_cmd = 'gmake' if shutil.which('gmake') else 'make'
|
2019-10-05 12:38:34 +06:00
|
|
|
|
2020-02-18 01:42:11 +06:00
|
|
|
if target:
|
|
|
|
make_args.append(target)
|
2019-11-16 13:10:19 +06:00
|
|
|
|
2020-04-13 21:55:27 +06:00
|
|
|
return [make_cmd, ':'.join(make_args)]
|
2019-10-05 12:38:34 +06:00
|
|
|
|
2019-11-16 13:10:19 +06:00
|
|
|
|
2019-12-09 06:16:01 +06:00
|
|
|
def compile_configurator_json(user_keymap, bootloader=None):
|
2019-10-05 12:38:34 +06:00
|
|
|
"""Convert a configurator export JSON file into a C file
|
|
|
|
|
|
|
|
Args:
|
2020-02-18 01:42:11 +06:00
|
|
|
|
2019-10-05 12:38:34 +06:00
|
|
|
configurator_filename
|
|
|
|
The configurator JSON export file
|
|
|
|
|
|
|
|
bootloader
|
|
|
|
A bootloader to flash
|
|
|
|
|
|
|
|
Returns:
|
2020-02-18 01:42:11 +06:00
|
|
|
|
2019-10-05 12:38:34 +06:00
|
|
|
A command to run to compile and flash the C file.
|
|
|
|
"""
|
|
|
|
# Write the keymap C file
|
|
|
|
qmk.keymap.write(user_keymap['keyboard'], user_keymap['keymap'], user_keymap['layout'], user_keymap['layers'])
|
|
|
|
|
|
|
|
# Return a command that can be run to make the keymap and flash if given
|
|
|
|
if bootloader is None:
|
|
|
|
return create_make_command(user_keymap['keyboard'], user_keymap['keymap'])
|
|
|
|
return create_make_command(user_keymap['keyboard'], user_keymap['keymap'], bootloader)
|
2020-02-18 01:42:11 +06:00
|
|
|
|
|
|
|
|
|
|
|
def parse_configurator_json(configurator_file):
|
|
|
|
"""Open and parse a configurator json export
|
|
|
|
"""
|
2020-05-27 02:05:41 +06:00
|
|
|
# FIXME(skullydazed/anyone): Add validation here
|
2020-02-18 01:42:11 +06:00
|
|
|
user_keymap = json.load(configurator_file)
|
|
|
|
|
|
|
|
return user_keymap
|
2020-03-29 18:29:44 +06: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 07:21:32 +06:00
|
|
|
safecmd = map(str, command)
|
|
|
|
safecmd = map(shlex.quote, safecmd)
|
2020-03-29 18:29:44 +06:00
|
|
|
safecmd = ' '.join(safecmd)
|
|
|
|
command = [os.environ['SHELL'], '-c', safecmd]
|
|
|
|
|
|
|
|
return subprocess.run(command, *args, **kwargs)
|