/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: John Arbash Meinel
  • Date: 2008-07-08 14:55:19 UTC
  • mfrom: (3530 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3532.
  • Revision ID: john@arbash-meinel.com-20080708145519-paqg4kjwbpgs2xmq
Merge bzr.dev 3530

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2008 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
17
17
from cStringIO import StringIO
18
18
import codecs
19
19
#import traceback
 
20
from warnings import warn
20
21
 
21
22
import bzrlib
22
23
from bzrlib.decorators import (needs_read_lock,
24
25
import bzrlib.errors as errors
25
26
from bzrlib.errors import BzrError
26
27
from bzrlib.osutils import file_iterator, safe_unicode
27
 
from bzrlib.symbol_versioning import (deprecated_method,
28
 
        )
 
28
from bzrlib.symbol_versioning import (
 
29
    deprecated_in,
 
30
    deprecated_method,
 
31
    )
29
32
from bzrlib.trace import mutter, note
30
33
import bzrlib.transactions as transactions
31
34
import bzrlib.urlutils as urlutils
56
59
    the object is constructed.  In older formats OSLocks are used everywhere.
57
60
    in newer formats a LockDir is used for Repositories and Branches, and 
58
61
    OSLocks for the local filesystem.
 
62
 
 
63
    This class is now deprecated; code should move to using the Transport 
 
64
    directly for file operations and using the lock or CountedLock for 
 
65
    locking.
59
66
    """
60
67
 
61
68
    # _lock_mode: None, or 'r' or 'w'
63
70
    # _lock_count: If _lock_mode is true, a positive count of the number of
64
71
    # times the lock has been taken *by this process*.   
65
72
    
66
 
    # If set to False (by a plugin, etc) BzrBranch will not set the
67
 
    # mode on created files or directories
68
 
    _set_file_mode = True
69
 
    _set_dir_mode = True
70
 
 
71
73
    def __init__(self, transport, lock_name, lock_class):
72
74
        """Create a LockableFiles group
73
75
 
104
106
 
105
107
    def __del__(self):
106
108
        if self.is_locked():
107
 
            # XXX: This should show something every time, and be suitable for
108
 
            # headless operation and embedding
109
 
            from warnings import warn
110
 
            warn("file group %r was not explicitly unlocked" % self)
111
 
            self._lock.unlock()
 
109
            # do not automatically unlock; there should have been a
 
110
            # try/finally to unlock this.
 
111
            warn("%r was gc'd while locked" % self)
112
112
 
113
113
    def break_lock(self):
114
114
        """Break the lock of this lockable files group if it is held.
125
125
        return urlutils.escape(safe_unicode(file_or_path))
126
126
 
127
127
    def _find_modes(self):
128
 
        """Determine the appropriate modes for files and directories."""
 
128
        """Determine the appropriate modes for files and directories.
 
129
        
 
130
        :deprecated: Replaced by BzrDir._find_modes.
 
131
        """
129
132
        try:
130
133
            st = self._transport.stat('.')
131
134
        except errors.TransportNotPossible:
139
142
            self._dir_mode = (st.st_mode & 07777) | 00700
140
143
            # Remove the sticky and execute bits for files
141
144
            self._file_mode = self._dir_mode & ~07111
142
 
        if not self._set_dir_mode:
143
 
            self._dir_mode = None
144
 
        if not self._set_file_mode:
145
 
            self._file_mode = None
146
145
 
 
146
    @deprecated_method(deprecated_in((1, 6, 0)))
147
147
    def controlfilename(self, file_or_path):
148
 
        """Return location relative to branch."""
 
148
        """Return location relative to branch.
 
149
        
 
150
        :deprecated: Use Transport methods instead.
 
151
        """
149
152
        return self._transport.abspath(self._escape(file_or_path))
150
153
 
151
154
    @needs_read_lock
 
155
    @deprecated_method(deprecated_in((1, 5, 0)))
152
156
    def get(self, relpath):
153
 
        """Get a file as a bytestream."""
 
157
        """Get a file as a bytestream.
 
158
        
 
159
        :deprecated: Use a Transport instead of LockableFiles.
 
160
        """
154
161
        relpath = self._escape(relpath)
155
162
        return self._transport.get(relpath)
156
163
 
157
164
    @needs_read_lock
 
165
    @deprecated_method(deprecated_in((1, 5, 0)))
158
166
    def get_utf8(self, relpath):
159
 
        """Get a file as a unicode stream."""
 
167
        """Get a file as a unicode stream.
 
168
        
 
169
        :deprecated: Use a Transport instead of LockableFiles.
 
170
        """
160
171
        relpath = self._escape(relpath)
161
172
        # DO NOT introduce an errors=replace here.
162
173
        return codecs.getreader('utf-8')(self._transport.get(relpath))
163
174
 
164
175
    @needs_write_lock
 
176
    @deprecated_method(deprecated_in((1, 6, 0)))
165
177
    def put(self, path, file):
166
178
        """Write a file.
167
179
        
168
180
        :param path: The path to put the file, relative to the .bzr control
169
181
                     directory
170
 
        :param f: A file-like or string object whose contents should be copied.
 
182
        :param file: A file-like or string object whose contents should be copied.
 
183
 
 
184
        :deprecated: Use Transport methods instead.
171
185
        """
172
186
        self._transport.put_file(self._escape(path), file, mode=self._file_mode)
173
187
 
174
188
    @needs_write_lock
 
189
    @deprecated_method(deprecated_in((1, 6, 0)))
175
190
    def put_bytes(self, path, a_string):
176
191
        """Write a string of bytes.
177
192
 
178
193
        :param path: The path to put the bytes, relative to the transport root.
179
 
        :param string: A string object, whose exact bytes are to be copied.
 
194
        :param a_string: A string object, whose exact bytes are to be copied.
 
195
 
 
196
        :deprecated: Use Transport methods instead.
180
197
        """
181
198
        self._transport.put_bytes(self._escape(path), a_string,
182
199
                                  mode=self._file_mode)
183
200
 
184
201
    @needs_write_lock
 
202
    @deprecated_method(deprecated_in((1, 6, 0)))
185
203
    def put_utf8(self, path, a_string):
186
204
        """Write a string, encoding as utf-8.
187
205
 
188
206
        :param path: The path to put the string, relative to the transport root.
189
207
        :param string: A string or unicode object whose contents should be copied.
 
208
 
 
209
        :deprecated: Use Transport methods instead.
190
210
        """
191
211
        # IterableFile would not be needed if Transport.put took iterables
192
212
        # instead of files.  ADHB 2005-12-25
243
263
    def lock_read(self):
244
264
        # mutter("lock read: %s (%s)", self, self._lock_count)
245
265
        if self._lock_mode:
246
 
            assert self._lock_mode in ('r', 'w'), \
247
 
                   "invalid lock mode %r" % self._lock_mode
 
266
            if self._lock_mode not in ('r', 'w'):
 
267
                raise ValueError("invalid lock mode %r" % (self._lock_mode,))
248
268
            self._lock_count += 1
249
269
        else:
250
270
            self._lock.lock_read()