26
28
from cStringIO import StringIO
31
31
from bzrlib.errors import BzrError, BzrCheckError
32
from bzrlib.branch import Branch, LocalBranch, BZR_BRANCH_FORMAT_5
32
from bzrlib.branch import Branch, BZR_BRANCH_FORMAT_5
33
33
from bzrlib.trace import mutter
34
from bzrlib.weavestore import WeaveStore
35
34
from bzrlib.xml5 import serializer_v5
38
36
# velocitynet.com.au transparently proxies connections and thereby
39
37
# breaks keep-alive -- sucks!
100
98
except urllib2.URLError:
103
scheme, host, path = list(urlparse.urlparse(url))[:3]
104
# discard params, query, fragment
106
# strip off one component of the path component
107
idx = path.rfind('/')
108
if idx == -1 or path == '/':
109
raise BzrError('no branch root found for URL %s'
110
' or enclosing directories'
113
url = urlparse.urlunparse((scheme, host, path, '', '', ''))
117
class RemoteBranch(LocalBranch):
102
idx = url.rindex('/')
104
raise BzrError('no branch root found for URL %s' % orig_url)
110
class RemoteBranch(Branch):
118
111
def __init__(self, baseurl, find_root=True):
119
112
"""Create new proxy for a remote branch."""
121
self.base = _find_remote_root(baseurl)
114
self.baseurl = _find_remote_root(baseurl)
124
self._check_format(False)
125
# is guaranteed to be a v5 store
116
self.baseurl = baseurl
127
cfn = self.controlfilename
128
assert self._branch_format == 5
129
self.control_weaves = WeaveStore(cfn([]), get_url)
130
self.weave_store = WeaveStore(cfn('weaves'), get_url)
131
self.revision_store = RemoteStore(cfn('revision-store'))
119
self.inventory_store = RemoteStore(baseurl + '/.bzr/inventory-store/')
120
self.text_store = RemoteStore(baseurl + '/.bzr/text-store/')
121
self.revision_store = RemoteStore(baseurl + '/.bzr/revision-store/')
133
123
def __str__(self):
134
124
b = getattr(self, 'baseurl', 'undefined')
137
127
__repr__ = __str__
139
def setup_caching(self, cache_root):
140
"""Set up cached stores located under cache_root"""
141
from bzrlib.meta_store import CachedStore
142
for store_name in ('inventory_store', 'text_store', 'revision_store'):
143
if not isinstance(getattr(self, store_name), CachedStore):
144
cache_path = os.path.join(cache_root, store_name)
146
new_store = CachedStore(getattr(self, store_name), cache_path)
147
setattr(self, store_name, new_store)
149
129
def controlfile(self, filename, mode):
150
130
if mode not in ('rb', 'rt', 'r'):
151
131
raise BzrError("file mode %r not supported for remote branches" % mode)
152
return get_url(self.base + '/.bzr/' + filename, False)
132
return get_url(self.baseurl + '/.bzr/' + filename, False)
155
135
def lock_read(self):
159
139
def lock_write(self):
160
140
from errors import LockError
161
141
raise LockError("write lock not supported for remote branch %s"
164
144
def unlock(self):
168
148
def relpath(self, path):
169
if not path.startswith(self.base):
149
if not path.startswith(self.baseurl):
170
150
raise BzrError('path %r is not under base URL %r'
151
% (path, self.baseurl))
152
pl = len(self.baseurl)
173
153
return path[pl:].lstrip('/')
176
156
def get_revision(self, revision_id):
157
from bzrlib.revision import Revision
178
159
revf = self.revision_store[revision_id]
195
176
raise ValueError('invalid store id', name)
196
177
return self._baseurl + '/' + name
198
def __contains__(self, fileid):
205
179
def __getitem__(self, fileid):
206
180
p = self._path(fileid)
208
182
return get_url(p, compressed=True)
209
except urllib2.URLError:
212
return get_url(p, compressed=False)
213
except urllib2.URLError:
214
184
raise KeyError(fileid)