28
26
from cStringIO import StringIO
31
from errors import BzrError, BzrCheckError
32
from branch import Branch, BZR_BRANCH_FORMAT
33
from trace import mutter
35
# velocitynet.com.au transparently proxies connections and thereby
36
# breaks keep-alive -- sucks!
39
ENABLE_URLGRABBER = True
31
from bzrlib.errors import BzrError, BzrCheckError
32
from bzrlib.branch import Branch, LocalBranch, BZR_BRANCH_FORMAT
33
from bzrlib.trace import mutter
34
from bzrlib.xml import serializer_v4
37
ENABLE_URLGRABBER = False
39
from bzrlib.errors import BzrError, NoSuchRevision
41
class GetFailed(BzrError):
42
def __init__(self, url, status):
43
BzrError.__init__(self, "Get %s failed with status %s" % (url, status))
42
47
if ENABLE_URLGRABBER:
44
import urlgrabber.keepalive
45
urlgrabber.keepalive.DEBUG = 0
48
import util.urlgrabber
49
import util.urlgrabber.keepalive
50
util.urlgrabber.keepalive.DEBUG = 0
46
51
def get_url(path, compressed=False):
51
56
mutter("grab url %s" % url)
52
url_f = urlgrabber.urlopen(url, keepalive=1, close_connection=0)
57
url_f = util.urlgrabber.urlopen(url, keepalive=1, close_connection=0)
58
if url_f.status != 200:
59
raise GetFailed(url, url_f.status)
89
96
except urllib2.URLError:
95
raise BzrError('no branch root found for URL %s' % orig_url)
101
class RemoteBranch(Branch):
99
scheme, host, path = list(urlparse.urlparse(url))[:3]
100
# discard params, query, fragment
102
# strip off one component of the path component
103
idx = path.rfind('/')
104
if idx == -1 or path == '/':
105
raise BzrError('no branch root found for URL %s'
106
' or enclosing directories'
109
url = urlparse.urlunparse((scheme, host, path, '', '', ''))
113
class RemoteBranch(LocalBranch):
102
114
def __init__(self, baseurl, find_root=True):
103
115
"""Create new proxy for a remote branch."""
105
self.baseurl = _find_remote_root(baseurl)
117
self.base = _find_remote_root(baseurl)
107
self.baseurl = baseurl
108
120
self._check_format()
110
122
self.inventory_store = RemoteStore(baseurl + '/.bzr/inventory-store/')
118
130
__repr__ = __str__
132
def setup_caching(self, cache_root):
133
"""Set up cached stores located under cache_root"""
134
from bzrlib.meta_store import CachedStore
135
for store_name in ('inventory_store', 'text_store', 'revision_store'):
136
if not isinstance(getattr(self, store_name), CachedStore):
137
cache_path = os.path.join(cache_root, store_name)
139
new_store = CachedStore(getattr(self, store_name), cache_path)
140
setattr(self, store_name, new_store)
120
142
def controlfile(self, filename, mode):
121
143
if mode not in ('rb', 'rt', 'r'):
122
144
raise BzrError("file mode %r not supported for remote branches" % mode)
123
return get_url(self.baseurl + '/.bzr/' + filename, False)
145
return get_url(self.base + '/.bzr/' + filename, False)
126
148
def lock_read(self):
130
152
def lock_write(self):
131
153
from errors import LockError
132
154
raise LockError("write lock not supported for remote branch %s"
135
157
def unlock(self):
139
161
def relpath(self, path):
140
if not path.startswith(self.baseurl):
162
if not path.startswith(self.base):
141
163
raise BzrError('path %r is not under base URL %r'
142
% (path, self.baseurl))
143
pl = len(self.baseurl)
144
166
return path[pl:].lstrip('/')
147
169
def get_revision(self, revision_id):
148
from bzrlib.revision import Revision
149
from bzrlib.xml import unpack_xml
150
revf = self.revision_store[revision_id]
151
r = unpack_xml(Revision, revf)
171
revf = self.revision_store[revision_id]
173
raise NoSuchRevision(self, revision_id)
174
r = serializer_v4.read_revision(revf)
152
175
if r.revision_id != revision_id:
153
176
raise BzrCheckError('revision stored as {%s} actually contains {%s}'
154
177
% (revision_id, r.revision_id))