/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-03-22 01:35:14 UTC
  • mfrom: (7490.7.6 work)
  • mto: This revision was merged to the branch mainline in revision 7499.
  • Revision ID: jelmer@jelmer.uk-20200322013514-7vw1ntwho04rcuj3
merge lp:brz/3.1.

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
 
 
19
17
import os
20
18
 
21
 
from .lazy_import import lazy_import
22
 
lazy_import(globals(), """
23
19
import stat
24
 
import warnings
25
20
 
26
21
from breezy import (
27
22
    errors,
28
23
    osutils,
29
24
    )
30
 
""")
31
25
 
32
26
# not forksafe - but we dont fork.
33
27
_pid = os.getpid()
34
28
_hostname = None
35
29
 
36
30
 
 
31
class AtomicFileAlreadyClosed(errors.PathError):
 
32
 
 
33
    _fmt = ('"%(function)s" called on an AtomicFile after it was closed:'
 
34
            ' "%(path)s"')
 
35
 
 
36
    def __init__(self, path, function):
 
37
        errors.PathError.__init__(self, path=path, extra=None)
 
38
        self.function = function
 
39
 
 
40
 
37
41
class AtomicFile(object):
38
42
    """A file that does an atomic-rename to move into place.
39
43
 
91
95
    def _close_tmpfile(self, func_name):
92
96
        """Close the local temp file in preparation for commit or abort"""
93
97
        if self._fd is None:
94
 
            raise errors.AtomicFileAlreadyClosed(path=self.realfilename,
95
 
                                                 function=func_name)
 
98
            raise AtomicFileAlreadyClosed(path=self.realfilename,
 
99
                                          function=func_name)
96
100
        fd = self._fd
97
101
        self._fd = None
98
102
        os.close(fd)
111
115
        """Discard the file unless already committed."""
112
116
        if self._fd is not None:
113
117
            self.abort()
 
118
 
 
119
    def __enter__(self):
 
120
        return self
 
121
 
 
122
    def __exit__(self, exc_type, exc_val, exc_tb):
 
123
        if exc_type:
 
124
            self.abort()
 
125
            return False
 
126
        self.commit()