/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/atomicfile.py

  • Committer: Marius Kruger
  • Date: 2010-07-10 21:28:56 UTC
  • mto: (5384.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 5385.
  • Revision ID: marius.kruger@enerweb.co.za-20100710212856-uq4ji3go0u5se7hx
* Update documentation
* add NEWS

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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
from __future__ import absolute_import
18
17
 
19
18
import os
20
19
 
 
20
from bzrlib.lazy_import import lazy_import
 
21
lazy_import(globals(), """
21
22
import stat
 
23
import socket
 
24
import warnings
22
25
 
23
 
from breezy import (
 
26
from bzrlib import (
24
27
    errors,
25
28
    osutils,
 
29
    symbol_versioning,
26
30
    )
 
31
""")
27
32
 
28
33
# not forksafe - but we dont fork.
29
34
_pid = os.getpid()
30
35
_hostname = None
31
36
 
32
37
 
33
 
class AtomicFileAlreadyClosed(errors.PathError):
34
 
 
35
 
    _fmt = ('"%(function)s" called on an AtomicFile after it was closed:'
36
 
            ' "%(path)s"')
37
 
 
38
 
    def __init__(self, path, function):
39
 
        errors.PathError.__init__(self, path=path, extra=None)
40
 
        self.function = function
41
 
 
42
 
 
43
38
class AtomicFile(object):
44
39
    """A file that does an atomic-rename to move into place.
45
40
 
73
68
        if new_mode is not None:
74
69
            local_mode = new_mode
75
70
        else:
76
 
            local_mode = 0o666
 
71
            local_mode = 0666
77
72
 
78
73
        # Use a low level fd operation to avoid chmodding later.
79
74
        # This may not succeed, but it should help most of the time
84
79
            # the common case is that we won't, though.
85
80
            st = os.fstat(self._fd)
86
81
            if stat.S_IMODE(st.st_mode) != new_mode:
87
 
                osutils.chmod_if_possible(self.tmpfilename, new_mode)
 
82
                os.chmod(self.tmpfilename, new_mode)
 
83
 
 
84
    def _get_closed(self):
 
85
        symbol_versioning.warn('AtomicFile.closed deprecated in bzr 0.10',
 
86
                               DeprecationWarning, stacklevel=2)
 
87
        return self._fd is None
 
88
 
 
89
    closed = property(_get_closed)
88
90
 
89
91
    def __repr__(self):
90
92
        return '%s(%r)' % (self.__class__.__name__,
97
99
    def _close_tmpfile(self, func_name):
98
100
        """Close the local temp file in preparation for commit or abort"""
99
101
        if self._fd is None:
100
 
            raise AtomicFileAlreadyClosed(path=self.realfilename,
101
 
                                          function=func_name)
 
102
            raise errors.AtomicFileAlreadyClosed(path=self.realfilename,
 
103
                                                 function=func_name)
102
104
        fd = self._fd
103
105
        self._fd = None
104
106
        os.close(fd)
118
120
        if self._fd is not None:
119
121
            self.abort()
120
122
 
121
 
    def __enter__(self):
122
 
        return self
123
 
 
124
 
    def __exit__(self, exc_type, exc_val, exc_tb):
125
 
        if exc_type:
126
 
            self.abort()
127
 
            return False
128
 
        self.commit()
 
123
    def __del__(self):
 
124
        if self._fd is not None:
 
125
            warnings.warn("%r leaked" % self)