~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
17
18
563 by Martin Pool
- AtomicFile emits a warning if it is gc'd without being closed
19
from warnings import warn
1185.1.40 by Robert Collins
Merge what applied of Alexander Belchenko's win32 patch.
20
from osutils import rename
1185.12.80 by Aaron Bentley
Added mode preservation to AtomicFile
21
import errno
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
431 by Martin Pool
- stat cache is written in utf-8 to accomodate non-ascii
34
    def __init__(self, filename, mode='wb', encoding=None):
409 by Martin Pool
- New AtomicFile class
35
        if mode != 'wb' and mode != 'wt':
36
            raise ValueError("invalid AtomicFile mode %r" % mode)
37
38
        import os, 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
51
52
53
    def __repr__(self):
54
        return '%s(%r)' % (self.__class__.__name__,
55
                           self.realfilename)
56
    
409 by Martin Pool
- New AtomicFile class
57
58
    def commit(self):
497 by Martin Pool
- new AtomicFile.close() aborts if appropriate
59
        """Close the file and move to final name."""
410 by Martin Pool
- Fix ignore command and add tests
60
        import sys, os
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:
70
            stat = os.lstat(self.realfilename)
71
            os.chmod(self.tmpfilename, stat.st_mode)
72
        except OSError, e:
73
            if e.errno != errno.ENOENT:
74
                raise
1185.1.40 by Robert Collins
Merge what applied of Alexander Belchenko's win32 patch.
75
        rename(self.tmpfilename, self.realfilename)
409 by Martin Pool
- New AtomicFile class
76
563 by Martin Pool
- AtomicFile emits a warning if it is gc'd without being closed
77
409 by Martin Pool
- New AtomicFile class
78
    def abort(self):
497 by Martin Pool
- new AtomicFile.close() aborts if appropriate
79
        """Discard temporary file without committing changes."""
410 by Martin Pool
- Fix ignore command and add tests
80
        import os
563 by Martin Pool
- AtomicFile emits a warning if it is gc'd without being closed
81
82
        if self.closed:
83
            raise Exception('%r is already closed' % self)
84
565 by Martin Pool
- more invariant checks in AtomicFile
85
        self.closed = True
409 by Martin Pool
- New AtomicFile class
86
        self.f.close()
565 by Martin Pool
- more invariant checks in AtomicFile
87
        self.f = None
409 by Martin Pool
- New AtomicFile class
88
        os.remove(self.tmpfilename)
497 by Martin Pool
- new AtomicFile.close() aborts if appropriate
89
563 by Martin Pool
- AtomicFile emits a warning if it is gc'd without being closed
90
497 by Martin Pool
- new AtomicFile.close() aborts if appropriate
91
    def close(self):
92
        """Discard the file unless already committed."""
93
        if not self.closed:
94
            self.abort()
563 by Martin Pool
- AtomicFile emits a warning if it is gc'd without being closed
95
96
97
    def __del__(self):
1185.11.2 by John Arbash Meinel
LocalTransport tests pass.
98
        if hasattr(self, 'closed') and not self.closed:
563 by Martin Pool
- AtomicFile emits a warning if it is gc'd without being closed
99
            warn("%r leaked" % self)
428 by Martin Pool
- Use AtomicFile to update statcache.
100