File tree Expand file tree Collapse file tree 3 files changed +23
-0
lines changed Expand file tree Collapse file tree 3 files changed +23
-0
lines changed Original file line number Diff line number Diff line change 1+ import atexit
12import multiprocessing
3+ import os
24import shutil
35from collections .abc import Iterable , Sequence
46from operator import attrgetter
57from pathlib import Path
8+ import tempfile
69from typing import Optional , Union
710
811import attrs
@@ -100,6 +103,21 @@ class ExtractionConfig:
100103 dir_handlers : DirectoryHandlers = BUILTIN_DIR_HANDLERS
101104 verbose : int = 1
102105 progress_reporter : type [ProgressReporter ] = NullProgressReporter
106+ tmp_dir : Path = attrs .field (factory = lambda : Path (tempfile .mkdtemp (prefix = "unblob-tmp-" )))
107+
108+ def __attrs_post_init__ (self ):
109+ """Set environment variables so all subprocesses and handlers use our temp dir"""
110+ for var in ("TMP" , "TMPDIR" , "TEMP" , "TEMPDIR" ):
111+ os .environ [var ] = self .tmp_dir .as_posix ()
112+ atexit .register (self ._cleanup_tmp_dir )
113+
114+ def _cleanup_tmp_dir (self ):
115+ """Remove the temp directory and its contents at the end of the run"""
116+ if self .tmp_dir .exists ():
117+ try :
118+ shutil .rmtree (self .tmp_dir )
119+ except Exception as e :
120+ logger .warning ("Failed to clean up tmp_dir" , tmp_dir = self .tmp_dir , exc_info = e )
103121
104122 def _get_output_path (self , path : Path ) -> Path :
105123 """Return path under extract root."""
Original file line number Diff line number Diff line change @@ -55,6 +55,10 @@ def __init__(
5555 AccessFS .remove_file (config .extract_root ),
5656 AccessFS .make_dir (config .extract_root .parent ),
5757 AccessFS .read_write (log_path ),
58+ # Allow access to the managed temp directory for handlers
59+ AccessFS .read_write (config .tmp_dir ),
60+ AccessFS .remove_dir (config .tmp_dir ),
61+ AccessFS .remove_file (config .tmp_dir ),
5862 * extra_passthrough ,
5963 ]
6064
Original file line number Diff line number Diff line change 11from collections .abc import Iterable
22from pathlib import Path
3+ import tempfile
34from typing import Optional
45from unittest import mock
56
You can’t perform that action at this time.
0 commit comments