added pip hook to mommify newly installed packages

This commit is contained in:
Hazel Noack 2025-07-28 14:04:07 +02:00
parent aff54b38ed
commit 29098500e2

View File

@ -30,7 +30,7 @@ def write_current_config():
f.write(data)
TEMPLATE = """#!{inner_bin}
WRAPPER_TEMPLATE = """#!{inner_bin}
# -*- coding: utf-8 -*-
import sys, subprocess
@ -46,6 +46,53 @@ print(get_response(Situation.POSITIVE if code == 0 else Situation.NEGATIVE))
exit(code=code)
"""
PIP_HOOK = """# GENERATED BY MOMMY
code = main()
from pathlib import Path
bin_path = Path(".venv", "bin")
python_interpreter_wrappers = []
for path in bin_path.iterdir():
if path.is_symlink() and path.name.startswith("inner_"):
python_interpreter_wrappers.append(path.name.replace("inner_", "", 1))
for path in bin_path.iterdir():
if path.is_symlink():
continue
if path.name in python_interpreter_wrappers:
continue
text: str
with path.open("r") as f:
text = f.read()
first_line = text.split("\\n")[0]
if not ("inner_" in first_line and first_line.startswith("#!")):
continue
print(f"mommifying " + str(path))
text = text.replace("inner_", "", 1)
with path.open("w") as f:
f.write(text)
sys.exit(code)"""
def mommify_pip(path: Path):
text: str
with path.open("r") as f:
text = f.read()
if "# GENERATED BY MOMMY" in text:
print(f"pip hook already installed in {path}")
return
print(f"installing pip hook in {path}")
text = text.replace("sys.exit(main())", PIP_HOOK, 1)
with path.open("w") as f:
f.write(text)
def mommify_venv():
v = ".venv"
@ -58,6 +105,8 @@ def mommify_venv():
for path in bin_path.iterdir():
if not path.is_symlink():
if path.name.startswith("pip"):
mommify_pip(path)
continue
name = path.name
@ -85,6 +134,6 @@ def mommify_venv():
# creating the wrapper string
print("writing wrapper script")
with path.open("w") as f:
f.write(TEMPLATE.format(inner_bin=str(inner_bin)))
f.write(WRAPPER_TEMPLATE.format(inner_bin=str(inner_bin)))
print("making wrapper script executable")
path.chmod(path.stat().st_mode | stat.S_IEXEC)