Mercurial > treepkg
changeset 59:9cb94b9ac6a6
merge
author | Bernhard Herzog <bh@intevation.de> |
---|---|
date | Tue, 10 Apr 2007 18:17:53 +0200 |
parents | 83e1aa122ad0 (current diff) 39b2deea8481 (diff) |
children | 353912e12d53 |
files | |
diffstat | 3 files changed, 18 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- 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")
--- 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)
--- 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):