From d20af71118c6536e988352611e9a1115507a428f Mon Sep 17 00:00:00 2001 From: zvecr Date: Sat, 30 Dec 2023 04:13:43 +0000 Subject: [PATCH 1/2] Automatically configure userspace at runtime --- qmk_cli/helpers.py | 47 ++++++++++++++++++++++++++++++++++++++++++- qmk_cli/script_qmk.py | 4 +++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/qmk_cli/helpers.py b/qmk_cli/helpers.py index 90767fa..d3c9265 100644 --- a/qmk_cli/helpers.py +++ b/qmk_cli/helpers.py @@ -1,8 +1,8 @@ """Useful helper functions. """ import os +import json from functools import lru_cache -from importlib.util import find_spec from pathlib import Path from milc import cli @@ -58,3 +58,48 @@ def in_qmk_firmware(): # Move up a directory before the next iteration cur_dir = cur_dir / '..' cur_dir = cur_dir.resolve() + + +def is_qmk_userspace(qmk_userspace): + """Returns True if the given Path() is a qmk_userspace clone. + """ + path = qmk_userspace / 'qmk.json' + if not path.exists(): + return False + + try: + return 'userspace_version' in json.loads(path.read_text(encoding="UTF-8")) + except json.decoder.JSONDecodeError as e: + return False + + +@lru_cache(maxsize=2) +def find_qmk_userspace(): + """Look for qmk_userspace in the usual places. + """ + if in_qmk_userspace(): + return in_qmk_userspace() + + if cli.config.user.overlay_dir: + return Path(cli.config.user.overlay_dir).expanduser().resolve() + + if 'QMK_USERSPACE' in os.environ: + path = Path(os.environ['QMK_USERSPACE']).expanduser() + if path.exists(): + return path.resolve() + return path + + return Path.home() / 'qmk_userspace' + + +def in_qmk_userspace(): + """Returns the path to the qmk_userspace we are currently in, or None if we are not inside qmk_userspace. + """ + cur_dir = Path.cwd() + while len(cur_dir.parents) > 0: + if is_qmk_userspace(cur_dir): + return cur_dir + + # Move up a directory before the next iteration + cur_dir = cur_dir / '..' + cur_dir = cur_dir.resolve() diff --git a/qmk_cli/script_qmk.py b/qmk_cli/script_qmk.py index 543dc6b..aaa6325 100644 --- a/qmk_cli/script_qmk.py +++ b/qmk_cli/script_qmk.py @@ -13,7 +13,7 @@ import milc from . import __version__ -from .helpers import find_qmk_firmware, is_qmk_firmware +from .helpers import find_qmk_firmware, is_qmk_firmware, find_qmk_userspace, is_qmk_userspace milc.cli.milc_options(version=__version__) milc.EMOJI_LOGLEVELS['INFO'] = '{fg_blue}Ψ{style_reset_all}' @@ -59,7 +59,9 @@ def main(): exit(1) # Environment setup + qmk_userspace = find_qmk_userspace() qmk_firmware = find_qmk_firmware() + os.environ['QMK_USERSPACE'] = str(qmk_userspace) os.environ['QMK_HOME'] = str(qmk_firmware) os.environ['ORIG_CWD'] = os.getcwd() From ae45bdeb027cc31121489d5f9f1a90615fc9feb2 Mon Sep 17 00:00:00 2001 From: zvecr Date: Sun, 23 Jun 2024 14:21:20 +0100 Subject: [PATCH 2/2] Update 'qmk env' --- qmk_cli/subcommands/env.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/qmk_cli/subcommands/env.py b/qmk_cli/subcommands/env.py index 1f10874..83323d8 100644 --- a/qmk_cli/subcommands/env.py +++ b/qmk_cli/subcommands/env.py @@ -4,16 +4,18 @@ from pathlib import Path from milc import cli -from qmk_cli.helpers import is_qmk_firmware +from qmk_cli.helpers import is_qmk_firmware, is_qmk_userspace @cli.argument('var', arg_only=True, default=None, nargs='?', help='Optional variable to query') @cli.subcommand('Prints environment information.') def env(cli): home = os.environ.get('QMK_HOME', "") + userspace = os.environ.get('QMK_USERSPACE', "") data = { 'QMK_HOME': home, - 'QMK_FIRMWARE': home if is_qmk_firmware(Path(home)) else "" + 'QMK_FIRMWARE': home if is_qmk_firmware(Path(home)) else "", + 'QMK_USERSPACE': userspace if is_qmk_userspace(Path(userspace)) else "" } # Now munge the current cli config