~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/remotebranch.py

  • Committer: Martin Pool
  • Date: 2005-08-30 06:10:39 UTC
  • Revision ID: mbp@sourcefrog.net-20050830061039-1d0347fb236c39ad
- clean up some code in revision.py

- move all exceptions to bzrlib.errors

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/env python
 
2
 
1
3
# Copyright (C) 2005 Canonical Ltd
2
4
 
3
5
# This program is free software; you can redistribute it and/or modify
24
26
 
25
27
import gzip
26
28
from cStringIO import StringIO
27
 
import os
28
29
import urllib2
29
 
import urlparse
30
 
 
31
 
from bzrlib.errors import BzrError, BzrCheckError
32
 
from bzrlib.branch import Branch, LocalBranch, BZR_BRANCH_FORMAT_5
33
 
from bzrlib.trace import mutter
34
 
from bzrlib.weavestore import WeaveStore
35
 
from bzrlib.xml5 import serializer_v5
36
 
 
 
30
 
 
31
from errors import BzrError, BzrCheckError
 
32
from branch import Branch, BZR_BRANCH_FORMAT
 
33
from trace import mutter
37
34
 
38
35
# velocitynet.com.au transparently proxies connections and thereby
39
36
# breaks keep-alive -- sucks!
40
37
 
41
38
 
42
 
ENABLE_URLGRABBER = False
 
39
ENABLE_URLGRABBER = True
43
40
 
44
 
from bzrlib.errors import BzrError, NoSuchRevision
 
41
from bzrlib.errors import BzrError
45
42
 
46
43
class GetFailed(BzrError):
47
44
    def __init__(self, url, status):
87
84
    orig_url = url
88
85
    while True:
89
86
        try:
90
 
            fmt_url = url + '/.bzr/branch-format'
91
 
            ff = get_url(fmt_url)
 
87
            ff = get_url(url + '/.bzr/branch-format')
 
88
 
92
89
            fmt = ff.read()
93
90
            ff.close()
94
91
 
95
 
            if fmt != BZR_BRANCH_FORMAT_5:
 
92
            fmt = fmt.rstrip('\r\n')
 
93
            if fmt != BZR_BRANCH_FORMAT.rstrip('\r\n'):
96
94
                raise BzrError("sorry, branch format %r not supported at url %s"
97
95
                               % (fmt, url))
98
96
            
100
98
        except urllib2.URLError:
101
99
            pass
102
100
 
103
 
        scheme, host, path = list(urlparse.urlparse(url))[:3]
104
 
        # discard params, query, fragment
105
 
        
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'
111
 
                           % orig_url)
112
 
        path = path[:idx]
113
 
        url = urlparse.urlunparse((scheme, host, path, '', '', ''))
114
 
        
115
 
 
116
 
 
117
 
class RemoteBranch(LocalBranch):
 
101
        try:
 
102
            idx = url.rindex('/')
 
103
        except ValueError:
 
104
            raise BzrError('no branch root found for URL %s' % orig_url)
 
105
 
 
106
        url = url[:idx]        
 
107
        
 
108
 
 
109
 
 
110
class RemoteBranch(Branch):
118
111
    def __init__(self, baseurl, find_root=True):
119
112
        """Create new proxy for a remote branch."""
120
113
        if find_root:
121
 
            self.base = _find_remote_root(baseurl)
 
114
            self.baseurl = _find_remote_root(baseurl)
122
115
        else:
123
 
            self.base = baseurl
124
 
        self._check_format(False)
125
 
        # is guaranteed to be a v5 store
 
116
            self.baseurl = baseurl
 
117
            self._check_format()
126
118
 
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/')
132
122
 
133
123
    def __str__(self):
134
124
        b = getattr(self, 'baseurl', 'undefined')
136
126
 
137
127
    __repr__ = __str__
138
128
 
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)
145
 
                os.mkdir(cache_path)
146
 
                new_store = CachedStore(getattr(self, store_name), cache_path)
147
 
                setattr(self, store_name, new_store)
148
 
 
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)
153
133
 
154
134
 
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"
162
 
                        % self.base)
 
142
                        % self.baseurl)
163
143
 
164
144
    def unlock(self):
165
145
        pass
166
146
    
167
147
 
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'
171
 
                           % (path, self.base))
172
 
        pl = len(self.base)
 
151
                           % (path, self.baseurl))
 
152
        pl = len(self.baseurl)
173
153
        return path[pl:].lstrip('/')
174
154
 
175
155
 
176
156
    def get_revision(self, revision_id):
177
 
        try:
178
 
            revf = self.revision_store[revision_id]
179
 
        except KeyError:
180
 
            raise NoSuchRevision(self, revision_id)
181
 
        r = serializer_v5.read_revision(revf)
 
157
        from bzrlib.revision import Revision
 
158
        from bzrlib.xml import unpack_xml
 
159
        revf = self.revision_store[revision_id]
 
160
        r = unpack_xml(Revision, revf)
182
161
        if r.revision_id != revision_id:
183
162
            raise BzrCheckError('revision stored as {%s} actually contains {%s}'
184
163
                                % (revision_id, r.revision_id))
194
173
        if '/' in name:
195
174
            raise ValueError('invalid store id', name)
196
175
        return self._baseurl + '/' + name
197
 
 
198
 
    def __contains__(self, fileid):
199
 
        try:
200
 
            self[fileid]
201
 
            return True
202
 
        except KeyError:
203
 
            return False
204
176
        
205
177
    def __getitem__(self, fileid):
206
178
        p = self._path(fileid)
207
179
        try:
208
180
            return get_url(p, compressed=True)
209
 
        except urllib2.URLError:
210
 
            pass
211
 
        try:
212
 
            return get_url(p, compressed=False)
213
 
        except urllib2.URLError:
 
181
        except:
214
182
            raise KeyError(fileid)
215
183
    
216
184