~jeanfrancois.roy/bzr/url-safe-escape

« 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
31
from bzrlib.errors import BzrError, BzrCheckError
32
 
from bzrlib.branch import Branch, BZR_BRANCH_FORMAT_5
 
32
from bzrlib.branch import Branch, LocalBranch, BZR_BRANCH_FORMAT_5
33
33
from bzrlib.trace import mutter
34
34
from bzrlib.xml5 import serializer_v5
35
35
 
85
85
    orig_url = url
86
86
    while True:
87
87
        try:
88
 
            ff = get_url(url + '/.bzr/branch-format')
89
 
 
 
88
            fmt_url = url + '/.bzr/branch-format'
 
89
            ff = get_url(fmt_url)
90
90
            fmt = ff.read()
91
91
            ff.close()
92
92
 
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
172
        try:
159
173
            revf = self.revision_store[revision_id]
160
174
        except KeyError:
180
194
        p = self._path(fileid)
181
195
        try:
182
196
            return get_url(p, compressed=True)
183
 
        except:
 
197
        except urllib2.URLError:
 
198
            pass
 
199
        try:
 
200
            return get_url(p, compressed=False)
 
201
        except urllib2.URLError:
184
202
            raise KeyError(fileid)
185
203
    
186
204