]> git.decadent.org.uk Git - dak.git/commitdiff
temp_filename
authorJoerg Jaspert <joerg@debian.org>
Fri, 23 Jan 2009 23:06:54 +0000 (00:06 +0100)
committerJoerg Jaspert <joerg@debian.org>
Fri, 23 Jan 2009 23:06:54 +0000 (00:06 +0100)
stop using mktemp and use mkstemp. Also use the functions with all their
features, like setting prefix, instead of trying to do that ourself.
Yikes, one-liner now, could remove the function alltogether and instead
use mkstemp directly, but *maybe* we want to get more code into here,
so lets keep it for now.

Signed-off-by: Joerg Jaspert <joerg@debian.org>
daklib/utils.py

index 0fa3eaa602153db6dc832a4795de7c055de0823b..830794833f13cc870a3d36e3aa7a2d4974236b25 100755 (executable)
@@ -1358,26 +1358,16 @@ def clean_symlink (src, dest, root):
 
 ################################################################################
 
 
 ################################################################################
 
-def temp_filename(directory=None, dotprefix=None, perms=0700):
+def temp_filename(directory=None, prefix="dak", suffix=""):
     """Return a secure and unique filename by pre-creating it.
 If 'directory' is non-null, it will be the directory the file is pre-created in.
     """Return a secure and unique filename by pre-creating it.
 If 'directory' is non-null, it will be the directory the file is pre-created in.
-If 'dotprefix' is non-null, the filename will be prefixed with a '.'."""
+If 'prefix' is non-null, the filename will be prefixed with it, default is dak.
+If 'suffix' is non-null, the filename will end with it.
 
 
-    if directory:
-        old_tempdir = tempfile.tempdir
-        tempfile.tempdir = directory
-
-    filename = tempfile.mktemp()
-
-    if dotprefix:
-        filename = "%s/.%s" % (os.path.dirname(filename), os.path.basename(filename))
-    fd = os.open(filename, os.O_RDWR|os.O_CREAT|os.O_EXCL, perms)
-    os.close(fd)
-
-    if directory:
-        tempfile.tempdir = old_tempdir
+Returns a pair (fd, name).
+"""
 
 
-    return filename
+    return tempfile.mkstemp(suffix, prefix, directory)
 
 ################################################################################
 
 
 ################################################################################