added logging
This commit is contained in:
parent
be024389ba
commit
ac7360ffe8
@ -1,10 +1,6 @@
|
|||||||
import random
|
import random
|
||||||
import subprocess
|
|
||||||
import sys
|
import sys
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
import os
|
|
||||||
import re
|
|
||||||
import signal
|
|
||||||
|
|
||||||
from .config import get_mood, get_template_values
|
from .config import get_mood, get_template_values
|
||||||
from .static import RESPONSES, Situation, colors
|
from .static import RESPONSES, Situation, colors
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
import sys
|
import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import stat
|
import stat
|
||||||
|
import subprocess
|
||||||
|
import logging
|
||||||
|
|
||||||
import toml
|
import toml
|
||||||
|
|
||||||
@ -9,6 +11,19 @@ from .static import Situation
|
|||||||
from .config import CONFIG_FILES, CONFIG_DIRECTORY, generate_current_configuration
|
from .config import CONFIG_FILES, CONFIG_DIRECTORY, generate_current_configuration
|
||||||
|
|
||||||
|
|
||||||
|
logging.basicConfig(
|
||||||
|
format=' %(message)s',
|
||||||
|
force=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
log_level = logging.INFO
|
||||||
|
|
||||||
|
mommy_logger = logging.getLogger("mommy")
|
||||||
|
mommy_logger.setLevel(logging.INFO)
|
||||||
|
serious_logger = logging.getLogger("serious")
|
||||||
|
serious_logger.setLevel(logging.WARNING)
|
||||||
|
|
||||||
|
|
||||||
def development():
|
def development():
|
||||||
s = "positive"
|
s = "positive"
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
@ -83,60 +98,84 @@ PIP_HOOK = """# GENERATED BY MOMMY
|
|||||||
sys.exit(code)"""
|
sys.exit(code)"""
|
||||||
|
|
||||||
|
|
||||||
def mommify_pip(path: Path):
|
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))
|
||||||
|
|
||||||
|
inner_symlink = path.parent / ("inner_" + path.name)
|
||||||
|
symlink_target = path.resolve()
|
||||||
|
|
||||||
|
if inner_symlink.exists():
|
||||||
|
raise Exception("inner symlink somehow already exists. This shouldn't happen because of prior checks")
|
||||||
|
|
||||||
|
mommy_logger.info("mommy shows her girl where the interpreter is: %s -> %s", inner_symlink, symlink_target)
|
||||||
|
serious_logger.info("creating symlink: %s -> %s", inner_symlink, symlink_target)
|
||||||
|
inner_symlink.symlink_to(symlink_target)
|
||||||
|
|
||||||
|
# remove original symlink
|
||||||
|
mommy_logger.info("mommy deletes the original interpreter~ %s", path)
|
||||||
|
serious_logger.info("deleting original symlink %s", path)
|
||||||
|
path.unlink()
|
||||||
|
|
||||||
|
# creating the wrapper string
|
||||||
|
mommy_logger.info("mommy writes wrapper script as %s", Path)
|
||||||
|
serious_logger.info("writing wrapper script at %s", path)
|
||||||
|
with path.open("w") as f:
|
||||||
|
f.write(WRAPPER_TEMPLATE.format(inner_bin=str(inner_symlink)))
|
||||||
|
serious_logger.info("making wrapper script executable")
|
||||||
|
path.chmod(path.stat().st_mode | stat.S_IEXEC)
|
||||||
|
|
||||||
|
|
||||||
|
def install_pip_hook(path: Path):
|
||||||
text: str
|
text: str
|
||||||
with path.open("r") as f:
|
with path.open("r") as f:
|
||||||
text = f.read()
|
text = f.read()
|
||||||
|
|
||||||
if "# GENERATED BY MOMMY" in text:
|
if "# GENERATED BY MOMMY" in text:
|
||||||
print(f"pip hook already installed in {path}")
|
mommy_logger.info("ahhhhh mommy already watches %s", str(path))
|
||||||
|
serious_logger.info("pip hook already installed at %s", str(path))
|
||||||
return
|
return
|
||||||
|
|
||||||
print(f"installing pip hook in {path}")
|
mommy_logger.info("mommy needs to keep an eye on this little pip~ %s", str(path))
|
||||||
|
serious_logger.info("installing pip hook at %s", str(path))
|
||||||
|
|
||||||
text = text.replace("sys.exit(main())", PIP_HOOK, 1)
|
text = text.replace("sys.exit(main())", PIP_HOOK, 1)
|
||||||
with path.open("w") as f:
|
with path.open("w") as f:
|
||||||
f.write(text)
|
f.write(text)
|
||||||
|
|
||||||
|
|
||||||
def mommify_venv():
|
def mommify_venv():
|
||||||
|
|
||||||
v = ".venv"
|
v = ".venv"
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
v = sys.argv[1]
|
v = sys.argv[1]
|
||||||
|
|
||||||
bin_path = Path(v, "bin")
|
bin_path = Path(v, "bin")
|
||||||
bin_path = bin_path.resolve()
|
bin_path = bin_path.resolve()
|
||||||
print(bin_path)
|
|
||||||
|
|
||||||
for path in bin_path.iterdir():
|
mommy_logger.info("mommy looks in %s to mess your system up~ <33", str(bin_path))
|
||||||
if not path.is_symlink():
|
serious_logger.info("scanning binary directory of venv at %s", str(bin_path))
|
||||||
if path.name.startswith("pip"):
|
|
||||||
mommify_pip(path)
|
for path in list(bin_path.iterdir()):
|
||||||
continue
|
|
||||||
|
|
||||||
name = path.name
|
name = path.name
|
||||||
if name.startswith("inner_"):
|
|
||||||
continue
|
|
||||||
target = path.resolve()
|
|
||||||
|
|
||||||
print("")
|
if path.is_symlink():
|
||||||
print(f"modifying {name} ({target})")
|
# could be python interpreter
|
||||||
|
# check for both just to be more expressive
|
||||||
|
if name.startswith("inner_") or not name.startswith("python"):
|
||||||
|
continue
|
||||||
|
|
||||||
|
if subprocess.run([str(path), '-c', '"exit()"']) != 0:
|
||||||
|
continue
|
||||||
|
|
||||||
# creating inner symlink
|
wrap_interpreter(path)
|
||||||
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}")
|
else:
|
||||||
Path(bin_path, "inner_" + name).symlink_to(target)
|
# could be pip
|
||||||
|
if not name.startswith("pip"):
|
||||||
|
continue
|
||||||
|
|
||||||
# remove original symlink
|
install_pip_hook(path)
|
||||||
print(f"removing original symlink: {path}")
|
|
||||||
path.unlink()
|
|
||||||
|
|
||||||
# creating the wrapper string
|
serious_logger.info("")
|
||||||
print("writing wrapper script")
|
mommy_logger.info("")
|
||||||
with path.open("w") as f:
|
|
||||||
f.write(WRAPPER_TEMPLATE.format(inner_bin=str(inner_bin)))
|
|
||||||
print("making wrapper script executable")
|
|
||||||
path.chmod(path.stat().st_mode | stat.S_IEXEC)
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user