/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/remotebranch.py

  • Committer: Martin Pool
  • Date: 2005-09-29 12:35:37 UTC
  • mto: (1185.12.2) (1393.1.12)
  • mto: This revision was merged to the branch mainline in revision 1396.
  • Revision ID: mbp@sourcefrog.net-20050929123537-65f1aa0de94e1fea
- fold testsweet into bzrlib.selftest

  This makes the test suite run properly even for an installed copy of
  bzr.  testsweet was pretty strongly coupled to bzrlib anyhow.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#! /usr/bin/env python
2
 
 
3
1
# Copyright (C) 2005 Canonical Ltd
4
2
 
5
3
# This program is free software; you can redistribute it and/or modify
26
24
 
27
25
import gzip
28
26
from cStringIO import StringIO
 
27
import os
29
28
import urllib2
30
 
 
31
 
from errors import BzrError, BzrCheckError
32
 
from branch import Branch, BZR_BRANCH_FORMAT
33
 
from trace import mutter
 
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
 
34
37
 
35
38
# velocitynet.com.au transparently proxies connections and thereby
36
39
# breaks keep-alive -- sucks!
37
40
 
38
41
 
39
 
ENABLE_URLGRABBER = True
 
42
ENABLE_URLGRABBER = False
40
43
 
41
 
from bzrlib.errors import BzrError
 
44
from bzrlib.errors import BzrError, NoSuchRevision
42
45
 
43
46
class GetFailed(BzrError):
44
47
    def __init__(self, url, status):
47
50
        self.status = status
48
51
 
49
52
if ENABLE_URLGRABBER:
50
 
    import urlgrabber
51
 
    import urlgrabber.keepalive
52
 
    urlgrabber.keepalive.DEBUG = 0
 
53
    import util.urlgrabber
 
54
    import util.urlgrabber.keepalive
 
55
    util.urlgrabber.keepalive.DEBUG = 0
53
56
    def get_url(path, compressed=False):
54
57
        try:
55
58
            url = path
56
59
            if compressed:
57
60
                url += '.gz'
58
61
            mutter("grab url %s" % url)
59
 
            url_f = urlgrabber.urlopen(url, keepalive=1, close_connection=0)
 
62
            url_f = util.urlgrabber.urlopen(url, keepalive=1, close_connection=0)
60
63
            if url_f.status != 200:
61
64
                raise GetFailed(url, url_f.status)
62
65
            if not compressed:
84
87
    orig_url = url
85
88
    while True:
86
89
        try:
87
 
            ff = get_url(url + '/.bzr/branch-format')
88
 
 
 
90
            fmt_url = url + '/.bzr/branch-format'
 
91
            ff = get_url(fmt_url)
89
92
            fmt = ff.read()
90
93
            ff.close()
91
94
 
92
 
            fmt = fmt.rstrip('\r\n')
93
 
            if fmt != BZR_BRANCH_FORMAT.rstrip('\r\n'):
 
95
            if fmt != BZR_BRANCH_FORMAT_5:
94
96
                raise BzrError("sorry, branch format %r not supported at url %s"
95
97
                               % (fmt, url))
96
98
            
98
100
        except urllib2.URLError:
99
101
            pass
100
102
 
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):
 
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):
111
118
    def __init__(self, baseurl, find_root=True):
112
119
        """Create new proxy for a remote branch."""
113
120
        if find_root:
114
 
            self.baseurl = _find_remote_root(baseurl)
 
121
            self.base = _find_remote_root(baseurl)
115
122
        else:
116
 
            self.baseurl = baseurl
117
 
            self._check_format()
 
123
            self.base = baseurl
 
124
        self._check_format(False)
 
125
        # is guaranteed to be a v5 store
118
126
 
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/')
 
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'))
122
132
 
123
133
    def __str__(self):
124
134
        b = getattr(self, 'baseurl', 'undefined')
126
136
 
127
137
    __repr__ = __str__
128
138
 
 
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
 
129
149
    def controlfile(self, filename, mode):
130
150
        if mode not in ('rb', 'rt', 'r'):
131
151
            raise BzrError("file mode %r not supported for remote branches" % mode)
132
 
        return get_url(self.baseurl + '/.bzr/' + filename, False)
 
152
        return get_url(self.base + '/.bzr/' + filename, False)
133
153
 
134
154
 
135
155
    def lock_read(self):
139
159
    def lock_write(self):
140
160
        from errors import LockError
141
161
        raise LockError("write lock not supported for remote branch %s"
142
 
                        % self.baseurl)
 
162
                        % self.base)
143
163
 
144
164
    def unlock(self):
145
165
        pass
146
166
    
147
167
 
148
168
    def relpath(self, path):
149
 
        if not path.startswith(self.baseurl):
 
169
        if not path.startswith(self.base):
150
170
            raise BzrError('path %r is not under base URL %r'
151
 
                           % (path, self.baseurl))
152
 
        pl = len(self.baseurl)
 
171
                           % (path, self.base))
 
172
        pl = len(self.base)
153
173
        return path[pl:].lstrip('/')
154
174
 
155
175
 
156
176
    def get_revision(self, revision_id):
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)
 
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)
161
182
        if r.revision_id != revision_id:
162
183
            raise BzrCheckError('revision stored as {%s} actually contains {%s}'
163
184
                                % (revision_id, r.revision_id))
173
194
        if '/' in name:
174
195
            raise ValueError('invalid store id', name)
175
196
        return self._baseurl + '/' + name
 
197
 
 
198
    def __contains__(self, fileid):
 
199
        try:
 
200
            self[fileid]
 
201
            return True
 
202
        except KeyError:
 
203
            return False
176
204
        
177
205
    def __getitem__(self, fileid):
178
206
        p = self._path(fileid)
179
207
        try:
180
208
            return get_url(p, compressed=True)
181
 
        except:
 
209
        except urllib2.URLError:
 
210
            pass
 
211
        try:
 
212
            return get_url(p, compressed=False)
 
213
        except urllib2.URLError:
182
214
            raise KeyError(fileid)
183
215
    
184
216