~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/atomicfile.py

merge merge tweaks from aaron, which includes latest .dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2004, 2005 by Canonical Ltd
2
 
#
 
2
 
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
#
 
7
 
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
12
 
#
 
12
 
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
 
import errno
18
 
import os
 
17
 
19
18
 
20
19
from warnings import warn
21
 
from bzrlib.osutils import rename
 
20
 
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
 
38
 
        import socket
 
37
        import os, socket
39
38
        self.tmpfilename = '%s.%d.%s.tmp' % (filename, os.getpid(),
40
39
                                             socket.gethostname())
41
40
        self.realfilename = filename
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):
58
56
 
59
57
    def commit(self):
60
58
        """Close the file and move to final name."""
61
 
 
 
59
        import sys, os
 
60
        
62
61
        if self.closed:
63
62
            raise Exception('%r is already closed' % self)
64
63
 
66
65
        self.f.close()
67
66
        self.f = None
68
67
        
69
 
        try:
70
 
            if self._new_mode is None:
71
 
                self._new_mode = os.lstat(self.realfilename).st_mode
72
 
        except OSError, e:
73
 
            if e.errno != errno.ENOENT:
74
 
                raise
75
 
        else:
76
 
            os.chmod(self.tmpfilename, self._new_mode)
77
 
 
78
 
        rename(self.tmpfilename, self.realfilename)
 
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)
79
78
 
80
79
 
81
80
    def abort(self):
82
81
        """Discard temporary file without committing changes."""
 
82
        import os
83
83
 
84
84
        if self.closed:
85
85
            raise Exception('%r is already closed' % self)
97
97
 
98
98
 
99
99
    def __del__(self):
100
 
        if hasattr(self, 'closed') and not self.closed:
 
100
        if not self.closed:
101
101
            warn("%r leaked" % self)
102
102