# HG changeset patch # User Bernhard Herzog # Date 1176219727 -7200 # Node ID d0d08c7e7d378da2f2d77658233018d9d9943344 # Parent b0014b8f6029d0789585b8722fce7732bb97cdd8 allow optionally set the file permissions in writefile diff -r b0014b8f6029 -r d0d08c7e7d37 treepkg/util.py --- 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):