/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: 2020-04-05 19:11:34 UTC
  • mto: (7490.7.16 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200405191134-0aebh8ikiwygxma5
Populate the .gitignore file.

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