~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/atomicfile.py

  • Committer: John Arbash Meinel
  • Date: 2006-08-23 22:16:27 UTC
  • mto: This revision was merged to the branch mainline in revision 1955.
  • Revision ID: john@arbash-meinel.com-20060823221627-fc64105bb12ae770
Ghozzy: Fix Bzr's support of Active FTP (aftp://)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2004, 2005 Canonical Ltd
 
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
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
 
 
18
import codecs
 
19
import errno
18
20
import os
19
 
import sys
20
 
 
21
 
from bzrlib.lazy_import import lazy_import
22
 
lazy_import(globals(), """
23
21
import stat
24
22
import socket
 
23
import sys
25
24
import warnings
26
25
 
27
26
from bzrlib import (
29
28
    osutils,
30
29
    symbol_versioning,
31
30
    )
32
 
""")
 
31
from bzrlib.osutils import rename
33
32
 
34
33
# not forksafe - but we dont fork.
35
34
_pid = os.getpid()
36
 
_hostname = None
 
35
_hostname = socket.gethostname()
37
36
 
38
37
 
39
38
class AtomicFile(object):
48
47
    __slots__ = ['tmpfilename', 'realfilename', '_fd']
49
48
 
50
49
    def __init__(self, filename, mode='wb', new_mode=None):
51
 
        global _hostname
52
 
 
53
50
        self._fd = None
54
 
 
55
 
        if _hostname is None:
56
 
            _hostname = socket.gethostname()
 
51
        assert mode in ('wb', 'wt'), \
 
52
            "invalid AtomicFile mode %r" % mode
57
53
 
58
54
        self.tmpfilename = '%s.%d.%s.tmp' % (filename, _pid, _hostname)
59
55
 
62
58
        flags = os.O_EXCL | os.O_CREAT | os.O_WRONLY
63
59
        if mode == 'wb':
64
60
            flags |= osutils.O_BINARY
65
 
        elif mode != 'wt':
66
 
            raise ValueError("invalid AtomicFile mode %r" % mode)
67
61
 
68
62
        if new_mode is not None:
69
63
            local_mode = new_mode
108
102
    def commit(self):
109
103
        """Close the file and move to final name."""
110
104
        self._close_tmpfile('commit')
111
 
        osutils.rename(self.tmpfilename, self.realfilename)
 
105
        rename(self.tmpfilename, self.realfilename)
112
106
 
113
107
    def abort(self):
114
108
        """Discard temporary file without committing changes."""