Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically configure userspace at runtime #168

Merged
merged 2 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion qmk_cli/helpers.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
4 changes: 3 additions & 1 deletion qmk_cli/script_qmk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}'
Expand Down Expand Up @@ -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()

Expand Down
6 changes: 4 additions & 2 deletions qmk_cli/subcommands/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading