util: Add a utility for managing temporary files
In multiple situations we need to create temporary files or directories that should not be preserved after compose is finished. Let's add context managers that ensure these get cleaned up. This fixes tests leaving garbage around in /tmp. Signed-off-by: Lubomír Sedlář <lsedlar@redhat.com>
This commit is contained in:
parent
a57bc13e30
commit
857aee05c1
4 changed files with 57 additions and 42 deletions
|
|
@ -25,6 +25,7 @@ import re
|
|||
import urlparse
|
||||
import contextlib
|
||||
import traceback
|
||||
import tempfile
|
||||
|
||||
from kobo.shortcuts import run, force_list
|
||||
from productmd.common import get_major_version
|
||||
|
|
@ -572,3 +573,22 @@ def levenshtein(a, b):
|
|||
mat[j - 1][i - 1] + cost)
|
||||
|
||||
return mat[len(b)][len(a)]
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def temp_dir(log=None, *args, **kwargs):
|
||||
"""Create a temporary directory and ensure it's deleted."""
|
||||
if kwargs.get('dir'):
|
||||
# If we are supposed to create the temp dir in a particular location,
|
||||
# ensure the location already exists.
|
||||
makedirs(kwargs['dir'])
|
||||
dir = tempfile.mkdtemp(*args, **kwargs)
|
||||
try:
|
||||
yield dir
|
||||
finally:
|
||||
try:
|
||||
shutil.rmtree(dir)
|
||||
except OSError as exc:
|
||||
# Okay, we failed to delete temporary dir.
|
||||
if log:
|
||||
log.warning('Error removing %s: %s', dir, exc.strerror)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue