28
28
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
33
from bzrlib.errors import BzrError, BzrCheckError
34
from bzrlib.branch import Branch, BZR_BRANCH_FORMAT
35
from bzrlib.trace import mutter
36
from bzrlib.xml import serializer_v4
39
ENABLE_URLGRABBER = False
41
from bzrlib.errors import BzrError, NoSuchRevision
43
class GetFailed(BzrError):
44
def __init__(self, url, status):
45
BzrError.__init__(self, "Get %s failed with status %s" % (url, status))
42
49
if ENABLE_URLGRABBER:
44
import urlgrabber.keepalive
45
urlgrabber.keepalive.DEBUG = 0
50
import util.urlgrabber
51
import util.urlgrabber.keepalive
52
util.urlgrabber.keepalive.DEBUG = 0
46
53
def get_url(path, compressed=False):
51
58
mutter("grab url %s" % url)
52
url_f = urlgrabber.urlopen(url, keepalive=1, close_connection=0)
59
url_f = util.urlgrabber.urlopen(url, keepalive=1, close_connection=0)
60
if url_f.status != 200:
61
raise GetFailed(url, url_f.status)
89
98
except urllib2.URLError:
95
raise BzrError('no branch root found for URL %s' % orig_url)
101
scheme, host, path = list(urlparse.urlparse(url))[:3]
102
# discard params, query, fragment
104
# strip off one component of the path component
105
idx = path.rfind('/')
106
if idx == -1 or path == '/':
107
raise BzrError('no branch root found for URL %s'
108
' or enclosing directories'
111
url = urlparse.urlunparse((scheme, host, path, '', '', ''))
102
116
def __init__(self, baseurl, find_root=True):
103
117
"""Create new proxy for a remote branch."""
105
self.baseurl = _find_remote_root(baseurl)
119
self.base = _find_remote_root(baseurl)
107
self.baseurl = baseurl
108
122
self._check_format()
110
124
self.inventory_store = RemoteStore(baseurl + '/.bzr/inventory-store/')
120
134
def controlfile(self, filename, mode):
121
135
if mode not in ('rb', 'rt', 'r'):
122
136
raise BzrError("file mode %r not supported for remote branches" % mode)
123
return get_url(self.baseurl + '/.bzr/' + filename, False)
137
return get_url(self.base + '/.bzr/' + filename, False)
126
140
def lock_read(self):
130
144
def lock_write(self):
131
145
from errors import LockError
132
146
raise LockError("write lock not supported for remote branch %s"
135
149
def unlock(self):
139
153
def relpath(self, path):
140
if not path.startswith(self.baseurl):
154
if not path.startswith(self.base):
141
155
raise BzrError('path %r is not under base URL %r'
142
% (path, self.baseurl))
143
pl = len(self.baseurl)
144
158
return path[pl:].lstrip('/')
147
161
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)
163
revf = self.revision_store[revision_id]
165
raise NoSuchRevision(self, revision_id)
166
r = serializer_v4.read_revision(revf)
152
167
if r.revision_id != revision_id:
153
168
raise BzrCheckError('revision stored as {%s} actually contains {%s}'
154
169
% (revision_id, r.revision_id))