/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 breezy/atomicfile.py

  • Committer: Jelmer Vernooij
  • Date: 2018-10-28 11:33:30 UTC
  • mto: This revision was merged to the branch mainline in revision 7142.
  • Revision ID: jelmer@jelmer.uk-20181028113330-aexu0nqkdti4p2pc
Use sys.executable rather than python for ad-hoc tests.

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