/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/lockable_files.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:
1
 
# Copyright (C) 2005, 2006, 2008, 2009 Canonical Ltd
 
1
# Copyright (C) 2005-2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
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 bzrlib.lazy_import import lazy_import
 
17
from __future__ import absolute_import
 
18
 
 
19
from .lazy_import import lazy_import
18
20
lazy_import(globals(), """
19
 
import codecs
20
 
import warnings
21
 
 
22
 
from bzrlib import (
 
21
from breezy import (
23
22
    counted_lock,
24
 
    errors,
25
23
    lock,
26
 
    osutils,
27
24
    transactions,
28
25
    urlutils,
29
26
    )
30
27
""")
31
28
 
32
 
from bzrlib.decorators import (
 
29
from . import (
 
30
    errors,
 
31
    )
 
32
from .decorators import (
33
33
    only_raises,
34
34
    )
35
35
 
36
36
 
37
 
# XXX: The tracking here of lock counts and whether the lock is held is
38
 
# somewhat redundant with what's done in LockDir; the main difference is that
39
 
# LockableFiles permits reentrancy.
40
 
 
41
 
class _LockWarner(object):
42
 
    """Hold a counter for a lock and warn if GCed while the count is >= 1.
43
 
 
44
 
    This is separate from LockableFiles because putting a __del__ on
45
 
    LockableFiles can result in uncollectable cycles.
46
 
    """
47
 
 
48
 
    def __init__(self, repr):
49
 
        self.lock_count = 0
50
 
        self.repr = repr
51
 
 
52
 
    def __del__(self):
53
 
        if self.lock_count >= 1:
54
 
            # There should have been a try/finally to unlock this.
55
 
            warnings.warn("%r was gc'd while locked" % self.repr)
56
 
 
57
 
 
58
37
class LockableFiles(object):
59
38
    """Object representing a set of related files locked within the same scope.
60
39
 
69
48
    This class is now deprecated; code should move to using the Transport
70
49
    directly for file operations and using the lock or CountedLock for
71
50
    locking.
72
 
    
 
51
 
73
52
    :ivar _lock: The real underlying lock (e.g. a LockDir)
74
 
    :ivar _counted_lock: A lock decorated with a semaphore, so that it 
75
 
        can be re-entered.
 
53
    :ivar _lock_count: If _lock_mode is true, a positive count of the number
 
54
        of times the lock has been taken (and not yet released) *by this
 
55
        process*, through this particular object instance.
 
56
    :ivar _lock_mode: None, or 'r' or 'w'
76
57
    """
77
58
 
78
 
    # _lock_mode: None, or 'r' or 'w'
79
 
 
80
 
    # _lock_count: If _lock_mode is true, a positive count of the number of
81
 
    # times the lock has been taken *by this process*.
82
 
 
83
59
    def __init__(self, transport, lock_name, lock_class):
84
60
        """Create a LockableFiles group
85
61
 
93
69
        self.lock_name = lock_name
94
70
        self._transaction = None
95
71
        self._lock_mode = None
96
 
        self._lock_warner = _LockWarner(repr(self))
 
72
        self._lock_count = 0
97
73
        self._find_modes()
98
74
        esc_name = self._escape(lock_name)
99
75
        self._lock = lock_class(transport, esc_name,
112
88
    def __repr__(self):
113
89
        return '%s(%r)' % (self.__class__.__name__,
114
90
                           self._transport)
 
91
 
115
92
    def __str__(self):
116
93
        return 'LockableFiles(%s, %s)' % (self.lock_name, self._transport.base)
117
94
 
124
101
 
125
102
    def _escape(self, file_or_path):
126
103
        """DEPRECATED: Do not use outside this class"""
127
 
        if not isinstance(file_or_path, basestring):
128
 
            file_or_path = '/'.join(file_or_path)
129
104
        if file_or_path == '':
130
105
            return u''
131
 
        return urlutils.escape(osutils.safe_unicode(file_or_path))
 
106
        return urlutils.escape(file_or_path)
132
107
 
133
108
    def _find_modes(self):
134
109
        """Determine the appropriate modes for files and directories.
141
116
        try:
142
117
            st = self._transport.stat('.')
143
118
        except errors.TransportNotPossible:
144
 
            self._dir_mode = 0755
145
 
            self._file_mode = 0644
 
119
            self._dir_mode = 0o755
 
120
            self._file_mode = 0o644
146
121
        else:
147
122
            # Check the directory mode, but also make sure the created
148
123
            # directories and files are read-write for this user. This is
149
124
            # mostly a workaround for filesystems which lie about being able to
150
125
            # write to a directory (cygwin & win32)
151
 
            self._dir_mode = (st.st_mode & 07777) | 00700
 
126
            self._dir_mode = (st.st_mode & 0o7777) | 0o0700
152
127
            # Remove the sticky and execute bits for files
153
 
            self._file_mode = self._dir_mode & ~07111
 
128
            self._file_mode = self._dir_mode & ~0o7111
154
129
 
155
130
    def leave_in_place(self):
156
131
        """Set this LockableFiles to not clear the physical lock on unlock."""
175
150
        some other way, and need to synchronise this object's state with that
176
151
        fact.
177
152
        """
178
 
        # TODO: Upgrade locking to support using a Transport,
179
 
        # and potentially a remote locking protocol
180
153
        if self._lock_mode:
181
 
            if self._lock_mode != 'w' or not self.get_transaction().writeable():
 
154
            if (self._lock_mode != 'w'
 
155
                    or not self.get_transaction().writeable()):
182
156
                raise errors.ReadOnlyError(self)
183
157
            self._lock.validate_token(token)
184
 
            self._lock_warner.lock_count += 1
 
158
            self._lock_count += 1
185
159
            return self._token_from_lock
186
160
        else:
187
161
            token_from_lock = self._lock.lock_write(token=token)
188
 
            #traceback.print_stack()
 
162
            # traceback.print_stack()
189
163
            self._lock_mode = 'w'
190
 
            self._lock_warner.lock_count = 1
 
164
            self._lock_count = 1
191
165
            self._set_write_transaction()
192
166
            self._token_from_lock = token_from_lock
193
167
            return token_from_lock
196
170
        if self._lock_mode:
197
171
            if self._lock_mode not in ('r', 'w'):
198
172
                raise ValueError("invalid lock mode %r" % (self._lock_mode,))
199
 
            self._lock_warner.lock_count += 1
 
173
            self._lock_count += 1
200
174
        else:
201
175
            self._lock.lock_read()
202
 
            #traceback.print_stack()
 
176
            # traceback.print_stack()
203
177
            self._lock_mode = 'r'
204
 
            self._lock_warner.lock_count = 1
 
178
            self._lock_count = 1
205
179
            self._set_read_transaction()
206
180
 
207
181
    def _set_read_transaction(self):
218
192
    def unlock(self):
219
193
        if not self._lock_mode:
220
194
            return lock.cant_unlock_not_held(self)
221
 
        if self._lock_warner.lock_count > 1:
222
 
            self._lock_warner.lock_count -= 1
 
195
        if self._lock_count > 1:
 
196
            self._lock_count -= 1
223
197
        else:
224
 
            #traceback.print_stack()
 
198
            # traceback.print_stack()
225
199
            self._finish_transaction()
226
200
            try:
227
201
                self._lock.unlock()
228
202
            finally:
229
 
                self._lock_mode = self._lock_warner.lock_count = None
230
 
 
231
 
    @property
232
 
    def _lock_count(self):
233
 
        return self._lock_warner.lock_count
 
203
                self._lock_count = 0
 
204
                self._lock_mode = None
234
205
 
235
206
    def is_locked(self):
236
207
        """Return true if this LockableFiles group is locked"""
237
 
        return self._lock_warner.lock_count >= 1
 
208
        return self._lock_count >= 1
238
209
 
239
210
    def get_physical_lock_status(self):
240
211
        """Return physical lock status.
287
258
    This is suitable for use only in WorkingTrees (which are at present
288
259
    always local).
289
260
    """
 
261
 
290
262
    def __init__(self, transport, escaped_name, file_modebits, dir_modebits):
291
263
        self._transport = transport
292
264
        self._escaped_name = escaped_name
320
292
    def create(self, mode=None):
321
293
        """Create lock mechanism"""
322
294
        # for old-style locks, create the file now
323
 
        self._transport.put_bytes(self._escaped_name, '',
324
 
                            mode=self._file_modebits)
 
295
        self._transport.put_bytes(self._escaped_name, b'',
 
296
                                  mode=self._file_modebits)
325
297
 
326
298
    def validate_token(self, token):
327
299
        if token is not None:
328
300
            raise errors.TokenLockingNotSupported(self)
329