~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
563 by Martin Pool
- AtomicFile emits a warning if it is gc'd without being closed
21
558 by Martin Pool
- All top-level classes inherit from object
22
class AtomicFile(object):
409 by Martin Pool
- New AtomicFile class
23
    """A file that does an atomic-rename to move into place.
24
25
    This also causes hardlinks to break when it's written out.
26
27
    Open this as for a regular file, then use commit() to move into
28
    place or abort() to cancel.
29
431 by Martin Pool
- stat cache is written in utf-8 to accomodate non-ascii
30
    An encoding can be specified; otherwise the default is ascii.
409 by Martin Pool
- New AtomicFile class
31
    """
32
431 by Martin Pool
- stat cache is written in utf-8 to accomodate non-ascii
33
    def __init__(self, filename, mode='wb', encoding=None):
409 by Martin Pool
- New AtomicFile class
34
        if mode != 'wb' and mode != 'wt':
35
            raise ValueError("invalid AtomicFile mode %r" % mode)
36
37
        import os, socket
447 by Martin Pool
- atomicfile temporaries should end in .tmp to make it
38
        self.tmpfilename = '%s.%d.%s.tmp' % (filename, os.getpid(),
409 by Martin Pool
- New AtomicFile class
39
                                             socket.gethostname())
40
        self.realfilename = filename
41
        
431 by Martin Pool
- stat cache is written in utf-8 to accomodate non-ascii
42
        if encoding:
43
            import codecs
563 by Martin Pool
- AtomicFile emits a warning if it is gc'd without being closed
44
            self.f = codecs.open(self.tmpfilename, mode, encoding)
45
        else:
46
            self.f = open(self.tmpfilename, mode)
47
409 by Martin Pool
- New AtomicFile class
48
        self.write = self.f.write
563 by Martin Pool
- AtomicFile emits a warning if it is gc'd without being closed
49
        self.closed = False
50
51
52
    def __repr__(self):
53
        return '%s(%r)' % (self.__class__.__name__,
54
                           self.realfilename)
55
    
409 by Martin Pool
- New AtomicFile class
56
57
    def commit(self):
497 by Martin Pool
- new AtomicFile.close() aborts if appropriate
58
        """Close the file and move to final name."""
410 by Martin Pool
- Fix ignore command and add tests
59
        import sys, os
60
        
563 by Martin Pool
- AtomicFile emits a warning if it is gc'd without being closed
61
        if self.closed:
62
            raise Exception('%r is already closed' % self)
63
565 by Martin Pool
- more invariant checks in AtomicFile
64
        self.closed = True
409 by Martin Pool
- New AtomicFile class
65
        self.f.close()
565 by Martin Pool
- more invariant checks in AtomicFile
66
        self.f = None
67
        
1185.1.40 by Robert Collins
Merge what applied of Alexander Belchenko's win32 patch.
68
        rename(self.tmpfilename, self.realfilename)
409 by Martin Pool
- New AtomicFile class
69
563 by Martin Pool
- AtomicFile emits a warning if it is gc'd without being closed
70
409 by Martin Pool
- New AtomicFile class
71
    def abort(self):
497 by Martin Pool
- new AtomicFile.close() aborts if appropriate
72
        """Discard temporary file without committing changes."""
410 by Martin Pool
- Fix ignore command and add tests
73
        import os
563 by Martin Pool
- AtomicFile emits a warning if it is gc'd without being closed
74
75
        if self.closed:
76
            raise Exception('%r is already closed' % self)
77
565 by Martin Pool
- more invariant checks in AtomicFile
78
        self.closed = True
409 by Martin Pool
- New AtomicFile class
79
        self.f.close()
565 by Martin Pool
- more invariant checks in AtomicFile
80
        self.f = None
409 by Martin Pool
- New AtomicFile class
81
        os.remove(self.tmpfilename)
497 by Martin Pool
- new AtomicFile.close() aborts if appropriate
82
563 by Martin Pool
- AtomicFile emits a warning if it is gc'd without being closed
83
497 by Martin Pool
- new AtomicFile.close() aborts if appropriate
84
    def close(self):
85
        """Discard the file unless already committed."""
86
        if not self.closed:
87
            self.abort()
563 by Martin Pool
- AtomicFile emits a warning if it is gc'd without being closed
88
89
90
    def __del__(self):
91
        if not self.closed:
92
            warn("%r leaked" % self)
428 by Martin Pool
- Use AtomicFile to update statcache.
93