Skip to content

Commit

Permalink
fix the patch and rename installer.py to installer.pyw to invoke run_…
Browse files Browse the repository at this point in the history
…under_pythonw which creates the flag to hide the console
  • Loading branch information
Ati1707 committed Nov 15, 2024
1 parent f2424e7 commit 1fa1469
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
3 changes: 1 addition & 2 deletions helper/file_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ def limit_logger_files():
# Delete the oldest files to ensure only 2 are kept
files_to_delete = len(log_files) - 2
for file in log_files_sorted[:files_to_delete]:
print(f"Deleting: {file}")
file.unlink()

def create_logger():
Expand All @@ -73,7 +72,7 @@ def create_logger():
log_file = f"logs/{now}.log"
logging.basicConfig(
filename=log_file,
level=logging.DEBUG,
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s", datefmt='%m/%d/%Y %I:%M:%S'
)
return logging.getLogger(__name__)
1 change: 0 additions & 1 deletion installer.py → installer.pyw
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import content_database
import logging
import patches # Needed to apply monkey patching
import pathlib
import patoolib
Expand Down
26 changes: 21 additions & 5 deletions patches.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import logging

from patoolib.mime import *
from patoolib.util import *


def patched_run_checked(cmd, ret_ok=(0,), **kwargs):
"""Run command and raise PatoolError on error with an additional condition."""
retcode = run(cmd, **kwargs)
retcode = patched_run(cmd, **kwargs)

# Original condition, with an added condition:
# Add a custom check, if retcode is not a specific value.
Expand Down Expand Up @@ -82,22 +85,35 @@ def patched_run(cmd: Sequence[str], verbosity: int = 0, **kwargs) -> int:
if verbosity < 1:
# hide command output on stdout
kwargs['stdout'] = subprocess.DEVNULL
kwargs['stderr'] = subprocess.DEVNULL
if kwargs:
if verbosity > 0:
info = ", ".join(f"{k}={shell_quote(str(v))}" for k, v in kwargs.items())
log_info(f" with {info}")
kwargs['stdout'] = subprocess.PIPE
kwargs['stderr'] = subprocess.PIPE
if kwargs.get("shell"):
# for shell calls the command must be a string
cmd = " ".join(cmd)
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = subprocess.SW_HIDE
res = subprocess.run(cmd, startupinfo=startupinfo, **kwargs)
res = subprocess.run(cmd, text=True, **kwargs)
if res.stdout:
log_subprocess_output(res.stdout, level=logging.INFO)
if res.stderr:
pass
log_subprocess_output(res.stderr, level=logging.ERROR)
return res.returncode

def log_subprocess_output(output: str, level: int):
logger = logging.getLogger(__name__)
for line in output.splitlines():
logger.log(level, line)

# Apply the patch
import patoolib
import importlib

patoolib.util.run = patched_run
patoolib.util.run_checked = patched_run_checked
patoolib.mime.guess_mime_file = patched_guess_mime_file

importlib.reload(patoolib)

0 comments on commit 1fa1469

Please sign in to comment.