~bzr-pqm/bzr/bzr.dev

409 by Martin Pool
- New AtomicFile class
1
# Copyright (C) 2004, 2005 by Canonical Ltd
2
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
17
import errno
18
import os
409 by Martin Pool
- New AtomicFile class
19
563 by Martin Pool
- AtomicFile emits a warning if it is gc'd without being closed
20
from warnings import warn
1185.31.58 by John Arbash Meinel
Updating for new transport tests so that they pass on win32
21
from bzrlib.osutils import rename
563 by Martin Pool
- AtomicFile emits a warning if it is gc'd without being closed
22
558 by Martin Pool
- All top-level classes inherit from object
23
class AtomicFile(object):
409 by Martin Pool
- New AtomicFile class
24
    """A file that does an atomic-rename to move into place.
25
26
    This also causes hardlinks to break when it's written out.
27
28
    Open this as for a regular file, then use commit() to move into
29
    place or abort() to cancel.
30
431 by Martin Pool
- stat cache is written in utf-8 to accomodate non-ascii
31
    An encoding can be specified; otherwise the default is ascii.
409 by Martin Pool
- New AtomicFile class
32
    """
33
1185.58.2 by John Arbash Meinel
Added mode to the appropriate transport functions, and tests to make sure they work.
34
    def __init__(self, filename, mode='wb', encoding=None, new_mode=None):
409 by Martin Pool
- New AtomicFile class
35
        if mode != 'wb' and mode != 'wt':
36
            raise ValueError("invalid AtomicFile mode %r" % mode)
37
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
38
        import socket
447 by Martin Pool
- atomicfile temporaries should end in .tmp to make it
39
        self.tmpfilename = '%s.%d.%s.tmp' % (filename, os.getpid(),
409 by Martin Pool
- New AtomicFile class
40
                                             socket.gethostname())
41
        self.realfilename = filename
42
        
431 by Martin Pool
- stat cache is written in utf-8 to accomodate non-ascii
43
        if encoding:
44
            import codecs
563 by Martin Pool
- AtomicFile emits a warning if it is gc'd without being closed
45
            self.f = codecs.open(self.tmpfilename, mode, encoding)
46
        else:
47
            self.f = open(self.tmpfilename, mode)
48
409 by Martin Pool
- New AtomicFile class
49
        self.write = self.f.write
563 by Martin Pool
- AtomicFile emits a warning if it is gc'd without being closed
50
        self.closed = False
1185.58.2 by John Arbash Meinel
Added mode to the appropriate transport functions, and tests to make sure they work.
51
        self._new_mode = new_mode
563 by Martin Pool
- AtomicFile emits a warning if it is gc'd without being closed
52
53
54
    def __repr__(self):
55
        return '%s(%r)' % (self.__class__.__name__,
56
                           self.realfilename)
57
    
409 by Martin Pool
- New AtomicFile class
58
59
    def commit(self):
497 by Martin Pool
- new AtomicFile.close() aborts if appropriate
60
        """Close the file and move to final name."""
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
61
563 by Martin Pool
- AtomicFile emits a warning if it is gc'd without being closed
62
        if self.closed:
63
            raise Exception('%r is already closed' % self)
64
565 by Martin Pool
- more invariant checks in AtomicFile
65
        self.closed = True
409 by Martin Pool
- New AtomicFile class
66
        self.f.close()
565 by Martin Pool
- more invariant checks in AtomicFile
67
        self.f = None
68
        
1185.12.80 by Aaron Bentley
Added mode preservation to AtomicFile
69
        try:
1185.58.2 by John Arbash Meinel
Added mode to the appropriate transport functions, and tests to make sure they work.
70
            if self._new_mode is None:
71
                self._new_mode = os.lstat(self.realfilename).st_mode
1185.12.80 by Aaron Bentley
Added mode preservation to AtomicFile
72
        except OSError, e:
73
            if e.errno != errno.ENOENT:
74
                raise
1185.58.2 by John Arbash Meinel
Added mode to the appropriate transport functions, and tests to make sure they work.
75
        else:
76
            os.chmod(self.tmpfilename, self._new_mode)
77
1185.1.40 by Robert Collins
Merge what applied of Alexander Belchenko's win32 patch.
78
        rename(self.tmpfilename, self.realfilename)
409 by Martin Pool
- New AtomicFile class
79
563 by Martin Pool
- AtomicFile emits a warning if it is gc'd without being closed
80
409 by Martin Pool
- New AtomicFile class
81
    def abort(self):
497 by Martin Pool
- new AtomicFile.close() aborts if appropriate
82
        """Discard temporary file without committing changes."""
563 by Martin Pool
- AtomicFile emits a warning if it is gc'd without being closed
83
84
        if self.closed:
85
            raise Exception('%r is already closed' % self)
86
565 by Martin Pool
- more invariant checks in AtomicFile
87
        self.closed = True
409 by Martin Pool
- New AtomicFile class
88
        self.f.close()
565 by Martin Pool
- more invariant checks in AtomicFile
89
        self.f = None
409 by Martin Pool
- New AtomicFile class
90
        os.remove(self.tmpfilename)
497 by Martin Pool
- new AtomicFile.close() aborts if appropriate
91
563 by Martin Pool
- AtomicFile emits a warning if it is gc'd without being closed
92
497 by Martin Pool
- new AtomicFile.close() aborts if appropriate
93
    def close(self):
94
        """Discard the file unless already committed."""
95
        if not self.closed:
96
            self.abort()
563 by Martin Pool
- AtomicFile emits a warning if it is gc'd without being closed
97
98
99
    def __del__(self):
1185.11.2 by John Arbash Meinel
LocalTransport tests pass.
100
        if hasattr(self, 'closed') and not self.closed:
563 by Martin Pool
- AtomicFile emits a warning if it is gc'd without being closed
101
            warn("%r leaked" % self)
428 by Martin Pool
- Use AtomicFile to update statcache.
102