~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/remotebranch.py

  • Committer: Martin Pool
  • Date: 2005-05-10 06:21:07 UTC
  • Revision ID: mbp@sourcefrog.net-20050510062107-8b15b4c6b52d0279
doc

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 
22
22
At the moment remote branches are only for HTTP and only for read
23
23
access.
 
24
 
24
25
"""
25
26
 
26
27
 
27
28
import gzip
 
29
from sets import Set
28
30
from cStringIO import StringIO
29
31
import urllib2
30
32
 
36
38
# breaks keep-alive -- sucks!
37
39
 
38
40
 
39
 
ENABLE_URLGRABBER = True
 
41
 
 
42
ENABLE_URLGRABBER = False
 
43
 
 
44
def get_url(url, compressed=False):
 
45
    import urllib2
 
46
    if compressed:
 
47
        url += '.gz'
 
48
    mutter("get_url %s" % url)
 
49
    url_f = urllib2.urlopen(url)
 
50
    if compressed:
 
51
        return gzip.GzipFile(fileobj=StringIO(url_f.read()))
 
52
    else:
 
53
        return url_f
40
54
 
41
55
 
42
56
if ENABLE_URLGRABBER:
48
62
            url = path
49
63
            if compressed:
50
64
                url += '.gz'
51
 
            mutter("grab url %s" % url)
52
65
            url_f = urlgrabber.urlopen(url, keepalive=1, close_connection=0)
53
66
            if not compressed:
54
67
                return url_f
56
69
                return gzip.GzipFile(fileobj=StringIO(url_f.read()))
57
70
        except urllib2.URLError, e:
58
71
            raise BzrError("remote fetch failed: %r: %s" % (url, e))
59
 
else:
60
 
    def get_url(url, compressed=False):
61
 
        import urllib2
62
 
        if compressed:
63
 
            url += '.gz'
64
 
        mutter("get_url %s" % url)
65
 
        url_f = urllib2.urlopen(url)
66
 
        if compressed:
67
 
            return gzip.GzipFile(fileobj=StringIO(url_f.read()))
68
 
        else:
69
 
            return url_f
70
72
 
71
73
 
72
74
 
99
101
 
100
102
 
101
103
class RemoteBranch(Branch):
102
 
    def __init__(self, baseurl, find_root=True):
 
104
    def __init__(self, baseurl, find_root=True, lock_mode='r'):
103
105
        """Create new proxy for a remote branch."""
 
106
        if lock_mode not in ('', 'r'):
 
107
            raise BzrError('lock mode %r is not supported for remote branches'
 
108
                           % lock_mode)
 
109
 
104
110
        if find_root:
105
111
            self.baseurl = _find_remote_root(baseurl)
106
112
        else:
107
113
            self.baseurl = baseurl
108
114
            self._check_format()
109
115
 
110
 
        self.inventory_store = RemoteStore(baseurl + '/.bzr/inventory-store/')
111
 
        self.text_store = RemoteStore(baseurl + '/.bzr/text-store/')
112
 
        self.revision_store = RemoteStore(baseurl + '/.bzr/revision-store/')
113
 
 
114
116
    def __str__(self):
115
 
        b = getattr(self, 'baseurl', 'undefined')
116
 
        return '%s(%r)' % (self.__class__.__name__, b)
 
117
        return '%s(%r)' % (self.__class__.__name__, self.baseurl)
117
118
 
118
119
    __repr__ = __str__
119
120
 
122
123
            raise BzrError("file mode %r not supported for remote branches" % mode)
123
124
        return get_url(self.baseurl + '/.bzr/' + filename, False)
124
125
 
125
 
 
126
 
    def lock_read(self):
127
 
        # no locking for remote branches yet
128
 
        pass
129
 
 
130
 
    def lock_write(self):
131
 
        from errors import LockError
132
 
        raise LockError("write lock not supported for remote branch %s"
133
 
                        % self.baseurl)
134
 
 
135
 
    def unlock(self):
136
 
        pass
137
 
    
 
126
    def _need_readlock(self):
 
127
        # remote branch always safe for read
 
128
        pass
 
129
 
 
130
    def _need_writelock(self):
 
131
        raise BzrError("cannot get write lock on HTTP remote branch")
138
132
 
139
133
    def relpath(self, path):
140
134
        if not path.startswith(self.baseurl):
152
146
            raise BzrCheckError('revision stored as {%s} actually contains {%s}'
153
147
                                % (revision_id, r.revision_id))
154
148
        return r
155
 
 
156
 
 
157
 
class RemoteStore(object):
158
 
    def __init__(self, baseurl):
159
 
        self._baseurl = baseurl
160
 
        
161
 
 
162
 
    def _path(self, name):
163
 
        if '/' in name:
164
 
            raise ValueError('invalid store id', name)
165
 
        return self._baseurl + '/' + name
166
 
        
167
 
    def __getitem__(self, fileid):
168
 
        p = self._path(fileid)
169
 
        return get_url(p, compressed=True)
170
149
    
171
150
 
172
151
def simple_walk():
173
 
    """For experimental purposes, traverse many parts of a remote branch"""
174
152
    from revision import Revision
175
153
    from branch import Branch
176
154
    from inventory import Inventory
177
155
 
178
 
    got_invs = {}
179
 
    got_texts = {}
 
156
    got_invs = Set()
 
157
    got_texts = Set()
180
158
 
181
159
    print 'read history'
182
160
    history = get_url('/.bzr/revision-history').readlines()
210
188
                print '  fetch %s text {%s}' % (path, text_id)
211
189
                text_f = get_url('/.bzr/text-store/%s' % text_id,
212
190
                                 compressed=True)
213
 
                got_texts[text_id] = True
 
191
                got_texts.add(text_id)
214
192
 
215
 
            got_invs.add[inv_id] = True
 
193
            got_invs.add(inv_id)
216
194
 
217
195
        print '----'
218
196