Add remove file action

This commit is contained in:
Kovid Goyal 2025-03-26 09:55:04 +05:30
parent e3f6c88d2c
commit 04f6473f38
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -19,6 +19,7 @@ from calibre.utils.ipc.simple_worker import start_pipe_worker
lock = Lock() lock = Lock()
worker = None worker = None
RMTREE_ACTION = 'rmtree' RMTREE_ACTION = 'rmtree'
UNLINK_ACTION = 'unlink'
def thread_safe(f): def thread_safe(f):
@ -35,6 +36,18 @@ def remove_folder(path: str) -> None:
_send_command(RMTREE_ACTION, os.path.abspath(path)) _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(): def ensure_worker():
global worker global worker
if worker is None: if worker is None:
@ -79,12 +92,12 @@ else:
def main(): def main():
ac_map = {RMTREE_ACTION: remove_dir, UNLINK_ACTION: unlink}
for line in sys.stdin.buffer: for line in sys.stdin.buffer:
if line: if line:
try: try:
cmd = json.loads(line) cmd = json.loads(line)
if cmd['action'] == RMTREE_ACTION: atexit.register(ac_map[cmd['action']], cmd['payload'])
atexit.register(remove_dir, cmd['payload'])
except Exception: except Exception:
import traceback import traceback
traceback.print_exc() traceback.print_exc()