/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 bzrlib/lockable_files.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2007-04-17 08:04:15 UTC
  • mfrom: (2423.1.2 benchmarks)
  • Revision ID: pqm@pqm.ubuntu.com-20070417080415-5vn25svmf95ki88z
Also clear SmartTCPServer hooks from TestCase._clear_hooks

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2005, 2006 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
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
 
 
7
#
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
12
 
 
 
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
19
#import traceback
20
20
 
21
21
import bzrlib
22
 
from bzrlib.decorators import *
 
22
from bzrlib.decorators import (needs_read_lock,
 
23
        needs_write_lock)
23
24
import bzrlib.errors as errors
24
 
from bzrlib.errors import LockError, ReadOnlyError
 
25
from bzrlib.errors import BzrError
25
26
from bzrlib.osutils import file_iterator, safe_unicode
26
 
from bzrlib.symbol_versioning import *
 
27
from bzrlib.symbol_versioning import (deprecated_method, 
 
28
        zero_eight)
27
29
from bzrlib.trace import mutter, note
28
30
import bzrlib.transactions as transactions
 
31
import bzrlib.urlutils as urlutils
 
32
 
29
33
 
30
34
# XXX: The tracking here of lock counts and whether the lock is held is
31
35
# somewhat redundant with what's done in LockDir; the main difference is that
73
77
        :param lock_class: Class of lock strategy to use: typically
74
78
            either LockDir or TransportLock.
75
79
        """
76
 
        object.__init__(self)
77
80
        self._transport = transport
78
81
        self.lock_name = lock_name
79
82
        self._transaction = None
80
 
        self._find_modes()
81
83
        self._lock_mode = None
82
84
        self._lock_count = 0
 
85
        self._find_modes()
83
86
        esc_name = self._escape(lock_name)
84
87
        self._lock = lock_class(transport, esc_name,
85
88
                                file_modebits=self._file_mode,
91
94
        This should normally be called only when the LockableFiles directory
92
95
        is first created on disk.
93
96
        """
94
 
        self._lock.create()
 
97
        self._lock.create(mode=self._dir_mode)
95
98
 
96
99
    def __repr__(self):
97
100
        return '%s(%r)' % (self.__class__.__name__,
107
110
            warn("file group %r was not explicitly unlocked" % self)
108
111
            self._lock.unlock()
109
112
 
 
113
    def break_lock(self):
 
114
        """Break the lock of this lockable files group if it is held.
 
115
 
 
116
        The current ui factory will be used to prompt for user conformation.
 
117
        """
 
118
        self._lock.break_lock()
 
119
 
110
120
    def _escape(self, file_or_path):
111
121
        if not isinstance(file_or_path, basestring):
112
122
            file_or_path = '/'.join(file_or_path)
113
123
        if file_or_path == '':
114
124
            return u''
115
 
        return bzrlib.transport.urlescape(safe_unicode(file_or_path))
 
125
        return urlutils.escape(safe_unicode(file_or_path))
116
126
 
117
127
    def _find_modes(self):
118
128
        """Determine the appropriate modes for files and directories."""
183
193
                     directory
184
194
        :param f: A file-like or string object whose contents should be copied.
185
195
        """
186
 
        self._transport.put(self._escape(path), file, mode=self._file_mode)
 
196
        self._transport.put_file(self._escape(path), file, mode=self._file_mode)
 
197
 
 
198
    @needs_write_lock
 
199
    def put_bytes(self, path, a_string):
 
200
        """Write a string of bytes.
 
201
 
 
202
        :param path: The path to put the bytes, relative to the transport root.
 
203
        :param string: A string object, whose exact bytes are to be copied.
 
204
        """
 
205
        self._transport.put_bytes(self._escape(path), a_string,
 
206
                                  mode=self._file_mode)
187
207
 
188
208
    @needs_write_lock
189
209
    def put_utf8(self, path, a_string):
190
210
        """Write a string, encoding as utf-8.
191
211
 
192
212
        :param path: The path to put the string, relative to the transport root.
193
 
        :param string: A file-like or string object whose contents should be copied.
 
213
        :param string: A string or unicode object whose contents should be copied.
194
214
        """
195
215
        # IterableFile would not be needed if Transport.put took iterables
196
216
        # instead of files.  ADHB 2005-12-25
200
220
        # these are valuable files which should have exact contents.
201
221
        if not isinstance(a_string, basestring):
202
222
            raise errors.BzrBadParameterNotString(a_string)
203
 
        self.put(path, StringIO(a_string.encode('utf-8')))
204
 
 
205
 
    def lock_write(self):
 
223
        self.put_bytes(path, a_string.encode('utf-8'))
 
224
 
 
225
    def leave_in_place(self):
 
226
        """Set this LockableFiles to not clear the physical lock on unlock."""
 
227
        self._lock.leave_in_place()
 
228
 
 
229
    def dont_leave_in_place(self):
 
230
        """Set this LockableFiles to clear the physical lock on unlock."""
 
231
        self._lock.dont_leave_in_place()
 
232
 
 
233
    def lock_write(self, token=None):
 
234
        """Lock this group of files for writing.
 
235
        
 
236
        :param token: if this is already locked, then lock_write will fail
 
237
            unless the token matches the existing lock.
 
238
        :returns: a token if this instance supports tokens, otherwise None.
 
239
        :raises TokenLockingNotSupported: when a token is given but this
 
240
            instance doesn't support using token locks.
 
241
        :raises MismatchedToken: if the specified token doesn't match the token
 
242
            of the existing lock.
 
243
 
 
244
        A token should be passed in if you know that you have locked the object
 
245
        some other way, and need to synchronise this object's state with that
 
246
        fact.
 
247
        """
206
248
        # mutter("lock write: %s (%s)", self, self._lock_count)
207
249
        # TODO: Upgrade locking to support using a Transport,
208
250
        # and potentially a remote locking protocol
209
251
        if self._lock_mode:
210
252
            if self._lock_mode != 'w' or not self.get_transaction().writeable():
211
 
                raise ReadOnlyError(self)
 
253
                raise errors.ReadOnlyError(self)
 
254
            self._lock.validate_token(token)
212
255
            self._lock_count += 1
 
256
            return self._token_from_lock
213
257
        else:
214
 
            self._lock.lock_write()
 
258
            token_from_lock = self._lock.lock_write(token=token)
215
259
            #note('write locking %s', self)
216
260
            #traceback.print_stack()
217
261
            self._lock_mode = 'w'
218
262
            self._lock_count = 1
219
263
            self._set_transaction(transactions.WriteTransaction())
 
264
            self._token_from_lock = token_from_lock
 
265
            return token_from_lock
220
266
 
221
267
    def lock_read(self):
222
268
        # mutter("lock read: %s (%s)", self, self._lock_count)
244
290
            #note('unlocking %s', self)
245
291
            #traceback.print_stack()
246
292
            self._finish_transaction()
247
 
            self._lock.unlock()
248
 
            self._lock_mode = self._lock_count = None
 
293
            try:
 
294
                self._lock.unlock()
 
295
            finally:
 
296
                self._lock_mode = self._lock_count = None
249
297
 
250
298
    def is_locked(self):
251
299
        """Return true if this LockableFiles group is locked"""
252
300
        return self._lock_count >= 1
253
301
 
 
302
    def get_physical_lock_status(self):
 
303
        """Return physical lock status.
 
304
        
 
305
        Returns true if a lock is held on the transport. If no lock is held, or
 
306
        the underlying locking mechanism does not support querying lock
 
307
        status, false is returned.
 
308
        """
 
309
        try:
 
310
            return self._lock.peek() is not None
 
311
        except NotImplementedError:
 
312
            return False
 
313
 
254
314
    def get_transaction(self):
255
315
        """Return the current active transaction.
256
316
 
296
356
        self._file_modebits = file_modebits
297
357
        self._dir_modebits = dir_modebits
298
358
 
299
 
    def lock_write(self):
 
359
    def break_lock(self):
 
360
        raise NotImplementedError(self.break_lock)
 
361
 
 
362
    def leave_in_place(self):
 
363
        raise NotImplementedError(self.leave_in_place)
 
364
 
 
365
    def dont_leave_in_place(self):
 
366
        raise NotImplementedError(self.dont_leave_in_place)
 
367
 
 
368
    def lock_write(self, token=None):
 
369
        if token is not None:
 
370
            raise errors.TokenLockingNotSupported(self)
300
371
        self._lock = self._transport.lock_write(self._escaped_name)
301
372
 
302
373
    def lock_read(self):
306
377
        self._lock.unlock()
307
378
        self._lock = None
308
379
 
309
 
    def create(self):
 
380
    def peek(self):
 
381
        raise NotImplementedError()
 
382
 
 
383
    def create(self, mode=None):
310
384
        """Create lock mechanism"""
311
385
        # for old-style locks, create the file now
312
 
        self._transport.put(self._escaped_name, StringIO(), 
 
386
        self._transport.put_bytes(self._escaped_name, '',
313
387
                            mode=self._file_modebits)
 
388
 
 
389
    def validate_token(self, token):
 
390
        if token is not None:
 
391
            raise errors.TokenLockingNotSupported(self)
 
392