2020-02-18 01:42:11 +06:00
|
|
|
"""Helper functions for commands.
|
2019-10-05 12:38:34 +06:00
|
|
|
"""
|
2020-03-29 18:29:44 +06:00
|
|
|
import os
|
2021-07-10 21:04:50 +06:00
|
|
|
import sys
|
2020-04-13 22:44:27 +06:00
|
|
|
import shutil
|
2023-11-28 02:53:43 +06:00
|
|
|
from pathlib import Path
|
2021-01-17 05:13:04 +06:00
|
|
|
|
|
|
|
from milc import cli
|
2022-03-01 02:02:39 +06:00
|
|
|
import jsonschema
|
2020-02-18 01:42:11 +06:00
|
|
|
|
2023-11-28 02:53:43 +06:00
|
|
|
from qmk.constants import QMK_USERSPACE, HAS_QMK_USERSPACE
|
2022-03-01 02:02:39 +06:00
|
|
|
from qmk.json_schema import json_load, validate
|
2023-11-22 06:14:34 +06:00
|
|
|
from qmk.keyboard import keyboard_alias_definitions
|
2024-06-15 15:37:47 +06:00
|
|
|
from qmk.util import maybe_exit
|
2021-01-17 05:13:04 +06:00
|
|
|
|
|
|
|
|
2023-11-15 11:24:54 +06:00
|
|
|
def find_make():
|
2021-01-17 05:13:04 +06:00
|
|
|
"""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 12:38:34 +06:00
|
|
|
|
2021-01-17 05:13:04 +06:00
|
|
|
return make_cmd
|
2019-11-16 13:10:19 +06:00
|
|
|
|
2021-01-17 05:13:04 +06:00
|
|
|
|
2021-08-18 04:46:59 +06:00
|
|
|
def get_make_parallel_args(parallel=1):
|
|
|
|
"""Returns the arguments for running the specified number of parallel jobs.
|
|
|
|
"""
|
|
|
|
parallel_args = []
|
|
|
|
|
|
|
|
if int(parallel) <= 0:
|
|
|
|
# 0 or -1 means -j without argument (unlimited jobs)
|
|
|
|
parallel_args.append('--jobs')
|
2023-11-15 11:24:54 +06:00
|
|
|
elif int(parallel) > 1:
|
2021-08-18 04:46:59 +06:00
|
|
|
parallel_args.append('--jobs=' + str(parallel))
|
|
|
|
|
|
|
|
if int(parallel) != 1:
|
|
|
|
# If more than 1 job is used, synchronize parallel output by target
|
|
|
|
parallel_args.append('--output-sync=target')
|
|
|
|
|
|
|
|
return parallel_args
|
|
|
|
|
|
|
|
|
2020-02-18 01:42:11 +06:00
|
|
|
def parse_configurator_json(configurator_file):
|
|
|
|
"""Open and parse a configurator json export
|
|
|
|
"""
|
2022-03-01 02:02:39 +06:00
|
|
|
user_keymap = json_load(configurator_file)
|
|
|
|
# Validate against the jsonschema
|
|
|
|
try:
|
|
|
|
validate(user_keymap, 'qmk.keymap.v1')
|
|
|
|
|
|
|
|
except jsonschema.ValidationError as e:
|
|
|
|
cli.log.error(f'Invalid JSON keymap: {configurator_file} : {e.message}')
|
2024-06-15 15:37:47 +06:00
|
|
|
maybe_exit(1)
|
2022-03-01 02:02:39 +06:00
|
|
|
|
2023-10-06 05:34:23 +06:00
|
|
|
keyboard = user_keymap['keyboard']
|
2023-11-22 06:14:34 +06:00
|
|
|
aliases = keyboard_alias_definitions()
|
2021-03-24 22:26:38 +06:00
|
|
|
|
2023-10-06 05:34:23 +06:00
|
|
|
while keyboard in aliases:
|
|
|
|
last_keyboard = keyboard
|
|
|
|
keyboard = aliases[keyboard].get('target', keyboard)
|
|
|
|
if keyboard == last_keyboard:
|
|
|
|
break
|
2021-03-24 22:26:38 +06:00
|
|
|
|
2023-10-06 05:34:23 +06:00
|
|
|
user_keymap['keyboard'] = keyboard
|
2020-02-18 01:42:11 +06:00
|
|
|
return user_keymap
|
2021-07-10 21:04:50 +06:00
|
|
|
|
|
|
|
|
2024-08-12 18:34:22 +06:00
|
|
|
def parse_env_vars(args):
|
2022-10-20 19:35:27 +06:00
|
|
|
"""Common processing for cli.args.env
|
|
|
|
"""
|
|
|
|
envs = {}
|
|
|
|
for env in args:
|
|
|
|
if '=' in env:
|
|
|
|
key, value = env.split('=', 1)
|
|
|
|
envs[key] = value
|
|
|
|
else:
|
|
|
|
cli.log.warning('Invalid environment variable: %s', env)
|
2024-08-12 18:34:22 +06:00
|
|
|
return envs
|
|
|
|
|
|
|
|
|
|
|
|
def build_environment(args):
|
|
|
|
envs = parse_env_vars(args)
|
2023-11-28 02:53:43 +06:00
|
|
|
|
|
|
|
if HAS_QMK_USERSPACE:
|
|
|
|
envs['QMK_USERSPACE'] = Path(QMK_USERSPACE).resolve()
|
|
|
|
|
2022-10-20 19:35:27 +06:00
|
|
|
return envs
|
|
|
|
|
|
|
|
|
2021-07-10 21:04:50 +06:00
|
|
|
def in_virtualenv():
|
|
|
|
"""Check if running inside a virtualenv.
|
|
|
|
Based on https://stackoverflow.com/a/1883251
|
|
|
|
"""
|
|
|
|
active_prefix = getattr(sys, "base_prefix", None) or getattr(sys, "real_prefix", None) or sys.prefix
|
|
|
|
return active_prefix != sys.prefix
|
2022-03-18 07:09:29 +06:00
|
|
|
|
|
|
|
|
|
|
|
def dump_lines(output_file, lines, quiet=True):
|
|
|
|
"""Handle dumping to stdout or file
|
|
|
|
Creates parent folders if required
|
|
|
|
"""
|
|
|
|
generated = '\n'.join(lines) + '\n'
|
|
|
|
if output_file and output_file.name != '-':
|
|
|
|
output_file.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
if output_file.exists():
|
2024-07-24 11:16:51 +06:00
|
|
|
with open(output_file, 'r', encoding='utf-8', newline='\n') as f:
|
|
|
|
existing = f.read()
|
|
|
|
if existing == generated:
|
|
|
|
if not quiet:
|
|
|
|
cli.log.info(f'No changes to {output_file.name}.')
|
|
|
|
return
|
2022-03-18 07:09:29 +06:00
|
|
|
output_file.replace(output_file.parent / (output_file.name + '.bak'))
|
2024-06-20 00:43:23 +06:00
|
|
|
with open(output_file, 'w', encoding='utf-8', newline='\n') as f:
|
|
|
|
f.write(generated)
|
|
|
|
# output_file.write_text(generated, encoding='utf-8', newline='\n') # `newline` needs Python 3.10
|
2022-03-18 07:09:29 +06:00
|
|
|
|
|
|
|
if not quiet:
|
|
|
|
cli.log.info(f'Wrote {output_file.name} to {output_file}.')
|
|
|
|
else:
|
|
|
|
print(generated)
|