~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/atomicfile.py

  • Committer: Robert Collins
  • Date: 2007-07-04 08:08:13 UTC
  • mfrom: (2572 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2587.
  • Revision ID: robertc@robertcollins.net-20070704080813-wzebx0r88fvwj5rq
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

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