~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lockable_files.py

  • Committer: Martin Pool
  • Date: 2006-03-03 06:07:11 UTC
  • mto: This revision was merged to the branch mainline in revision 1593.
  • Revision ID: mbp@sourcefrog.net-20060303060711-51b8200efda1847b
Get LockableFiles tests running against LockDir

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
 
47
47
    Instances of this class are often called control_files.
48
48
    
49
 
    This object builds on top of a Transport, which is used to actually 
50
 
    write the files to disk, and a Lock or LockDir, which controls how 
51
 
    access to the files is controlled.  The particular type of locking used
52
 
    is set when the object is constructed.
53
 
 
54
 
    This class 
55
 
 
56
 
    _lock_mode
57
 
        None, or 'r' or 'w'
58
 
 
59
 
    _lock_count
60
 
        If _lock_mode is true, a positive count of the number of times the
61
 
        lock has been taken *by this process*.  Others may have compatible 
62
 
        read locks.
63
 
 
64
 
    _lock
65
 
        Lock object from bzrlib.lock.
 
49
    This object builds on top of a Transport, which is used to actually write
 
50
    the files to disk, and an OSLock or LockDir, which controls how access to
 
51
    the files is controlled.  The particular type of locking used is set when
 
52
    the object is constructed.  In older formats OSLocks are used everywhere.
 
53
    in newer formats a LockDir is used for Repositories and Branches, and 
 
54
    OSLocks for the local filesystem.
66
55
    """
67
56
 
68
 
    _lock_mode = None
 
57
    _lock_mode = None               # None, or 'r' or 'w'
 
58
 
 
59
    # If _lock_mode is true, a positive count of the number of times the
 
60
    # lock has been taken *by this process*.  Others may have compatible 
 
61
    # read locks.
69
62
    _lock_count = None
70
 
    _lock = None
 
63
 
71
64
    # If set to False (by a plugin, etc) BzrBranch will not set the
72
65
    # mode on created files or directories
73
66
    _set_file_mode = True
74
67
    _set_dir_mode = True
75
68
 
76
 
    def __init__(self, transport, lock_name):
 
69
    def __init__(self, transport, lock_name, lock_strategy_class=None):
 
70
        """Create a LockableFiles group
 
71
 
 
72
        :param transport: Transport pointing to the directory holding the 
 
73
            control files and lock.
 
74
        :param lock_name: Name of the lock guarding these files.
 
75
        :param lock_strategy_class: Class of lock strategy to use.
 
76
        """
77
77
        object.__init__(self)
78
78
        self._transport = transport
79
79
        self.lock_name = lock_name
80
80
        self._transaction = None
81
81
        self._find_modes()
82
 
        self._lock_strategy = OldTransportLockStrategy(transport,
83
 
                self._escape(lock_name))
84
 
 
85
 
    def __del__(self):
86
 
        if self._lock_mode or self._lock:
87
 
            # XXX: This should show something every time, and be suitable for
88
 
            # headless operation and embedding
89
 
            from warnings import warn
90
 
            warn("file group %r was not explicitly unlocked" % self)
91
 
            self._lock.unlock()
 
82
        # TODO: remove this and make the parameter mandatory
 
83
        if lock_strategy_class is None:
 
84
            lock_strategy_class = OldTransportLockStrategy
 
85
        esc_name = self._escape(lock_name)
 
86
        self._lock_strategy = lock_strategy_class(transport, esc_name)
92
87
 
93
88
    def _escape(self, file_or_path):
94
89
        if not isinstance(file_or_path, basestring):
132
127
        """
133
128
 
134
129
        relpath = self._escape(file_or_path)
135
 
        #TODO: codecs.open() buffers linewise, so it was overloaded with
 
130
        # TODO: codecs.open() buffers linewise, so it was overloaded with
136
131
        # a much larger buffer, do we need to do the same for getreader/getwriter?
137
132
        if mode == 'rb': 
138
133
            return self.get(relpath)
286
281
    This is turned on by newer storage formats which want to use
287
282
    LockDirs.  This only guards writes, not reads.
288
283
    """
 
284
    # TODO: perhaps we should just refer to the LockDir directly rather than
 
285
    # using this proxy; it would require adapting some of the method names
 
286
    
289
287
    def __init__(self, transport, escaped_name):
290
288
        from bzrlib.lockdir import LockDir
291
289
        self._lockdir = LockDir(transport, escaped_name)
295
293
        self._lockdir.attempt_lock()
296
294
 
297
295
    def lock_read(self):
298
 
        """No locking is done for reads in this format"""
299
 
        pass
 
296
        """Lock for read.
 
297
 
 
298
        LockDir always takes exclusive locks, even when a shared read 
 
299
        lock is requested.
 
300
        """
 
301
        self.lock_write()
300
302
 
301
303
    def unlock(self):
302
304
        self._lockdir.unlock()