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
|
2021-07-10 17:04:50 +02:00
|
|
|
import sys
|
2020-04-13 18:44:27 +02:00
|
|
|
import shutil
|
2021-01-17 00:13:04 +01:00
|
|
|
from pathlib import Path
|
2021-05-20 00:24:46 +02:00
|
|
|
from subprocess import DEVNULL
|
2021-01-17 18:33:29 +01:00
|
|
|
from time import strftime
|
2021-01-17 00:13:04 +01:00
|
|
|
|
|
|
|
from milc import cli
|
2020-02-17 20:42:11 +01:00
|
|
|
|
2019-10-05 08:38:34 +02:00
|
|
|
import qmk.keymap
|
2021-07-10 17:04:50 +02:00
|
|
|
from qmk.constants import QMK_FIRMWARE, KEYBOARD_OUTPUT_PREFIX
|
2021-03-24 17:26:38 +01:00
|
|
|
from qmk.json_schema import json_load
|
2021-01-17 00:13:04 +01:00
|
|
|
|
2021-01-17 18:33:29 +01:00
|
|
|
time_fmt = '%Y-%m-%d-%H:%M:%S'
|
|
|
|
|
2021-01-17 00:13:04 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
2021-09-16 06:59:57 +02:00
|
|
|
def create_make_target(target, dry_run=False, parallel=1, **env_vars):
|
2021-05-09 12:57:49 +02:00
|
|
|
"""Create a make command
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
|
|
|
target
|
|
|
|
Usually a make rule, such as 'clean' or 'all'.
|
|
|
|
|
2021-09-16 06:59:57 +02:00
|
|
|
dry_run
|
|
|
|
make -n -- don't actually build
|
|
|
|
|
2021-05-09 12:57:49 +02:00
|
|
|
parallel
|
|
|
|
The number of make jobs to run in parallel
|
|
|
|
|
|
|
|
**env_vars
|
|
|
|
Environment variables to be passed to make.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
A command that can be run to make the specified keyboard and keymap
|
|
|
|
"""
|
|
|
|
env = []
|
|
|
|
make_cmd = _find_make()
|
|
|
|
|
|
|
|
for key, value in env_vars.items():
|
|
|
|
env.append(f'{key}={value}')
|
|
|
|
|
2021-09-16 06:59:57 +02:00
|
|
|
return [make_cmd, *(['-n'] if dry_run else []), *get_make_parallel_args(parallel), *env, target]
|
2021-05-09 12:57:49 +02:00
|
|
|
|
|
|
|
|
2021-09-16 06:59:57 +02:00
|
|
|
def create_make_command(keyboard, keymap, target=None, dry_run=False, 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-09-16 06:59:57 +02:00
|
|
|
dry_run
|
|
|
|
make -n -- don't actually build
|
|
|
|
|
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
|
|
|
|
"""
|
2020-02-17 20:42:11 +01:00
|
|
|
make_args = [keyboard, keymap]
|
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-09-16 06:59:57 +02:00
|
|
|
return create_make_target(':'.join(make_args), dry_run=dry_run, parallel=parallel, **env_vars)
|
2019-11-16 08:10:19 +01:00
|
|
|
|
2021-01-17 00:13:04 +01:00
|
|
|
|
2021-06-26 18:29:02 +02:00
|
|
|
def get_git_version(current_time, repo_dir='.', check_dir='.'):
|
2021-01-17 18:33:29 +01:00
|
|
|
"""Returns the current git version for a repo, or the current time.
|
|
|
|
"""
|
|
|
|
git_describe_cmd = ['git', 'describe', '--abbrev=6', '--dirty', '--always', '--tags']
|
|
|
|
|
2021-06-26 18:29:02 +02:00
|
|
|
if repo_dir != '.':
|
|
|
|
repo_dir = Path('lib') / repo_dir
|
|
|
|
|
|
|
|
if check_dir != '.':
|
|
|
|
check_dir = repo_dir / check_dir
|
|
|
|
|
2021-01-17 18:33:29 +01:00
|
|
|
if Path(check_dir).exists():
|
2021-05-20 00:24:46 +02:00
|
|
|
git_describe = cli.run(git_describe_cmd, stdin=DEVNULL, cwd=repo_dir)
|
2021-01-17 18:33:29 +01:00
|
|
|
|
|
|
|
if git_describe.returncode == 0:
|
|
|
|
return git_describe.stdout.strip()
|
|
|
|
|
|
|
|
else:
|
2021-03-09 22:36:39 +01:00
|
|
|
cli.log.warn(f'"{" ".join(git_describe_cmd)}" returned error code {git_describe.returncode}')
|
2021-01-17 18:33:29 +01:00
|
|
|
print(git_describe.stderr)
|
2021-06-26 18:29:02 +02:00
|
|
|
return current_time
|
2021-01-17 18:33:29 +01:00
|
|
|
|
2021-06-26 18:29:02 +02:00
|
|
|
return current_time
|
2021-01-17 18:33:29 +01:00
|
|
|
|
|
|
|
|
2021-08-18 00:46:59 +02: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')
|
|
|
|
else:
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2021-06-26 18:29:02 +02:00
|
|
|
def create_version_h(skip_git=False, skip_all=False):
|
|
|
|
"""Generate version.h contents
|
2021-01-17 18:33:29 +01:00
|
|
|
"""
|
2021-06-26 18:29:02 +02:00
|
|
|
if skip_all:
|
|
|
|
current_time = "1970-01-01-00:00:00"
|
|
|
|
else:
|
|
|
|
current_time = strftime(time_fmt)
|
|
|
|
|
|
|
|
if skip_git:
|
|
|
|
git_version = "NA"
|
|
|
|
chibios_version = "NA"
|
|
|
|
chibios_contrib_version = "NA"
|
|
|
|
else:
|
|
|
|
git_version = get_git_version(current_time)
|
|
|
|
chibios_version = get_git_version(current_time, "chibios", "os")
|
|
|
|
chibios_contrib_version = get_git_version(current_time, "chibios-contrib", "os")
|
|
|
|
|
|
|
|
version_h_lines = f"""/* This file was automatically generated. Do not edit or copy.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#define QMK_VERSION "{git_version}"
|
|
|
|
#define QMK_BUILDDATE "{current_time}"
|
|
|
|
#define CHIBIOS_VERSION "{chibios_version}"
|
|
|
|
#define CHIBIOS_CONTRIB_VERSION "{chibios_contrib_version}"
|
|
|
|
"""
|
2021-01-17 18:33:29 +01:00
|
|
|
|
2021-06-26 18:29:02 +02:00
|
|
|
return version_h_lines
|
2021-01-17 18:33:29 +01:00
|
|
|
|
|
|
|
|
2021-02-01 20:55:35 +01:00
|
|
|
def compile_configurator_json(user_keymap, bootloader=None, parallel=1, **env_vars):
|
2021-01-17 00:13:04 +01:00
|
|
|
"""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
|
|
|
|
2021-06-26 18:29:02 +02:00
|
|
|
version_h = Path('quantum/version.h')
|
|
|
|
version_h.write_text(create_version_h())
|
2021-01-17 18:33:29 +01:00
|
|
|
|
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'
|
2021-01-17 18:33:29 +01:00
|
|
|
make_command = [_find_make()]
|
|
|
|
|
|
|
|
if not cli.config.general.verbose:
|
|
|
|
make_command.append('-s')
|
|
|
|
|
|
|
|
make_command.extend([
|
2021-08-18 00:46:59 +02:00
|
|
|
*get_make_parallel_args(parallel),
|
2021-01-17 00:13:04 +01:00
|
|
|
'-r',
|
|
|
|
'-R',
|
|
|
|
'-f',
|
|
|
|
'build_keyboard.mk',
|
2021-01-17 18:33:29 +01:00
|
|
|
])
|
2021-01-17 00:13:04 +01:00
|
|
|
|
2021-02-01 20:55:35 +01:00
|
|
|
if bootloader:
|
|
|
|
make_command.append(bootloader)
|
|
|
|
|
2021-01-17 00:13:04 +01:00
|
|
|
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',
|
2021-08-30 01:50:22 +02:00
|
|
|
'QMK_BIN="qmk"',
|
2021-01-17 00:13:04 +01:00
|
|
|
])
|
|
|
|
|
|
|
|
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)
|
2021-03-24 17:26:38 +01:00
|
|
|
orig_keyboard = user_keymap['keyboard']
|
|
|
|
aliases = json_load(Path('data/mappings/keyboard_aliases.json'))
|
|
|
|
|
|
|
|
if orig_keyboard in aliases:
|
|
|
|
if 'target' in aliases[orig_keyboard]:
|
|
|
|
user_keymap['keyboard'] = aliases[orig_keyboard]['target']
|
|
|
|
|
|
|
|
if 'layouts' in aliases[orig_keyboard] and user_keymap['layout'] in aliases[orig_keyboard]['layouts']:
|
|
|
|
user_keymap['layout'] = aliases[orig_keyboard]['layouts'][user_keymap['layout']]
|
2020-02-17 20:42:11 +01:00
|
|
|
|
|
|
|
return user_keymap
|
2021-07-10 17:04:50 +02:00
|
|
|
|
|
|
|
|
2021-07-30 22:57:40 +02:00
|
|
|
def git_get_username():
|
|
|
|
"""Retrieves user's username from Git config, if set.
|
|
|
|
"""
|
|
|
|
git_username = cli.run(['git', 'config', '--get', 'user.name'])
|
|
|
|
|
|
|
|
if git_username.returncode == 0 and git_username.stdout:
|
|
|
|
return git_username.stdout.strip()
|
|
|
|
|
|
|
|
|
2021-07-10 17:04:50 +02:00
|
|
|
def git_check_repo():
|
|
|
|
"""Checks that the .git directory exists inside QMK_HOME.
|
|
|
|
|
|
|
|
This is a decent enough indicator that the qmk_firmware directory is a
|
|
|
|
proper Git repository, rather than a .zip download from GitHub.
|
|
|
|
"""
|
|
|
|
dot_git_dir = QMK_FIRMWARE / '.git'
|
|
|
|
|
|
|
|
return dot_git_dir.is_dir()
|
|
|
|
|
|
|
|
|
|
|
|
def git_get_branch():
|
|
|
|
"""Returns the current branch for a repo, or None.
|
|
|
|
"""
|
|
|
|
git_branch = cli.run(['git', 'branch', '--show-current'])
|
|
|
|
if not git_branch.returncode != 0 or not git_branch.stdout:
|
|
|
|
# Workaround for Git pre-2.22
|
|
|
|
git_branch = cli.run(['git', 'rev-parse', '--abbrev-ref', 'HEAD'])
|
|
|
|
|
|
|
|
if git_branch.returncode == 0:
|
|
|
|
return git_branch.stdout.strip()
|
|
|
|
|
|
|
|
|
|
|
|
def git_is_dirty():
|
|
|
|
"""Returns 1 if repo is dirty, or 0 if clean
|
|
|
|
"""
|
|
|
|
git_diff_staged_cmd = ['git', 'diff', '--quiet']
|
|
|
|
git_diff_unstaged_cmd = [*git_diff_staged_cmd, '--cached']
|
|
|
|
|
|
|
|
unstaged = cli.run(git_diff_staged_cmd)
|
|
|
|
staged = cli.run(git_diff_unstaged_cmd)
|
|
|
|
|
|
|
|
return unstaged.returncode != 0 or staged.returncode != 0
|
|
|
|
|
|
|
|
|
|
|
|
def git_get_remotes():
|
|
|
|
"""Returns the current remotes for a repo.
|
|
|
|
"""
|
|
|
|
remotes = {}
|
|
|
|
|
|
|
|
git_remote_show_cmd = ['git', 'remote', 'show']
|
|
|
|
git_remote_get_cmd = ['git', 'remote', 'get-url']
|
|
|
|
|
|
|
|
git_remote_show = cli.run(git_remote_show_cmd)
|
|
|
|
if git_remote_show.returncode == 0:
|
|
|
|
for name in git_remote_show.stdout.splitlines():
|
|
|
|
git_remote_name = cli.run([*git_remote_get_cmd, name])
|
|
|
|
remotes[name.strip()] = {"url": git_remote_name.stdout.strip()}
|
|
|
|
|
|
|
|
return remotes
|
|
|
|
|
|
|
|
|
|
|
|
def git_check_deviation(active_branch):
|
|
|
|
"""Return True if branch has custom commits
|
|
|
|
"""
|
|
|
|
cli.run(['git', 'fetch', 'upstream', active_branch])
|
|
|
|
deviations = cli.run(['git', '--no-pager', 'log', f'upstream/{active_branch}...{active_branch}'])
|
|
|
|
return bool(deviations.returncode)
|
|
|
|
|
|
|
|
|
|
|
|
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
|