~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lockable_files.py

  • Committer: Aaron Bentley
  • Date: 2009-03-24 15:47:32 UTC
  • mto: This revision was merged to the branch mainline in revision 4241.
  • Revision ID: aaron@aaronbentley.com-20090324154732-bwkvi4dx3o90a7dq
Add output, emit minimal inventory delta.

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
from cStringIO import StringIO
 
18
 
17
19
from bzrlib.lazy_import import lazy_import
18
20
lazy_import(globals(), """
19
21
import codecs
20
22
import warnings
21
23
 
22
24
from bzrlib import (
23
 
    counted_lock,
24
25
    errors,
25
 
    lock,
26
26
    osutils,
27
27
    transactions,
28
28
    urlutils,
30
30
""")
31
31
 
32
32
from bzrlib.decorators import (
33
 
    only_raises,
 
33
    needs_read_lock,
 
34
    needs_write_lock,
 
35
    )
 
36
from bzrlib.symbol_versioning import (
 
37
    deprecated_in,
 
38
    deprecated_method,
34
39
    )
35
40
 
36
41
 
58
63
class LockableFiles(object):
59
64
    """Object representing a set of related files locked within the same scope.
60
65
 
61
 
    This coordinates access to the lock along with providing a transaction.
 
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.
62
71
 
63
72
    LockableFiles manage a lock count and can be locked repeatedly by
64
73
    a single caller.  (The underlying lock implementation generally does not
66
75
 
67
76
    Instances of this class are often called control_files.
68
77
 
 
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
 
69
85
    This class is now deprecated; code should move to using the Transport
70
86
    directly for file operations and using the lock or CountedLock for
71
87
    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.
76
88
    """
77
89
 
78
90
    # _lock_mode: None, or 'r' or 'w'
99
111
        self._lock = lock_class(transport, esc_name,
100
112
                                file_modebits=self._file_mode,
101
113
                                dir_modebits=self._dir_mode)
102
 
        self._counted_lock = counted_lock.CountedLock(self._lock)
103
114
 
104
115
    def create_lock(self):
105
116
        """Create the lock.
133
144
    def _find_modes(self):
134
145
        """Determine the appropriate modes for files and directories.
135
146
 
136
 
        :deprecated: Replaced by BzrDir._find_creation_modes.
 
147
        :deprecated: Replaced by BzrDir._find_modes.
137
148
        """
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
141
149
        try:
142
150
            st = self._transport.stat('.')
143
151
        except errors.TransportNotPossible:
152
160
            # Remove the sticky and execute bits for files
153
161
            self._file_mode = self._dir_mode & ~07111
154
162
 
 
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
 
155
238
    def leave_in_place(self):
156
239
        """Set this LockableFiles to not clear the physical lock on unlock."""
157
240
        self._lock.leave_in_place()
214
297
        """Setup a write transaction."""
215
298
        self._set_transaction(transactions.WriteTransaction())
216
299
 
217
 
    @only_raises(errors.LockNotHeld, errors.LockBroken)
218
300
    def unlock(self):
219
301
        if not self._lock_mode:
220
 
            return lock.cant_unlock_not_held(self)
 
302
            raise errors.LockNotHeld(self)
221
303
        if self._lock_warner.lock_count > 1:
222
304
            self._lock_warner.lock_count -= 1
223
305
        else: