/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: Robert Collins
  • Date: 2010-05-06 11:08:10 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506110810-h3j07fh5gmw54s25
Cleaner matcher matching revised unlocking protocol.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2008 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2008, 2009 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
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
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 
 
17
 
from cStringIO import StringIO
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
16
 
19
17
from bzrlib.lazy_import import lazy_import
20
18
lazy_import(globals(), """
22
20
import warnings
23
21
 
24
22
from bzrlib import (
 
23
    counted_lock,
25
24
    errors,
 
25
    lock,
26
26
    osutils,
27
27
    transactions,
28
28
    urlutils,
30
30
""")
31
31
 
32
32
from bzrlib.decorators import (
33
 
    needs_read_lock,
34
 
    needs_write_lock,
35
 
    )
36
 
from bzrlib.symbol_versioning import (
37
 
    deprecated_in,
38
 
    deprecated_method,
 
33
    only_raises,
39
34
    )
40
35
 
41
36
 
63
58
class LockableFiles(object):
64
59
    """Object representing a set of related files locked within the same scope.
65
60
 
66
 
    These files are used by a WorkingTree, Repository or Branch, and should
67
 
    generally only be touched by that object.
68
 
 
69
 
    LockableFiles also provides some policy on top of Transport for encoding
70
 
    control files as utf-8.
 
61
    This coordinates access to the lock along with providing a transaction.
71
62
 
72
63
    LockableFiles manage a lock count and can be locked repeatedly by
73
64
    a single caller.  (The underlying lock implementation generally does not
75
66
 
76
67
    Instances of this class are often called control_files.
77
68
 
78
 
    This object builds on top of a Transport, which is used to actually write
79
 
    the files to disk, and an OSLock or LockDir, which controls how access to
80
 
    the files is controlled.  The particular type of locking used is set when
81
 
    the object is constructed.  In older formats OSLocks are used everywhere.
82
 
    in newer formats a LockDir is used for Repositories and Branches, and
83
 
    OSLocks for the local filesystem.
84
 
 
85
69
    This class is now deprecated; code should move to using the Transport
86
70
    directly for file operations and using the lock or CountedLock for
87
71
    locking.
 
72
    
 
73
    :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.
88
76
    """
89
77
 
90
78
    # _lock_mode: None, or 'r' or 'w'
111
99
        self._lock = lock_class(transport, esc_name,
112
100
                                file_modebits=self._file_mode,
113
101
                                dir_modebits=self._dir_mode)
 
102
        self._counted_lock = counted_lock.CountedLock(self._lock)
114
103
 
115
104
    def create_lock(self):
116
105
        """Create the lock.
144
133
    def _find_modes(self):
145
134
        """Determine the appropriate modes for files and directories.
146
135
 
147
 
        :deprecated: Replaced by BzrDir._find_modes.
 
136
        :deprecated: Replaced by BzrDir._find_creation_modes.
148
137
        """
 
138
        # XXX: The properties created by this can be removed or deprecated
 
139
        # once all the _get_text_store methods etc no longer use them.
 
140
        # -- mbp 20080512
149
141
        try:
150
142
            st = self._transport.stat('.')
151
143
        except errors.TransportNotPossible:
160
152
            # Remove the sticky and execute bits for files
161
153
            self._file_mode = self._dir_mode & ~07111
162
154
 
163
 
    @deprecated_method(deprecated_in((1, 6, 0)))
164
 
    def controlfilename(self, file_or_path):
165
 
        """Return location relative to branch.
166
 
 
167
 
        :deprecated: Use Transport methods instead.
168
 
        """
169
 
        return self._transport.abspath(self._escape(file_or_path))
170
 
 
171
 
    @needs_read_lock
172
 
    @deprecated_method(deprecated_in((1, 5, 0)))
173
 
    def get(self, relpath):
174
 
        """Get a file as a bytestream.
175
 
 
176
 
        :deprecated: Use a Transport instead of LockableFiles.
177
 
        """
178
 
        relpath = self._escape(relpath)
179
 
        return self._transport.get(relpath)
180
 
 
181
 
    @needs_read_lock
182
 
    @deprecated_method(deprecated_in((1, 5, 0)))
183
 
    def get_utf8(self, relpath):
184
 
        """Get a file as a unicode stream.
185
 
 
186
 
        :deprecated: Use a Transport instead of LockableFiles.
187
 
        """
188
 
        relpath = self._escape(relpath)
189
 
        # DO NOT introduce an errors=replace here.
190
 
        return codecs.getreader('utf-8')(self._transport.get(relpath))
191
 
 
192
 
    @needs_write_lock
193
 
    @deprecated_method(deprecated_in((1, 6, 0)))
194
 
    def put(self, path, file):
195
 
        """Write a file.
196
 
 
197
 
        :param path: The path to put the file, relative to the .bzr control
198
 
                     directory
199
 
        :param file: A file-like or string object whose contents should be copied.
200
 
 
201
 
        :deprecated: Use Transport methods instead.
202
 
        """
203
 
        self._transport.put_file(self._escape(path), file, mode=self._file_mode)
204
 
 
205
 
    @needs_write_lock
206
 
    @deprecated_method(deprecated_in((1, 6, 0)))
207
 
    def put_bytes(self, path, a_string):
208
 
        """Write a string of bytes.
209
 
 
210
 
        :param path: The path to put the bytes, relative to the transport root.
211
 
        :param a_string: A string object, whose exact bytes are to be copied.
212
 
 
213
 
        :deprecated: Use Transport methods instead.
214
 
        """
215
 
        self._transport.put_bytes(self._escape(path), a_string,
216
 
                                  mode=self._file_mode)
217
 
 
218
 
    @needs_write_lock
219
 
    @deprecated_method(deprecated_in((1, 6, 0)))
220
 
    def put_utf8(self, path, a_string):
221
 
        """Write a string, encoding as utf-8.
222
 
 
223
 
        :param path: The path to put the string, relative to the transport root.
224
 
        :param string: A string or unicode object whose contents should be copied.
225
 
 
226
 
        :deprecated: Use Transport methods instead.
227
 
        """
228
 
        # IterableFile would not be needed if Transport.put took iterables
229
 
        # instead of files.  ADHB 2005-12-25
230
 
        # RBC 20060103 surely its not needed anyway, with codecs transcode
231
 
        # file support ?
232
 
        # JAM 20060103 We definitely don't want encode(..., 'replace')
233
 
        # these are valuable files which should have exact contents.
234
 
        if not isinstance(a_string, basestring):
235
 
            raise errors.BzrBadParameterNotString(a_string)
236
 
        self.put_bytes(path, a_string.encode('utf-8'))
237
 
 
238
155
    def leave_in_place(self):
239
156
        """Set this LockableFiles to not clear the physical lock on unlock."""
240
157
        self._lock.leave_in_place()
297
214
        """Setup a write transaction."""
298
215
        self._set_transaction(transactions.WriteTransaction())
299
216
 
 
217
    @only_raises(errors.LockNotHeld, errors.LockBroken)
300
218
    def unlock(self):
301
219
        if not self._lock_mode:
302
 
            raise errors.LockNotHeld(self)
 
220
            return lock.cant_unlock_not_held(self)
303
221
        if self._lock_warner.lock_count > 1:
304
222
            self._lock_warner.lock_count -= 1
305
223
        else: