~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/atomicfile.py

  • Committer: Robert Collins
  • Date: 2005-10-06 22:15:52 UTC
  • mfrom: (1185.13.2)
  • mto: This revision was merged to the branch mainline in revision 1420.
  • Revision ID: robertc@robertcollins.net-20051006221552-9b15c96fa504e0ad
mergeĀ fromĀ upstream

Show diffs side-by-side

added added

removed removed

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