# HG changeset patch # User Bernhard Herzog # Date 1176221873 -7200 # Node ID 9cb94b9ac6a63e00aca7031adf176717bb00ab11 # Parent 83e1aa122ad0c29f9e824c366b1f45c330c5cae1# Parent 39b2deea8481170afc4eefc675abc5917e455d80 merge diff -r 83e1aa122ad0 -r 9cb94b9ac6a6 test/test_status.py --- a/test/test_status.py Tue Apr 10 12:20:47 2007 +0200 +++ b/test/test_status.py Tue Apr 10 18:17:53 2007 +0200 @@ -8,6 +8,7 @@ """Tests for the Status classes""" import os +import stat import unittest from datetime import datetime @@ -35,6 +36,13 @@ otherstatus = RevisionStatus(self.filename) self.assertEquals(otherstatus.status.name, "error") + def test_status_file_permissions(self): + status = RevisionStatus(self.filename) + status.error() + + mode = os.stat(self.filename).st_mode + self.assertEquals(stat.S_IMODE(mode) & 0444, 0444) + def test_getting_unknown_fields(self): status = RevisionStatus(self.filename) self.assertRaises(AttributeError, getattr, status, "unknown_field") diff -r 83e1aa122ad0 -r 9cb94b9ac6a6 treepkg/status.py --- a/treepkg/status.py Tue Apr 10 12:20:47 2007 +0200 +++ b/treepkg/status.py Tue Apr 10 18:17:53 2007 +0200 @@ -152,7 +152,7 @@ if field in self._values: lines.append("%s: %s\n" % (field, desc.serialize(self._values[field]))) - util.writefile(self._filename, "".join(lines)) + util.writefile(self._filename, "".join(lines), 0644) def __getattr__(self, attr): desc = self._fields.get(attr) diff -r 83e1aa122ad0 -r 9cb94b9ac6a6 treepkg/util.py --- a/treepkg/util.py Tue Apr 10 12:20:47 2007 +0200 +++ b/treepkg/util.py Tue Apr 10 18:17:53 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):