/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-08-14 21:37:46 UTC
  • mto: This revision was merged to the branch mainline in revision 7083.
  • Revision ID: jelmer@jelmer.uk-20180814213746-oxx6tcn57k8k98ed
Fix an ignore test. Make AtomicFile a contextmanager.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
import os
20
20
 
21
 
from .lazy_import import lazy_import
22
 
lazy_import(globals(), """
23
21
import stat
24
22
import warnings
25
23
 
27
25
    errors,
28
26
    osutils,
29
27
    )
30
 
""")
31
28
 
32
29
# not forksafe - but we dont fork.
33
30
_pid = os.getpid()
34
31
_hostname = None
35
32
 
36
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
 
37
44
class AtomicFile(object):
38
45
    """A file that does an atomic-rename to move into place.
39
46
 
91
98
    def _close_tmpfile(self, func_name):
92
99
        """Close the local temp file in preparation for commit or abort"""
93
100
        if self._fd is None:
94
 
            raise errors.AtomicFileAlreadyClosed(path=self.realfilename,
95
 
                                                 function=func_name)
 
101
            raise AtomicFileAlreadyClosed(path=self.realfilename,
 
102
                                          function=func_name)
96
103
        fd = self._fd
97
104
        self._fd = None
98
105
        os.close(fd)
111
118
        """Discard the file unless already committed."""
112
119
        if self._fd is not None:
113
120
            self.abort()
 
121
 
 
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()