implemented mommify of venv

This commit is contained in:
Hazel Noack 2025-07-28 11:31:31 +02:00
parent 6f2a104c4a
commit 66db5acc76
3 changed files with 69 additions and 0 deletions

6
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,6 @@
{
"cSpell.words": [
"mommify",
"venv"
]
}

View File

@ -15,6 +15,7 @@ license-files = ["LICENSE"]
[project.scripts] [project.scripts]
python-mommy-dev = "python_mommy_venv.__main__:development" python-mommy-dev = "python_mommy_venv.__main__:development"
mommify-venv = "python_mommy_venv.__main__:mommify_venv"
[build-system] [build-system]
requires = ["hatchling", "hatch-requirements-txt"] requires = ["hatchling", "hatch-requirements-txt"]

View File

@ -1,4 +1,6 @@
import sys import sys
from pathlib import Path
import stat
from . import get_response from . import get_response
from .static import Situation from .static import Situation
@ -11,3 +13,63 @@ def development():
print(get_response(Situation(s))) 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)