From a8a1f425ccc0e9def00b98089373ff90a86deb79 Mon Sep 17 00:00:00 2001 From: Hazel Noack Date: Tue, 29 Jul 2025 12:08:27 +0200 Subject: [PATCH] ensure running in venv --- python_mommy_venv/__main__.py | 9 +++++++++ python_mommy_venv/static.py | 16 ++++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/python_mommy_venv/__main__.py b/python_mommy_venv/__main__.py index 7f49976..b66d7ee 100644 --- a/python_mommy_venv/__main__.py +++ b/python_mommy_venv/__main__.py @@ -82,6 +82,13 @@ PIP_HOOK = """# GENERATED BY MOMMY sys.exit(code)""" +def assert_venv(): + if not IS_VENV: + mommy_logger.error("mommy doesn't run in a virtual environment~") + serious_logger.error("this has to run in a virtual environment") + exit(1) + + def wrap_interpreter(path: Path): mommy_logger.info("mommy found a symlink to an interpreter~ %s", str(path)) serious_logger.info("interpreter symlink found at %s", str(path)) @@ -130,6 +137,8 @@ def install_pip_hook(path: Path): def mommify_venv(): + assert_venv() + compile_local = False compiled_base_dir = VENV_DIRECTORY if compile_local else CONFIG_DIRECTORY compiled_config_file = compiled_base_dir / COMPILED_CONFIG_FILE_NAME diff --git a/python_mommy_venv/static.py b/python_mommy_venv/static.py index c265f66..a934e4c 100644 --- a/python_mommy_venv/static.py +++ b/python_mommy_venv/static.py @@ -73,19 +73,27 @@ def _get_xdg_config_dir() -> Path: CONFIG_DIRECTORY = _get_xdg_config_dir() / "mommy" COMPILED_CONFIG_FILE_NAME = "compiled-mommy.json" +IS_VENV = sys.prefix != sys.base_prefix +VENV_DIRECTORY = Path(sys.prefix) + def get_config_file() -> Optional[Path]: - config_files = [ + config_files = [] + if IS_VENV: + config_files.extend([ + VENV_DIRECTORY / "python-mommy.toml", + VENV_DIRECTORY / "mommy.toml", + ]) + config_files.extend([ CONFIG_DIRECTORY / "python-mommy.toml", CONFIG_DIRECTORY / "mommy.toml", - ] + ]) for f in config_files: if f.exists(): return f -IS_VENV = sys.prefix != sys.base_prefix -VENV_DIRECTORY = Path(sys.prefix) + def get_compiled_config_file() -> Path: compiled_config_files = [