/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: Gustav Hartvigsson
  • Date: 2021-01-09 21:36:27 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20210109213627-h1xwcutzy9m7a99b
Added 'Case Preserving Working Tree Use Cases' from Canonical Wiki

* Addod a page from the Canonical Bazaar wiki
  with information on the scmeatics of case
  perserving filesystems an a case insensitive
  filesystem works.
  
  * Needs re-work, but this will do as it is the
    same inforamoton as what was on the linked
    page in the currint documentation.

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