~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/atomicfile.py

  • Committer: Robert Collins
  • Date: 2006-01-05 22:30:59 UTC
  • mto: (1534.1.4 integration)
  • mto: This revision was merged to the branch mainline in revision 1536.
  • Revision ID: robertc@robertcollins.net-20060105223059-a8b64f7b47cf12fb
 * bzrlib.osutils.safe_unicode now exists to provide parameter coercion
   for functions that need unicode strings. (Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
 
19
19
from warnings import warn
20
 
from osutils import rename
 
20
from bzrlib.osutils import rename
21
21
import errno
22
22
 
23
23
class AtomicFile(object):
31
31
    An encoding can be specified; otherwise the default is ascii.
32
32
    """
33
33
 
34
 
    def __init__(self, filename, mode='wb', encoding=None):
 
34
    def __init__(self, filename, mode='wb', encoding=None, new_mode=None):
35
35
        if mode != 'wb' and mode != 'wt':
36
36
            raise ValueError("invalid AtomicFile mode %r" % mode)
37
37
 
48
48
 
49
49
        self.write = self.f.write
50
50
        self.closed = False
 
51
        self._new_mode = new_mode
51
52
 
52
53
 
53
54
    def __repr__(self):
67
68
        self.f = None
68
69
        
69
70
        try:
70
 
            stat = os.lstat(self.realfilename)
71
 
            os.chmod(self.tmpfilename, stat.st_mode)
 
71
            if self._new_mode is None:
 
72
                self._new_mode = os.lstat(self.realfilename).st_mode
72
73
        except OSError, e:
73
74
            if e.errno != errno.ENOENT:
74
75
                raise
 
76
        else:
 
77
            os.chmod(self.tmpfilename, self._new_mode)
 
78
 
75
79
        rename(self.tmpfilename, self.realfilename)
76
80
 
77
81