~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/atomicfile.py

  • Committer: Aaron Bentley
  • Date: 2006-02-16 18:52:17 UTC
  • mto: (1558.1.4 Aaron's integration)
  • mto: This revision was merged to the branch mainline in revision 1565.
  • Revision ID: abentley@panoramicfeedback.com-20060216185217-c766d147d91191d8
Added progress bars to merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
 
19
19
from warnings import warn
20
 
 
 
20
from bzrlib.osutils import rename
 
21
import errno
21
22
 
22
23
class AtomicFile(object):
23
24
    """A file that does an atomic-rename to move into place.
30
31
    An encoding can be specified; otherwise the default is ascii.
31
32
    """
32
33
 
33
 
    def __init__(self, filename, mode='wb', encoding=None):
 
34
    def __init__(self, filename, mode='wb', encoding=None, new_mode=None):
34
35
        if mode != 'wb' and mode != 'wt':
35
36
            raise ValueError("invalid AtomicFile mode %r" % mode)
36
37
 
47
48
 
48
49
        self.write = self.f.write
49
50
        self.closed = False
 
51
        self._new_mode = new_mode
50
52
 
51
53
 
52
54
    def __repr__(self):
65
67
        self.f.close()
66
68
        self.f = None
67
69
        
68
 
        if sys.platform == 'win32':
69
 
            # windows cannot rename over an existing file
70
 
            try:
71
 
                os.remove(self.realfilename)
72
 
            except OSError, e:
73
 
                import errno
74
 
                if e.errno != errno.ENOENT:
75
 
                    raise
76
 
                
77
 
        os.rename(self.tmpfilename, self.realfilename)
 
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
        rename(self.tmpfilename, self.realfilename)
78
80
 
79
81
 
80
82
    def abort(self):
97
99
 
98
100
 
99
101
    def __del__(self):
100
 
        if not self.closed:
 
102
        if hasattr(self, 'closed') and not self.closed:
101
103
            warn("%r leaked" % self)
102
104