Compare commits

..

7 Commits

Author SHA1 Message Date
Hazel Noack
9040b26279 implemented option to disable requests 2025-07-30 12:16:34 +02:00
Hazel Noack
98d656b2fe validate type of vars from config file 2025-07-30 12:00:32 +02:00
Hazel Noack
abadff31d8 imporved logging 2025-07-30 11:55:33 +02:00
Hazel Noack
ba36851336 changed the returncode to make sure it detects python 2025-07-30 11:16:03 +02:00
Hazel Noack
d9e6bac410 rewrote resolving the symlinks in comprehension 2025-07-30 11:08:27 +02:00
Hazel Noack
69f6a11874 feat: implemented error handling for empty lists 2025-07-30 11:00:18 +02:00
amnesia
8e8409afc4 typo 2025-07-29 21:13:44 +02:00
2 changed files with 70 additions and 17 deletions

View File

@@ -32,7 +32,7 @@ def config_logging(verbose: bool):
logging.getLogger().setLevel(logging.DEBUG) logging.getLogger().setLevel(logging.DEBUG)
mommy_logger.setLevel(50) mommy_logger.setLevel(50)
serious_logger.setLevel(logging.DEBUG) serious_logger.setLevel(logging.DEBUG)
WRAPPER_TEMPLATE = """#!{inner_bin} WRAPPER_TEMPLATE = """#!{inner_bin}
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
@@ -75,8 +75,8 @@ PIP_HOOK = """# GENERATED BY MOMMY
first_line = text.split("\\n")[0] first_line = text.split("\\n")[0]
if not ("inner_" in first_line and first_line.startswith("#!")): if not ("inner_" in first_line and first_line.startswith("#!")):
continue continue
print(f"mommifying " + str(path)) print(f"mommifying " + str(path))
text = text.replace("inner_", "", 1) text = text.replace("inner_", "", 1)
@@ -94,12 +94,12 @@ def assert_venv(only_warn: bool = False):
exit(1) exit(1)
def write_compile_config(local: bool): def write_compile_config(local: bool, disable_requests: bool = False):
assert_venv(only_warn=not local) assert_venv(only_warn=not local)
compiled_base_dir = VENV_DIRECTORY if local else CONFIG_DIRECTORY compiled_base_dir = VENV_DIRECTORY if local else CONFIG_DIRECTORY
compiled_config_file = compiled_base_dir / COMPILED_CONFIG_FILE_NAME compiled_config_file = compiled_base_dir / COMPILED_CONFIG_FILE_NAME
compiled = compile_config() compiled = compile_config(disable_requests=disable_requests)
mommy_logger.info("mommy writes its moods in %s", compiled_config_file) mommy_logger.info("mommy writes its moods in %s", compiled_config_file)
serious_logger.info("writing compiled config file to %s", compiled_config_file) serious_logger.info("writing compiled config file to %s", compiled_config_file)
compiled_base_dir.mkdir(parents=True, exist_ok=True) compiled_base_dir.mkdir(parents=True, exist_ok=True)
@@ -145,7 +145,7 @@ def install_pip_hook(path: Path):
mommy_logger.info("ahhhhh mommy already watches %s", str(path)) mommy_logger.info("ahhhhh mommy already watches %s", str(path))
serious_logger.info("pip hook already installed at %s", str(path)) serious_logger.info("pip hook already installed at %s", str(path))
return return
mommy_logger.info("mommy needs to keep an eye on this little pip~ %s", str(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)) serious_logger.info("installing pip hook at %s", str(path))
@@ -156,7 +156,7 @@ def install_pip_hook(path: Path):
def cli_compile_config(): def cli_compile_config():
parser = argparse.ArgumentParser(description="only recompile the config") parser = argparse.ArgumentParser(description="only recompile the config")
parser.add_argument( parser.add_argument(
"-v", "--verbose", "-v", "--verbose",
action="store_true", action="store_true",
@@ -169,14 +169,21 @@ def cli_compile_config():
help="compile the config only for the current virtual environment" help="compile the config only for the current virtual environment"
) )
parser.add_argument(
"-r", "--no-requests",
action="store_true",
help="by default if makes one request to GitHub to fetch the newest responses, this disables that"
)
args = parser.parse_args() args = parser.parse_args()
write_compile_config(args.local) config_logging(args.verbose)
write_compile_config(args.local, disable_requests=args.no_requests)
def mommify_venv(): def mommify_venv():
parser = argparse.ArgumentParser(description="patch the virtual environment to use mommy") parser = argparse.ArgumentParser(description="patch the virtual environment to use mommy")
parser.add_argument( parser.add_argument(
"-v", "--verbose", "-v", "--verbose",
action="store_true", action="store_true",
@@ -189,6 +196,12 @@ def mommify_venv():
help="compile the config only for the current virtual environment" help="compile the config only for the current virtual environment"
) )
parser.add_argument(
"-r", "--no-requests",
action="store_true",
help="by default if makes one request to GitHub to fetch the newest responses, this disables that"
)
args = parser.parse_args() args = parser.parse_args()
config_logging(args.verbose) config_logging(args.verbose)
@@ -203,11 +216,18 @@ def mommify_venv():
mommy_logger.info("mommy looks in %s to mess your system up~ <33", str(bin_path)) 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)) serious_logger.info("scanning binary directory of venv at %s", str(bin_path))
resolved_symlinks = {} # resolving the symlinks before making edits to anything because else it will mess up the resolving
for path in list(bin_path.iterdir()): # and link to the wrapper instead of the original script
if path.is_symlink(): resolved_symlinks = {
resolved_symlinks[path.name] = path.resolve() path.name: path.resolve()
for path in bin_path.iterdir()
if path.is_symlink()
}
serious_logger.debug("resolved symlinks:\n%s", "\n".join(
f"\t{name} => {str(target)}"
for name, target in resolved_symlinks.items()
))
for path in list(bin_path.iterdir()): for path in list(bin_path.iterdir()):
name = path.name name = path.name
@@ -218,7 +238,8 @@ def mommify_venv():
if name.startswith("inner_"): if name.startswith("inner_"):
continue continue
if subprocess.run([str(path), '-c', '"exit(0)"'], stdout=sys.devnull).returncode != 0: RANDOM_RETURNCODE = 161
if subprocess.run([str(path), '-c', f'exit({RANDOM_RETURNCODE})']).returncode != RANDOM_RETURNCODE:
continue continue
wrap_interpreter(path, resolved_symlinks[path.name]) wrap_interpreter(path, resolved_symlinks[path.name])

View File

@@ -25,7 +25,7 @@ ADDITIONAL_ENV_VARS = {
def _load_config_file(config_file: Path) -> Dict[str, List[str]]: def _load_config_file(config_file: Path) -> dict:
with config_file.open("r") as f: with config_file.open("r") as f:
data = toml.load(f) data = toml.load(f)
@@ -95,7 +95,22 @@ def compile_config(disable_requests: bool = False) -> dict:
# load config file # load config file
config_file = get_config_file() config_file = get_config_file()
if config_file is not None: if config_file is not None:
config.update(_load_config_file(config_file)) c = _load_config_file(config_file)
serious_logger.debug(
"config at %s:\n%s\n",
config_file,
json.dumps(c, indent=4)
)
config["mood"] = c.get("moods", config["mood"])
c_vars: dict = c.get("vars", {})
# validate the config var values
for key, val in c_vars.items():
if not isinstance(val, list):
mommy_logger.error("mommy needs the value of %s to be a list~", key)
serious_logger.error("the value of %s is not a list", key)
exit(1)
config.update(c_vars)
# fill config with env # fill config with env
for key, conf in config_definition.items(): for key, conf in config_definition.items():
@@ -103,6 +118,23 @@ def compile_config(disable_requests: bool = False) -> dict:
if val is not None: if val is not None:
config[key] = val.split("/") config[key] = val.split("/")
# validate empty variables
empty_values = []
for key, value in config.items():
if len(value) == 0:
empty_values.append(key)
if len(empty_values) > 0:
empty_values_sting = ", ".join(empty_values)
mommy_logger.error(
"mommy is very displeased that you didn't config the key(s) %s",
empty_values_sting,
)
serious_logger.error(
"the following keys have empty values and need to be configured: %s",
empty_values_sting
)
exit(1)
# validate moods # validate moods
for mood in config["mood"]: for mood in config["mood"]:
if mood not in mood_definitions: if mood not in mood_definitions: