~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/atomicfile.py

  • Committer: Martin Pool
  • Date: 2005-09-29 05:06:14 UTC
  • mto: (1185.12.2) (1393.1.12)
  • mto: This revision was merged to the branch mainline in revision 1396.
  • Revision ID: mbp@sourcefrog.net-20050929050614-a41e6f72af36bb4c
- better error on failing to import bzrlib

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
 
99
88
 
100
89
 
101
90
    def __del__(self):
102
 
        if hasattr(self, 'closed') and not self.closed:
 
91
        if not self.closed:
103
92
            warn("%r leaked" % self)
104
93