Commits (2)
......@@ -18,6 +18,7 @@
# along with diffoscope. If not, see <https://www.gnu.org/licenses/>.
import os
import re
import sys
import shutil
import logging
......@@ -78,16 +79,34 @@ def clean_all_temp_files():
_FILES.clear()
if _BASEDIR is not None:
logger.debug("Cleaning top-level temporary directory %s", _BASEDIR)
logger.debug(
"Cleaning top-level temporary directory %s", _BASEDIR.name
)
shutil.rmtree(_BASEDIR, ignore_errors=True)
shutil.rmtree(_BASEDIR.name, ignore_errors=True)
def _get_base_temporary_directory():
global _BASEDIR
if _BASEDIR is None:
try:
# Try and generate a potentially-useful suffix to our temporary directory
suffix = "_{}".format(
re.sub(
r"[^\w]",
"",
os.path.basename(os.path.dirname(sys.argv[-1])),
)[-10:]
)
except IndexError:
suffix = ""
# Alias the TemporaryDirectory instance (not the .name instance) as the
# directory may be reference-counted away.
_BASEDIR = tempfile.TemporaryDirectory(
dir=tempfile.gettempdir(), prefix="diffoscope_"
dir=tempfile.gettempdir(),
prefix="diffoscope_",
suffix=suffix,
)
logger.debug(
"Created top-level temporary directory: %s (free space: %s)",
......