~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/atomicfile.py

  • Committer: Alexander Belchenko
  • Date: 2006-07-30 06:52:39 UTC
  • mto: (1711.2.111 jam-integration)
  • mto: This revision was merged to the branch mainline in revision 1906.
  • Revision ID: bialix@ukr.net-20060730065239-03b4ac02d9f56202
branding: change Bazaar-NG to Bazaar

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
 
17
import errno
 
18
import os
18
19
 
19
20
from warnings import warn
20
 
 
 
21
from bzrlib.osutils import rename
21
22
 
22
23
class AtomicFile(object):
23
24
    """A file that does an atomic-rename to move into place.
30
31
    An encoding can be specified; otherwise the default is ascii.
31
32
    """
32
33
 
33
 
    def __init__(self, filename, mode='wb', encoding=None):
 
34
    def __init__(self, filename, mode='wb', encoding=None, new_mode=None):
34
35
        if mode != 'wb' and mode != 'wt':
35
36
            raise ValueError("invalid AtomicFile mode %r" % mode)
36
37
 
37
 
        import os, socket
 
38
        import socket
38
39
        self.tmpfilename = '%s.%d.%s.tmp' % (filename, os.getpid(),
39
40
                                             socket.gethostname())
40
41
        self.realfilename = filename
47
48
 
48
49
        self.write = self.f.write
49
50
        self.closed = False
 
51
        self._new_mode = new_mode
50
52
 
51
53
 
52
54
    def __repr__(self):
56
58
 
57
59
    def commit(self):
58
60
        """Close the file and move to final name."""
59
 
        import sys, os
60
 
        
 
61
 
61
62
        if self.closed:
62
63
            raise Exception('%r is already closed' % self)
63
64
 
65
66
        self.f.close()
66
67
        self.f = None
67
68
        
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)
 
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)
78
79
 
79
80
 
80
81
    def abort(self):
81
82
        """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 not self.closed:
 
100
        if hasattr(self, 'closed') and not self.closed:
101
101
            warn("%r leaked" % self)
102
102