/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
411 by Martin Pool
- start adding more useful RemoteBranch() class
1
#! /usr/bin/env python
2
188 by mbp at sourcefrog
- experimental remote-branch support
3
# Copyright (C) 2005 Canonical Ltd
4
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
9
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU General Public License for more details.
14
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
19
412 by Martin Pool
- RemoteBranch displays the log of a remote branch.
20
"""Proxy object for access to remote branches.
21
22
At the moment remote branches are only for HTTP and only for read
23
access.
24
25
"""
26
27
188 by mbp at sourcefrog
- experimental remote-branch support
28
## XXX: This is pretty slow on high-latency connections because it
29
## doesn't keep the HTTP connection alive.  If you have a smart local
30
## proxy it may be much better.  Eventually I want to switch to
31
## urlgrabber which should use HTTP much more efficiently.
32
33
412 by Martin Pool
- RemoteBranch displays the log of a remote branch.
34
import gzip
192 by mbp at sourcefrog
- exercise the network more towards doing a remote clone
35
from sets import Set
36
from cStringIO import StringIO
188 by mbp at sourcefrog
- experimental remote-branch support
37
412 by Martin Pool
- RemoteBranch displays the log of a remote branch.
38
from errors import BzrError, BzrCheckError
188 by mbp at sourcefrog
- experimental remote-branch support
39
from revision import Revision
411 by Martin Pool
- start adding more useful RemoteBranch() class
40
from branch import Branch
192 by mbp at sourcefrog
- exercise the network more towards doing a remote clone
41
from inventory import Inventory
188 by mbp at sourcefrog
- experimental remote-branch support
42
43
# h = HTTPConnection('localhost:8000')
44
# h = HTTPConnection('bazaar-ng.org')
45
193 by mbp at sourcefrog
more experiments with http get
46
# velocitynet.com.au transparently proxies connections and thereby
47
# breaks keep-alive -- sucks!
48
49
50
51
411 by Martin Pool
- start adding more useful RemoteBranch() class
52
# prefix = 'http://localhost:8000'
53
BASE_URL = 'http://bazaar-ng.org/bzr/bzr.dev/'
188 by mbp at sourcefrog
- experimental remote-branch support
54
412 by Martin Pool
- RemoteBranch displays the log of a remote branch.
55
ENABLE_URLGRABBER = False
56
57
def get_url(url, compressed=False):
58
    import urllib2
59
    if compressed:
60
        url += '.gz'
61
    url_f = urllib2.urlopen(url)
62
    if compressed:
63
        return gzip.GzipFile(fileobj=StringIO(url_f.read()))
64
    else:
65
        return url_f
66
67
if ENABLE_URLGRABBER:
68
    import urlgrabber
69
    import urlgrabber.keepalive
70
    urlgrabber.keepalive.DEBUG = 0
71
    def get_url(path, compressed=False):
72
        try:
73
            url = path
74
            if compressed:
75
                url += '.gz'
76
            url_f = urlgrabber.urlopen(url, keepalive=1, close_connection=0)
77
            if not compressed:
78
                return url_f
79
            else:
80
                return gzip.GzipFile(fileobj=StringIO(url_f.read()))
81
        except urllib2.URLError, e:
82
            raise BzrError("remote fetch failed: %r: %s" % (url, e))
188 by mbp at sourcefrog
- experimental remote-branch support
83
192 by mbp at sourcefrog
- exercise the network more towards doing a remote clone
84
411 by Martin Pool
- start adding more useful RemoteBranch() class
85
class RemoteBranch(Branch):
86
    def __init__(self, baseurl):
87
        """Create new proxy for a remote branch."""
88
        self.baseurl = baseurl
89
        self._check_format()
90
91
    def controlfile(self, filename, mode):
92
        if mode not in ('rb', 'rt', 'r'):
93
            raise BzrError("file mode %r not supported for remote branches" % mode)
94
        return get_url(self.baseurl + '/.bzr/' + filename, False)
95
96
    def _need_readlock(self):
97
        # remote branch always safe for read
98
        pass
99
100
    def _need_writelock(self):
101
        raise BzrError("cannot get write lock on HTTP remote branch")
412 by Martin Pool
- RemoteBranch displays the log of a remote branch.
102
103
    def get_revision(self, revision_id):
104
        from revision import Revision
105
        revf = get_url(self.baseurl + '/.bzr/revision-store/' + revision_id,
106
                       True)
107
        r = Revision.read_xml(revf)
108
        if r.revision_id != revision_id:
109
            raise BzrCheckError('revision stored as {%s} actually contains {%s}'
110
                                % (revision_id, r.revision_id))
111
        return r
188 by mbp at sourcefrog
- experimental remote-branch support
112
    
411 by Martin Pool
- start adding more useful RemoteBranch() class
113
114
def simple_walk():
115
    got_invs = Set()
116
    got_texts = Set()
117
118
    print 'read history'
119
    history = get_url('/.bzr/revision-history').readlines()
120
    num_revs = len(history)
121
    for i, rev_id in enumerate(history):
122
        rev_id = rev_id.rstrip()
123
        print 'read revision %d/%d' % (i, num_revs)
124
125
        # python gzip needs a seekable file (!!) but the HTTP response
126
        # isn't, so we need to buffer it
127
128
        rev_f = get_url('/.bzr/revision-store/%s' % rev_id,
192 by mbp at sourcefrog
- exercise the network more towards doing a remote clone
129
                        compressed=True)
411 by Martin Pool
- start adding more useful RemoteBranch() class
130
131
        rev = Revision.read_xml(rev_f)
132
        print rev.message
133
        inv_id = rev.inventory_id
134
        if inv_id not in got_invs:
135
            print 'get inventory %s' % inv_id
136
            inv_f = get_url('/.bzr/inventory-store/%s' % inv_id,
137
                            compressed=True)
138
            inv = Inventory.read_xml(inv_f)
139
            print '%4d inventory entries' % len(inv)
140
141
            for path, ie in inv.iter_entries():
142
                text_id = ie.text_id
143
                if text_id == None:
144
                    continue
145
                if text_id in got_texts:
146
                    continue
147
                print '  fetch %s text {%s}' % (path, text_id)
148
                text_f = get_url('/.bzr/text-store/%s' % text_id,
149
                                 compressed=True)
150
                got_texts.add(text_id)
151
152
            got_invs.add(inv_id)
153
154
        print '----'
155
156
157
def try_me():
158
    b = RemoteBranch(BASE_URL)
412 by Martin Pool
- RemoteBranch displays the log of a remote branch.
159
    ## print '\n'.join(b.revision_history())
160
    from log import show_log
161
    show_log(b)
411 by Martin Pool
- start adding more useful RemoteBranch() class
162
163
164
if __name__ == '__main__':
165
    try_me()
166