/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: Robert Collins
  • Date: 2005-09-27 07:24:40 UTC
  • mfrom: (1185.1.41)
  • Revision ID: robertc@robertcollins.net-20050927072440-1bf4d99c3e1db5b3
pair programming worx... merge integration and weave

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
 
29
import urlparse
30
30
 
31
 
from errors import BzrError, BzrCheckError
32
 
from branch import Branch, BZR_BRANCH_FORMAT
33
 
from trace import mutter
 
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.xml5 import serializer_v5
34
35
 
35
36
# velocitynet.com.au transparently proxies connections and thereby
36
37
# breaks keep-alive -- sucks!
37
38
 
38
39
 
39
 
ENABLE_URLGRABBER = True
 
40
ENABLE_URLGRABBER = False
40
41
 
41
 
from bzrlib.errors import BzrError
 
42
from bzrlib.errors import BzrError, NoSuchRevision
42
43
 
43
44
class GetFailed(BzrError):
44
45
    def __init__(self, url, status):
47
48
        self.status = status
48
49
 
49
50
if ENABLE_URLGRABBER:
50
 
    import urlgrabber
51
 
    import urlgrabber.keepalive
52
 
    urlgrabber.keepalive.DEBUG = 0
 
51
    import util.urlgrabber
 
52
    import util.urlgrabber.keepalive
 
53
    util.urlgrabber.keepalive.DEBUG = 0
53
54
    def get_url(path, compressed=False):
54
55
        try:
55
56
            url = path
56
57
            if compressed:
57
58
                url += '.gz'
58
59
            mutter("grab url %s" % url)
59
 
            url_f = urlgrabber.urlopen(url, keepalive=1, close_connection=0)
 
60
            url_f = util.urlgrabber.urlopen(url, keepalive=1, close_connection=0)
60
61
            if url_f.status != 200:
61
62
                raise GetFailed(url, url_f.status)
62
63
            if not compressed:
84
85
    orig_url = url
85
86
    while True:
86
87
        try:
87
 
            ff = get_url(url + '/.bzr/branch-format')
88
 
 
 
88
            fmt_url = url + '/.bzr/branch-format'
 
89
            ff = get_url(fmt_url)
89
90
            fmt = ff.read()
90
91
            ff.close()
91
92
 
92
 
            fmt = fmt.rstrip('\r\n')
93
 
            if fmt != BZR_BRANCH_FORMAT.rstrip('\r\n'):
 
93
            if fmt != BZR_BRANCH_FORMAT_5:
94
94
                raise BzrError("sorry, branch format %r not supported at url %s"
95
95
                               % (fmt, url))
96
96
            
98
98
        except urllib2.URLError:
99
99
            pass
100
100
 
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):
 
101
        scheme, host, path = list(urlparse.urlparse(url))[:3]
 
102
        # discard params, query, fragment
 
103
        
 
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'
 
109
                           % orig_url)
 
110
        path = path[:idx]
 
111
        url = urlparse.urlunparse((scheme, host, path, '', '', ''))
 
112
        
 
113
 
 
114
 
 
115
class RemoteBranch(LocalBranch):
111
116
    def __init__(self, baseurl, find_root=True):
112
117
        """Create new proxy for a remote branch."""
113
118
        if find_root:
114
 
            self.baseurl = _find_remote_root(baseurl)
 
119
            self.base = _find_remote_root(baseurl)
115
120
        else:
116
 
            self.baseurl = baseurl
 
121
            self.base = baseurl
117
122
            self._check_format()
118
123
 
119
124
        self.inventory_store = RemoteStore(baseurl + '/.bzr/inventory-store/')
126
131
 
127
132
    __repr__ = __str__
128
133
 
 
134
    def setup_caching(self, cache_root):
 
135
        """Set up cached stores located under cache_root"""
 
136
        from bzrlib.meta_store import CachedStore
 
137
        for store_name in ('inventory_store', 'text_store', 'revision_store'):
 
138
            if not isinstance(getattr(self, store_name), CachedStore):
 
139
                cache_path = os.path.join(cache_root, store_name)
 
140
                os.mkdir(cache_path)
 
141
                new_store = CachedStore(getattr(self, store_name), cache_path)
 
142
                setattr(self, store_name, new_store)
 
143
 
129
144
    def controlfile(self, filename, mode):
130
145
        if mode not in ('rb', 'rt', 'r'):
131
146
            raise BzrError("file mode %r not supported for remote branches" % mode)
132
 
        return get_url(self.baseurl + '/.bzr/' + filename, False)
 
147
        return get_url(self.base + '/.bzr/' + filename, False)
133
148
 
134
149
 
135
150
    def lock_read(self):
139
154
    def lock_write(self):
140
155
        from errors import LockError
141
156
        raise LockError("write lock not supported for remote branch %s"
142
 
                        % self.baseurl)
 
157
                        % self.base)
143
158
 
144
159
    def unlock(self):
145
160
        pass
146
161
    
147
162
 
148
163
    def relpath(self, path):
149
 
        if not path.startswith(self.baseurl):
 
164
        if not path.startswith(self.base):
150
165
            raise BzrError('path %r is not under base URL %r'
151
 
                           % (path, self.baseurl))
152
 
        pl = len(self.baseurl)
 
166
                           % (path, self.base))
 
167
        pl = len(self.base)
153
168
        return path[pl:].lstrip('/')
154
169
 
155
170
 
156
171
    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)
 
172
        try:
 
173
            revf = self.revision_store[revision_id]
 
174
        except KeyError:
 
175
            raise NoSuchRevision(self, revision_id)
 
176
        r = serializer_v5.read_revision(revf)
161
177
        if r.revision_id != revision_id:
162
178
            raise BzrCheckError('revision stored as {%s} actually contains {%s}'
163
179
                                % (revision_id, r.revision_id))
178
194
        p = self._path(fileid)
179
195
        try:
180
196
            return get_url(p, compressed=True)
181
 
        except:
 
197
        except urllib2.URLError:
 
198
            pass
 
199
        try:
 
200
            return get_url(p, compressed=False)
 
201
        except urllib2.URLError:
182
202
            raise KeyError(fileid)
183
203
    
184
204