~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/remotebranch.py

  • Committer: Martin Pool
  • Date: 2005-08-25 08:19:55 UTC
  • Revision ID: mbp@sourcefrog.net-20050825081955-d7295021dcfcd2ba
- disable standard logging to .bzr.log and stderr while running 
  tests

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
 
 
25
24
"""
26
25
 
27
26
 
28
27
import gzip
29
 
from sets import Set
30
28
from cStringIO import StringIO
31
29
import urllib2
32
30
 
38
36
# breaks keep-alive -- sucks!
39
37
 
40
38
 
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
54
 
 
 
39
ENABLE_URLGRABBER = True
 
40
 
 
41
from bzrlib.errors import BzrError
 
42
 
 
43
class GetFailed(BzrError):
 
44
    def __init__(self, url, status):
 
45
        BzrError.__init__(self, "Get %s failed with status %s" % (url, status))
 
46
        self.url = url
 
47
        self.status = status
55
48
 
56
49
if ENABLE_URLGRABBER:
57
 
    import urlgrabber
58
 
    import urlgrabber.keepalive
59
 
    urlgrabber.keepalive.DEBUG = 0
 
50
    import util.urlgrabber
 
51
    import util.urlgrabber.keepalive
 
52
    util.urlgrabber.keepalive.DEBUG = 0
60
53
    def get_url(path, compressed=False):
61
54
        try:
62
55
            url = path
63
56
            if compressed:
64
57
                url += '.gz'
65
 
            url_f = urlgrabber.urlopen(url, keepalive=1, close_connection=0)
 
58
            mutter("grab url %s" % url)
 
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)
66
62
            if not compressed:
67
63
                return url_f
68
64
            else:
69
65
                return gzip.GzipFile(fileobj=StringIO(url_f.read()))
70
66
        except urllib2.URLError, e:
71
67
            raise BzrError("remote fetch failed: %r: %s" % (url, e))
 
68
else:
 
69
    def get_url(url, compressed=False):
 
70
        import urllib2
 
71
        if compressed:
 
72
            url += '.gz'
 
73
        mutter("get_url %s" % url)
 
74
        url_f = urllib2.urlopen(url)
 
75
        if compressed:
 
76
            return gzip.GzipFile(fileobj=StringIO(url_f.read()))
 
77
        else:
 
78
            return url_f
72
79
 
73
80
 
74
81
 
101
108
 
102
109
 
103
110
class RemoteBranch(Branch):
104
 
    def __init__(self, baseurl, find_root=True, lock_mode='r'):
 
111
    def __init__(self, baseurl, find_root=True):
105
112
        """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
 
 
110
113
        if find_root:
111
114
            self.baseurl = _find_remote_root(baseurl)
112
115
        else:
113
116
            self.baseurl = baseurl
114
117
            self._check_format()
115
118
 
 
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/')
 
122
 
116
123
    def __str__(self):
117
 
        return '%s(%r)' % (self.__class__.__name__, self.baseurl)
 
124
        b = getattr(self, 'baseurl', 'undefined')
 
125
        return '%s(%r)' % (self.__class__.__name__, b)
118
126
 
119
127
    __repr__ = __str__
120
128
 
123
131
            raise BzrError("file mode %r not supported for remote branches" % mode)
124
132
        return get_url(self.baseurl + '/.bzr/' + filename, False)
125
133
 
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")
 
134
 
 
135
    def lock_read(self):
 
136
        # no locking for remote branches yet
 
137
        pass
 
138
 
 
139
    def lock_write(self):
 
140
        from errors import LockError
 
141
        raise LockError("write lock not supported for remote branch %s"
 
142
                        % self.baseurl)
 
143
 
 
144
    def unlock(self):
 
145
        pass
 
146
    
132
147
 
133
148
    def relpath(self, path):
134
149
        if not path.startswith(self.baseurl):
137
152
        pl = len(self.baseurl)
138
153
        return path[pl:].lstrip('/')
139
154
 
 
155
 
140
156
    def get_revision(self, revision_id):
141
 
        from revision import Revision
142
 
        revf = get_url(self.baseurl + '/.bzr/revision-store/' + revision_id,
143
 
                       True)
144
 
        r = Revision.read_xml(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)
145
161
        if r.revision_id != revision_id:
146
162
            raise BzrCheckError('revision stored as {%s} actually contains {%s}'
147
163
                                % (revision_id, r.revision_id))
148
164
        return r
 
165
 
 
166
 
 
167
class RemoteStore(object):
 
168
    def __init__(self, baseurl):
 
169
        self._baseurl = baseurl
 
170
        
 
171
 
 
172
    def _path(self, name):
 
173
        if '/' in name:
 
174
            raise ValueError('invalid store id', name)
 
175
        return self._baseurl + '/' + name
 
176
        
 
177
    def __getitem__(self, fileid):
 
178
        p = self._path(fileid)
 
179
        try:
 
180
            return get_url(p, compressed=True)
 
181
        except:
 
182
            raise KeyError(fileid)
149
183
    
150
184
 
151
 
def simple_walk():
152
 
    from revision import Revision
153
 
    from branch import Branch
154
 
    from inventory import Inventory
155
 
 
156
 
    got_invs = Set()
157
 
    got_texts = Set()
158
 
 
159
 
    print 'read history'
160
 
    history = get_url('/.bzr/revision-history').readlines()
161
 
    num_revs = len(history)
162
 
    for i, rev_id in enumerate(history):
163
 
        rev_id = rev_id.rstrip()
164
 
        print 'read revision %d/%d' % (i, num_revs)
165
 
 
166
 
        # python gzip needs a seekable file (!!) but the HTTP response
167
 
        # isn't, so we need to buffer it
168
 
 
169
 
        rev_f = get_url('/.bzr/revision-store/%s' % rev_id,
170
 
                        compressed=True)
171
 
 
172
 
        rev = Revision.read_xml(rev_f)
173
 
        print rev.message
174
 
        inv_id = rev.inventory_id
175
 
        if inv_id not in got_invs:
176
 
            print 'get inventory %s' % inv_id
177
 
            inv_f = get_url('/.bzr/inventory-store/%s' % inv_id,
178
 
                            compressed=True)
179
 
            inv = Inventory.read_xml(inv_f)
180
 
            print '%4d inventory entries' % len(inv)
181
 
 
182
 
            for path, ie in inv.iter_entries():
183
 
                text_id = ie.text_id
184
 
                if text_id == None:
185
 
                    continue
186
 
                if text_id in got_texts:
187
 
                    continue
188
 
                print '  fetch %s text {%s}' % (path, text_id)
189
 
                text_f = get_url('/.bzr/text-store/%s' % text_id,
190
 
                                 compressed=True)
191
 
                got_texts.add(text_id)
192
 
 
193
 
            got_invs.add(inv_id)
194
 
 
195
 
        print '----'
196
 
 
197
 
 
198
 
def try_me():
199
 
    BASE_URL = 'http://bazaar-ng.org/bzr/bzr.dev/'
200
 
    b = RemoteBranch(BASE_URL)
201
 
    ## print '\n'.join(b.revision_history())
202
 
    from log import show_log
203
 
    show_log(b)
204
 
 
205
 
 
206
 
if __name__ == '__main__':
207
 
    try_me()
208
185