From ac7360ffe8159278f389b27b786ce62873fe6d4f Mon Sep 17 00:00:00 2001 From: Hazel Noack Date: Tue, 29 Jul 2025 11:31:49 +0200 Subject: [PATCH] added logging --- python_mommy_venv/__init__.py | 4 -- python_mommy_venv/__main__.py | 103 +++++++++++++++++++++++----------- 2 files changed, 71 insertions(+), 36 deletions(-) diff --git a/python_mommy_venv/__init__.py b/python_mommy_venv/__init__.py index 48a07b0..2f18315 100644 --- a/python_mommy_venv/__init__.py +++ b/python_mommy_venv/__init__.py @@ -1,10 +1,6 @@ import random -import subprocess import sys from typing import Optional -import os -import re -import signal from .config import get_mood, get_template_values from .static import RESPONSES, Situation, colors diff --git a/python_mommy_venv/__main__.py b/python_mommy_venv/__main__.py index e8bd205..a154930 100644 --- a/python_mommy_venv/__main__.py +++ b/python_mommy_venv/__main__.py @@ -1,6 +1,8 @@ import sys from pathlib import Path import stat +import subprocess +import logging import toml @@ -9,6 +11,19 @@ from .static import Situation 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(): s = "positive" if len(sys.argv) > 1: @@ -83,60 +98,84 @@ PIP_HOOK = """# GENERATED BY MOMMY 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 with path.open("r") as f: text = f.read() 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 - 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) with path.open("w") as f: f.write(text) + 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(): - if path.name.startswith("pip"): - mommify_pip(path) - continue - + mommy_logger.info("mommy looks in %s to mess your system up~ <33", str(bin_path)) + serious_logger.info("scanning binary directory of venv at %s", str(bin_path)) + + for path in list(bin_path.iterdir()): name = path.name - if name.startswith("inner_"): - continue - target = path.resolve() - print("") - print(f"modifying {name} ({target})") + if path.is_symlink(): + # 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 - inner_bin = Path(bin_path, "inner_" + name) - if inner_bin.exists(): - print(f"inner symlink does already exist {inner_bin}") - print("skipping") - continue + wrap_interpreter(path) - print(f"creating symlink: {inner_bin} -> {target}") - Path(bin_path, "inner_" + name).symlink_to(target) + else: + # could be pip + if not name.startswith("pip"): + continue - # remove original symlink - print(f"removing original symlink: {path}") - path.unlink() + install_pip_hook(path) - # creating the wrapper string - print("writing wrapper script") - 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) + serious_logger.info("") + mommy_logger.info("")