Rev 10: Switch away from tracking stuff as a versioned file. in http://bzr.arbash-meinel.com/plugins/file_locking
John Arbash Meinel
john at arbash-meinel.com
Mon Sep 21 16:11:39 BST 2009
At http://bzr.arbash-meinel.com/plugins/file_locking
------------------------------------------------------------
revno: 10
revision-id: john at arbash-meinel.com-20090921151131-jnh3yhispypxjfx9
parent: john at arbash-meinel.com-20090918210025-kwzl3mh6r6oxlvcq
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: file_locking
timestamp: Mon 2009-09-21 10:11:31 -0500
message:
Switch away from tracking stuff as a versioned file.
I would like things to propagate, but this allows us to store everything
on the server, and get more 'live' updates.
-------------- next part --------------
=== modified file 'file_lock.py'
--- a/file_lock.py 2009-09-18 21:00:25 +0000
+++ b/file_lock.py 2009-09-21 15:11:31 +0000
@@ -136,18 +136,10 @@
'Expected: %s'
'Found: %s' % (expected_header, bytes))
lines = bytes.splitlines()
- if len(lines) < 2 or not lines[1].startswith('url: '):
- raise ValueError('Invalid content in: %s\n' % (bytes,))
- url = lines[1][len('url: '):]
- if not url:
- raise ValueError('Invalid url: %r' % (url,))
- return FileLockingInfo(url, lines[2:])
+ return FileLockingInfo(lines[1:])
def to_bytes(self, locking_info):
content = [self._HEADER, '\n']
- content.append('url: ')
- content.append(locking_info._lock_store_url)
- content.append('\n')
for tracked_id in sorted(locking_info._tracked_ids):
content.append(tracked_id)
content.append('\n')
@@ -161,17 +153,15 @@
:ivar _tracked_ids: The actual locations where content is tracked
"""
- _TREE_PATH = '.bzr-file-locking'
- _FILE_ID = 'bzr-file-locking-file-id'
-
- def __init__(self, url, tracked_ids):
- self._lock_store_url = url
+ def __init__(self, tracked_ids):
self._tracked_ids = set(tracked_ids)
class FileLockManager(object):
"""This is the overall manager that tracks everything we need to know."""
+ _CONFIG_ENTRY = 'bzr-file-locking-url'
+
def __init__(self, wt):
self._wt = wt
self._locking_info = None
@@ -183,22 +173,11 @@
self._wt.lock_write()
try:
s = FileLockingInfoSerializer()
- # Check and see if .bzr-file-locking has been added
- relpath = FileLockingInfo._TREE_PATH
- fid = FileLockingInfo._FILE_ID
- try:
- bytes = self._wt.get_file_text(fid, relpath)
- except (IOError, errors.NoSuchFile), e:
- locking_info = FileLockingInfo(url, [])
- else:
- locking_info = s.from_bytes(bytes)
- raise ValueError('lock store already initialized at: %s'
- % (locking_info._lock_store_url,))
- # Would we want to use a fixed file-id for this file? It actually
- # sort of makes sense...
- self._wt.add(['.bzr-file-locking'], [fid], ['file'])
- bytes = s.to_bytes(locking_info)
- self._wt.put_file_bytes_non_atomic(fid, bytes)
+ # Check and see if bzr-file-locking has been added
+ conf = self._wt.branch.get_config()
+ if conf.get_user_option(self._CONFIG_ENTRY) is not None:
+ raise ValueError('lock store already set.')
+ conf.set_user_option(self._CONFIG_ENTRY, url)
finally:
self._wt.unlock()
=== modified file 'tests/test_file_lock.py'
--- a/tests/test_file_lock.py 2009-09-18 21:00:25 +0000
+++ b/tests/test_file_lock.py 2009-09-21 15:11:31 +0000
@@ -21,7 +21,7 @@
from bzrlib.plugins.file_locking import file_lock
-class TestFileLockingSerializer(tests.TestCase):
+class TestFileLockingInfoSerializer(tests.TestCase):
def test_bad_data(self):
s = file_lock.FileLockingInfoSerializer()
@@ -30,41 +30,31 @@
self.assertRaises(ValueError, s.from_bytes,
'%s2\n' % s._HEADER)
self.assertRaises(ValueError, s.from_bytes, '')
- # No 'url: ' line, or invalid url
- self.assertRaises(ValueError, s.from_bytes, '%s\n' % s._HEADER)
- self.assertRaises(ValueError, s.from_bytes, '%s\nbogus\n' % s._HEADER)
- self.assertRaises(ValueError, s.from_bytes, '%s\nurl: \n' % s._HEADER)
def test_from_bytes(self):
s = file_lock.FileLockingInfoSerializer()
- li = s.from_bytes('%s\nurl: http://example.com/test/url\n' % s._HEADER)
- self.assertEqual('http://example.com/test/url', li._lock_store_url)
+ li = s.from_bytes('%s\n' % s._HEADER)
self.assertEqual(set(), li._tracked_ids)
def test_from_bytes_with_ids(self):
s = file_lock.FileLockingInfoSerializer()
- li = s.from_bytes('%s\nurl: http://example.com/test/url\n'
+ li = s.from_bytes('%s\n'
'file-id-1\n'
'dir-id-2\n'
% s._HEADER)
- self.assertEqual('http://example.com/test/url', li._lock_store_url)
self.assertEqual(set(['file-id-1', 'dir-id-2']), li._tracked_ids)
def test_to_bytes(self):
- li = file_lock.FileLockingInfo('http://example.com/foo',
- ['file-id-1', 'file-id-2'])
+ li = file_lock.FileLockingInfo(['file-id-1', 'file-id-2'])
s = file_lock.FileLockingInfoSerializer()
self.assertEqualDiff('%s\n'
- 'url: http://example.com/foo\n'
'file-id-1\n'
'file-id-2\n' % s._HEADER, s.to_bytes(li))
def test_to_bytes_is_sorted(self):
- li = file_lock.FileLockingInfo('http://example.com/foo',
- ['file-id-1', 'file-id-2', 'dir-id-3'])
+ li = file_lock.FileLockingInfo(['file-id-1', 'file-id-2', 'dir-id-3'])
s = file_lock.FileLockingInfoSerializer()
self.assertEqualDiff('%s\n'
- 'url: http://example.com/foo\n'
'dir-id-3\n'
'file-id-1\n'
'file-id-2\n' % s._HEADER, s.to_bytes(li))
@@ -132,11 +122,9 @@
def test_initialize(self):
tree, manager = self.make_tree_and_manager('.')
manager.initialize_lock_store('.')
- self.assertEqual(file_lock.FileLockingInfo._FILE_ID,
- tree.path2id(file_lock.FileLockingInfo._TREE_PATH))
- content = tree.get_file_text(file_lock.FileLockingInfo._FILE_ID)
- self.assertEqualDiff('# bzr-file-locking locking info v1\n'
- 'url: .\n', content)
+ conf = tree.branch.get_config()
+ self.assertEqual('.',
+ conf.get_user_option(file_lock.FileLockManager._CONFIG_ENTRY))
def test_is_tracked_no_tracking(self):
tree, manager = self.make_tree_and_manager('.')
More information about the bazaar-commits
mailing list