diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..1c28259 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "cSpell.words": [ + "mommify", + "venv" + ] +} \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 29ff4f4..110eb7f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -15,6 +15,7 @@ license-files = ["LICENSE"] [project.scripts] python-mommy-dev = "python_mommy_venv.__main__:development" +mommify-venv = "python_mommy_venv.__main__:mommify_venv" [build-system] requires = ["hatchling", "hatch-requirements-txt"] diff --git a/python_mommy_venv/__main__.py b/python_mommy_venv/__main__.py index d334ef6..a4e802c 100644 --- a/python_mommy_venv/__main__.py +++ b/python_mommy_venv/__main__.py @@ -1,4 +1,6 @@ import sys +from pathlib import Path +import stat from . import get_response from .static import Situation @@ -11,3 +13,63 @@ def development(): print(get_response(Situation(s))) + + +TEMPLATE = """#!{inner_bin} +# -*- coding: utf-8 -*- + +import sys, subprocess +from python_mommy_venv import get_response, Situation + + +INTERPRETER = "{inner_bin}" +result = subprocess.run([INTERPRETER] + sys.argv[1:]) +code = result.returncode + +print() +print(get_response(Situation.POSITIVE if code == 0 else Situation.NEGATIVE)) +exit(code=code) +""" + + +def mommify_venv(): + v = ".venv" + if len(sys.argv) > 1: + v = sys.argv[1] + + bin_path = Path(v, "bin") + bin_path = bin_path.resolve() + print(bin_path) + + for path in bin_path.iterdir(): + if not path.is_symlink(): + continue + + name = path.name + if name.startswith("inner_"): + continue + target = path.resolve() + + print("") + print(f"modifying {name} ({target})") + + # creating inner symlink + inner_bin = Path(bin_path, "inner_" + name) + if inner_bin.exists(): + print(f"inner symlink does already exist {inner_bin}") + print("skipping") + continue + + print(f"creating symlink: {inner_bin} -> {target}") + Path(bin_path, "inner_" + name).symlink_to(target) + + # remove original symlink + print(f"removing original symlink: {path}") + path.unlink() + + # creating the wrapper string + print("writing wrapper script") + with path.open("w") as f: + f.write(TEMPLATE.format(inner_bin=str(inner_bin))) + print("making wrapper script executable") + path.chmod(path.stat().st_mode | stat.S_IEXEC)