]> git.decadent.org.uk Git - dak.git/blobdiff - utils.py
2004-04-01 James Troup <james@nocrew.org> * utils.py (temp_filename): new helper...
[dak.git] / utils.py
index c537bf8f34df16b1492319b5dbf517e4136284f9..8445fd4d664289f1f6f9ad91335626546a27d2b5 100644 (file)
--- a/utils.py
+++ b/utils.py
@@ -2,7 +2,7 @@
 
 # Utility functions
 # Copyright (C) 2000, 2001, 2002, 2003, 2004  James Troup <james@nocrew.org>
-# $Id: utils.py,v 1.64 2004-03-11 00:20:51 troup Exp $
+# $Id: utils.py,v 1.65 2004-04-01 17:13:10 troup Exp $
 
 ################################################################################
 
@@ -928,6 +928,29 @@ def clean_symlink (src, dest, root):
 
 ################################################################################
 
+def temp_filename(directory=None, dotprefix=None, perms=0700):
+    """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 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;
+
+    return filename;
+
+################################################################################
+
 apt_pkg.init();
 
 Cnf = apt_pkg.newConfiguration();