manga-dlp/mangadlp/hooks.py

41 lines
1.1 KiB
Python
Raw Normal View History

2022-08-13 18:52:32 +02:00
import os
import subprocess
from loguru import logger as log
2022-08-13 18:52:32 +02:00
def run_hook(command: str, hook_type: str, **kwargs) -> int:
"""
2022-08-13 18:52:32 +02:00
Args:
command (str): command to run
hook_type (str): type of the hook
kwargs: key value pairs of env vars to set
2022-08-13 18:52:32 +02:00
Returns:
exit_code (int): exit code of command
2022-08-13 18:52:32 +02:00
"""
# check if hook commands are empty
if not command or command == "None":
log.debug(f"Hook '{hook_type}' empty. Not running")
return 2
2022-08-13 18:52:32 +02:00
command_list = command.split(" ")
2022-08-13 18:52:32 +02:00
# setting env vars
for key, value in kwargs.items():
os.environ[f"MDLP_{key.upper()}"] = str(value)
2022-08-13 18:52:32 +02:00
# running command
log.info(f"Hook '{hook_type}' - running command: '{command}'")
proc = subprocess.run(command_list, check=False, timeout=15, encoding="utf8")
exit_code = proc.returncode
2022-08-13 18:52:32 +02:00
if exit_code == 0:
log.debug("Hook returned status code 0. All good")
else:
log.warning(f"Hook returned status code {exit_code}. Possible error")
2022-08-13 18:52:32 +02:00
# return exit code of command
return exit_code