changeset 57:d0d08c7e7d37

allow optionally set the file permissions in writefile
author Bernhard Herzog <bh@intevation.de>
date Tue, 10 Apr 2007 17:42:07 +0200
parents b0014b8f6029
children 39b2deea8481
files treepkg/util.py
diffstat 1 files changed, 9 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/treepkg/util.py	Tue Apr 03 20:51:44 2007 +0200
+++ b/treepkg/util.py	Tue Apr 10 17:42:07 2007 +0200
@@ -83,12 +83,18 @@
 
 
 
-def writefile(filename, contents):
+def writefile(filename, contents, permissions=None):
     """Write contents to filename in an atomic way.
 
     The contents are first written to a temporary file in the same
     directory as filename.  Once the contents are written, the temporary
     file is closed and renamed to filename.
+
+    The optional parameter permissions, if given, are the permissions
+    for the new file.  By default, or if the parameter is None, the
+    default permissions set by the tempfile.mkstemp are used which means
+    that the file is only readable for the user that created the file.
+    The permissions value is used as the second parameter to os.chmod.
     """
     dirname, basename = os.path.split(filename)
     fileno, tempname = tempfile.mkstemp("", basename, dirname)
@@ -97,6 +103,8 @@
         if not contents.endswith("\n"):
             os.write(fileno, "\n")
         os.close(fileno)
+        if permissions is not None:
+            os.chmod(tempname, permissions)
         os.rename(tempname, filename)
     finally:
         if os.path.exists(tempname):
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)