summaryrefslogtreecommitdiffstats
path: root/nova/utils.py
diff options
context:
space:
mode:
authorJohannes Erdfelt <johannes.erdfelt@rackspace.com>2012-02-28 05:54:48 +0000
committerJohannes Erdfelt <johannes.erdfelt@rackspace.com>2012-02-28 05:56:58 +0000
commitf0d5df523b982ef1737dc0ee2e698b13041af64c (patch)
treea2a8cd2c8ca465aa644fc4eb1a3162224b0d7124 /nova/utils.py
parentf01b9b8dd25d763e652259a0f99264d93661b29f (diff)
downloadnova-f0d5df523b982ef1737dc0ee2e698b13041af64c.tar.gz
nova-f0d5df523b982ef1737dc0ee2e698b13041af64c.tar.xz
nova-f0d5df523b982ef1737dc0ee2e698b13041af64c.zip
Add utils.tempdir() context manager for easy temp dirs
Fixes bug 883323 (and others) Users of tempfile.mkdtemp() need to make sure the directory is cleaned up when it's done being used. Unfortunately, not all of the code does so at all, or safely (by using a try/finally block). Change-Id: I270109d83efec4f8b3dd954021493f4d96c6ab79
Diffstat (limited to 'nova/utils.py')
-rw-r--r--nova/utils.py14
1 files changed, 14 insertions, 0 deletions
diff --git a/nova/utils.py b/nova/utils.py
index 6bb0dd0f2..ef4932146 100644
--- a/nova/utils.py
+++ b/nova/utils.py
@@ -31,9 +31,11 @@ import pyclbr
import random
import re
import shlex
+import shutil
import socket
import struct
import sys
+import tempfile
import time
import types
import uuid
@@ -1543,3 +1545,15 @@ def temporary_chown(path, owner_uid=None):
finally:
if orig_uid != owner_uid:
execute('chown', orig_uid, path, run_as_root=True)
+
+
+@contextlib.contextmanager
+def tempdir(**kwargs):
+ tmpdir = tempfile.mkdtemp(**kwargs)
+ try:
+ yield tmpdir
+ finally:
+ try:
+ shutil.rmtree(tmpdir)
+ except OSError, e:
+ LOG.debug(_('Could not remove tmpdir: %s'), str(e))