From 04f6473f38e7b85fa3b84b3154eda20737a9bdf9 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 26 Mar 2025 09:55:04 +0530 Subject: [PATCH] Add remove file action --- src/calibre/utils/safe_atexit.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/calibre/utils/safe_atexit.py b/src/calibre/utils/safe_atexit.py index 89175f2681..5c9d1df1d8 100644 --- a/src/calibre/utils/safe_atexit.py +++ b/src/calibre/utils/safe_atexit.py @@ -19,6 +19,7 @@ from calibre.utils.ipc.simple_worker import start_pipe_worker lock = Lock() worker = None RMTREE_ACTION = 'rmtree' +UNLINK_ACTION = 'unlink' def thread_safe(f): @@ -35,6 +36,18 @@ def remove_folder(path: str) -> None: _send_command(RMTREE_ACTION, os.path.abspath(path)) +@thread_safe +def remove_file(path: str) -> None: + _send_command(UNLINK_ACTION, os.path.abspath(path)) + + +def unlink(path): + with suppress(Exception): + import os as oss + if oss.path.exists(path): + oss.remove(path) + + def ensure_worker(): global worker if worker is None: @@ -79,12 +92,12 @@ else: def main(): + ac_map = {RMTREE_ACTION: remove_dir, UNLINK_ACTION: unlink} for line in sys.stdin.buffer: if line: try: cmd = json.loads(line) - if cmd['action'] == RMTREE_ACTION: - atexit.register(remove_dir, cmd['payload']) + atexit.register(ac_map[cmd['action']], cmd['payload']) except Exception: import traceback traceback.print_exc()