~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/remote.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2011-03-11 20:21:17 UTC
  • mfrom: (5712.3.23 lazy-bzrdir)
  • Revision ID: pqm@pqm.ubuntu.com-20110311202117-tvicdu07o2x7jh8b
(jelmer) Add Prober.known_formats() in favour of
 BzrDirFormat.register_format() and ControlDirFormat.register_format().
 (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
from bzrlib import (
20
20
    bencode,
21
21
    branch,
22
 
    bzrdir,
 
22
    bzrdir as _mod_bzrdir,
23
23
    config,
24
24
    controldir,
25
25
    debug,
27
27
    graph,
28
28
    lock,
29
29
    lockdir,
30
 
    repository,
31
30
    repository as _mod_repository,
32
 
    revision,
33
31
    revision as _mod_revision,
34
32
    static_tuple,
35
33
    symbol_versioning,
36
34
    urlutils,
37
35
)
38
36
from bzrlib.branch import BranchReferenceFormat, BranchWriteLockResult
39
 
from bzrlib.bzrdir import BzrDir, RemoteBzrDirFormat
40
37
from bzrlib.decorators import needs_read_lock, needs_write_lock, only_raises
41
38
from bzrlib.errors import (
42
39
    NoSuchRevision,
44
41
    )
45
42
from bzrlib.lockable_files import LockableFiles
46
43
from bzrlib.smart import client, vfs, repository as smart_repo
47
 
from bzrlib.revision import ensure_null, NULL_REVISION
 
44
from bzrlib.smart.client import _SmartClient
 
45
from bzrlib.revision import NULL_REVISION
48
46
from bzrlib.repository import RepositoryWriteLockResult
49
47
from bzrlib.trace import mutter, note, warning
50
48
 
89
87
    return format
90
88
 
91
89
 
92
 
# Note: RemoteBzrDirFormat is in bzrdir.py
93
 
 
94
 
class RemoteBzrDir(BzrDir, _RpcHelper):
 
90
# Note that RemoteBzrDirProber lives in bzrlib.bzrdir so bzrlib.remote
 
91
# does not have to be imported unless a remote format is involved.
 
92
 
 
93
class RemoteBzrDirFormat(_mod_bzrdir.BzrDirMetaFormat1):
 
94
    """Format representing bzrdirs accessed via a smart server"""
 
95
 
 
96
    supports_workingtrees = False
 
97
 
 
98
    def __init__(self):
 
99
        _mod_bzrdir.BzrDirMetaFormat1.__init__(self)
 
100
        # XXX: It's a bit ugly that the network name is here, because we'd
 
101
        # like to believe that format objects are stateless or at least
 
102
        # immutable,  However, we do at least avoid mutating the name after
 
103
        # it's returned.  See <https://bugs.launchpad.net/bzr/+bug/504102>
 
104
        self._network_name = None
 
105
 
 
106
    def __repr__(self):
 
107
        return "%s(_network_name=%r)" % (self.__class__.__name__,
 
108
            self._network_name)
 
109
 
 
110
    def get_format_description(self):
 
111
        if self._network_name:
 
112
            real_format = controldir.network_format_registry.get(self._network_name)
 
113
            return 'Remote: ' + real_format.get_format_description()
 
114
        return 'bzr remote bzrdir'
 
115
 
 
116
    def get_format_string(self):
 
117
        raise NotImplementedError(self.get_format_string)
 
118
 
 
119
    def network_name(self):
 
120
        if self._network_name:
 
121
            return self._network_name
 
122
        else:
 
123
            raise AssertionError("No network name set.")
 
124
 
 
125
    def initialize_on_transport(self, transport):
 
126
        try:
 
127
            # hand off the request to the smart server
 
128
            client_medium = transport.get_smart_medium()
 
129
        except errors.NoSmartMedium:
 
130
            # TODO: lookup the local format from a server hint.
 
131
            local_dir_format = _mod_bzrdir.BzrDirMetaFormat1()
 
132
            return local_dir_format.initialize_on_transport(transport)
 
133
        client = _SmartClient(client_medium)
 
134
        path = client.remote_path_from_transport(transport)
 
135
        try:
 
136
            response = client.call('BzrDirFormat.initialize', path)
 
137
        except errors.ErrorFromSmartServer, err:
 
138
            _translate_error(err, path=path)
 
139
        if response[0] != 'ok':
 
140
            raise errors.SmartProtocolError('unexpected response code %s' % (response,))
 
141
        format = RemoteBzrDirFormat()
 
142
        self._supply_sub_formats_to(format)
 
143
        return RemoteBzrDir(transport, format)
 
144
 
 
145
    def parse_NoneTrueFalse(self, arg):
 
146
        if not arg:
 
147
            return None
 
148
        if arg == 'False':
 
149
            return False
 
150
        if arg == 'True':
 
151
            return True
 
152
        raise AssertionError("invalid arg %r" % arg)
 
153
 
 
154
    def _serialize_NoneTrueFalse(self, arg):
 
155
        if arg is False:
 
156
            return 'False'
 
157
        if arg:
 
158
            return 'True'
 
159
        return ''
 
160
 
 
161
    def _serialize_NoneString(self, arg):
 
162
        return arg or ''
 
163
 
 
164
    def initialize_on_transport_ex(self, transport, use_existing_dir=False,
 
165
        create_prefix=False, force_new_repo=False, stacked_on=None,
 
166
        stack_on_pwd=None, repo_format_name=None, make_working_trees=None,
 
167
        shared_repo=False):
 
168
        try:
 
169
            # hand off the request to the smart server
 
170
            client_medium = transport.get_smart_medium()
 
171
        except errors.NoSmartMedium:
 
172
            do_vfs = True
 
173
        else:
 
174
            # Decline to open it if the server doesn't support our required
 
175
            # version (3) so that the VFS-based transport will do it.
 
176
            if client_medium.should_probe():
 
177
                try:
 
178
                    server_version = client_medium.protocol_version()
 
179
                    if server_version != '2':
 
180
                        do_vfs = True
 
181
                    else:
 
182
                        do_vfs = False
 
183
                except errors.SmartProtocolError:
 
184
                    # Apparently there's no usable smart server there, even though
 
185
                    # the medium supports the smart protocol.
 
186
                    do_vfs = True
 
187
            else:
 
188
                do_vfs = False
 
189
        if not do_vfs:
 
190
            client = _SmartClient(client_medium)
 
191
            path = client.remote_path_from_transport(transport)
 
192
            if client_medium._is_remote_before((1, 16)):
 
193
                do_vfs = True
 
194
        if do_vfs:
 
195
            # TODO: lookup the local format from a server hint.
 
196
            local_dir_format = _mod_bzrdir.BzrDirMetaFormat1()
 
197
            self._supply_sub_formats_to(local_dir_format)
 
198
            return local_dir_format.initialize_on_transport_ex(transport,
 
199
                use_existing_dir=use_existing_dir, create_prefix=create_prefix,
 
200
                force_new_repo=force_new_repo, stacked_on=stacked_on,
 
201
                stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
 
202
                make_working_trees=make_working_trees, shared_repo=shared_repo,
 
203
                vfs_only=True)
 
204
        return self._initialize_on_transport_ex_rpc(client, path, transport,
 
205
            use_existing_dir, create_prefix, force_new_repo, stacked_on,
 
206
            stack_on_pwd, repo_format_name, make_working_trees, shared_repo)
 
207
 
 
208
    def _initialize_on_transport_ex_rpc(self, client, path, transport,
 
209
        use_existing_dir, create_prefix, force_new_repo, stacked_on,
 
210
        stack_on_pwd, repo_format_name, make_working_trees, shared_repo):
 
211
        args = []
 
212
        args.append(self._serialize_NoneTrueFalse(use_existing_dir))
 
213
        args.append(self._serialize_NoneTrueFalse(create_prefix))
 
214
        args.append(self._serialize_NoneTrueFalse(force_new_repo))
 
215
        args.append(self._serialize_NoneString(stacked_on))
 
216
        # stack_on_pwd is often/usually our transport
 
217
        if stack_on_pwd:
 
218
            try:
 
219
                stack_on_pwd = transport.relpath(stack_on_pwd)
 
220
                if not stack_on_pwd:
 
221
                    stack_on_pwd = '.'
 
222
            except errors.PathNotChild:
 
223
                pass
 
224
        args.append(self._serialize_NoneString(stack_on_pwd))
 
225
        args.append(self._serialize_NoneString(repo_format_name))
 
226
        args.append(self._serialize_NoneTrueFalse(make_working_trees))
 
227
        args.append(self._serialize_NoneTrueFalse(shared_repo))
 
228
        request_network_name = self._network_name or \
 
229
            _mod_bzrdir.BzrDirFormat.get_default_format().network_name()
 
230
        try:
 
231
            response = client.call('BzrDirFormat.initialize_ex_1.16',
 
232
                request_network_name, path, *args)
 
233
        except errors.UnknownSmartMethod:
 
234
            client._medium._remember_remote_is_before((1,16))
 
235
            local_dir_format = _mod_bzrdir.BzrDirMetaFormat1()
 
236
            self._supply_sub_formats_to(local_dir_format)
 
237
            return local_dir_format.initialize_on_transport_ex(transport,
 
238
                use_existing_dir=use_existing_dir, create_prefix=create_prefix,
 
239
                force_new_repo=force_new_repo, stacked_on=stacked_on,
 
240
                stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
 
241
                make_working_trees=make_working_trees, shared_repo=shared_repo,
 
242
                vfs_only=True)
 
243
        except errors.ErrorFromSmartServer, err:
 
244
            _translate_error(err, path=path)
 
245
        repo_path = response[0]
 
246
        bzrdir_name = response[6]
 
247
        require_stacking = response[7]
 
248
        require_stacking = self.parse_NoneTrueFalse(require_stacking)
 
249
        format = RemoteBzrDirFormat()
 
250
        format._network_name = bzrdir_name
 
251
        self._supply_sub_formats_to(format)
 
252
        bzrdir = RemoteBzrDir(transport, format, _client=client)
 
253
        if repo_path:
 
254
            repo_format = response_tuple_to_repo_format(response[1:])
 
255
            if repo_path == '.':
 
256
                repo_path = ''
 
257
            if repo_path:
 
258
                repo_bzrdir_format = RemoteBzrDirFormat()
 
259
                repo_bzrdir_format._network_name = response[5]
 
260
                repo_bzr = RemoteBzrDir(transport.clone(repo_path),
 
261
                    repo_bzrdir_format)
 
262
            else:
 
263
                repo_bzr = bzrdir
 
264
            final_stack = response[8] or None
 
265
            final_stack_pwd = response[9] or None
 
266
            if final_stack_pwd:
 
267
                final_stack_pwd = urlutils.join(
 
268
                    transport.base, final_stack_pwd)
 
269
            remote_repo = RemoteRepository(repo_bzr, repo_format)
 
270
            if len(response) > 10:
 
271
                # Updated server verb that locks remotely.
 
272
                repo_lock_token = response[10] or None
 
273
                remote_repo.lock_write(repo_lock_token, _skip_rpc=True)
 
274
                if repo_lock_token:
 
275
                    remote_repo.dont_leave_lock_in_place()
 
276
            else:
 
277
                remote_repo.lock_write()
 
278
            policy = _mod_bzrdir.UseExistingRepository(remote_repo, final_stack,
 
279
                final_stack_pwd, require_stacking)
 
280
            policy.acquire_repository()
 
281
        else:
 
282
            remote_repo = None
 
283
            policy = None
 
284
        bzrdir._format.set_branch_format(self.get_branch_format())
 
285
        if require_stacking:
 
286
            # The repo has already been created, but we need to make sure that
 
287
            # we'll make a stackable branch.
 
288
            bzrdir._format.require_stacking(_skip_repo=True)
 
289
        return remote_repo, bzrdir, require_stacking, policy
 
290
 
 
291
    def _open(self, transport):
 
292
        return RemoteBzrDir(transport, self)
 
293
 
 
294
    def __eq__(self, other):
 
295
        if not isinstance(other, RemoteBzrDirFormat):
 
296
            return False
 
297
        return self.get_format_description() == other.get_format_description()
 
298
 
 
299
    def __return_repository_format(self):
 
300
        # Always return a RemoteRepositoryFormat object, but if a specific bzr
 
301
        # repository format has been asked for, tell the RemoteRepositoryFormat
 
302
        # that it should use that for init() etc.
 
303
        result = RemoteRepositoryFormat()
 
304
        custom_format = getattr(self, '_repository_format', None)
 
305
        if custom_format:
 
306
            if isinstance(custom_format, RemoteRepositoryFormat):
 
307
                return custom_format
 
308
            else:
 
309
                # We will use the custom format to create repositories over the
 
310
                # wire; expose its details like rich_root_data for code to
 
311
                # query
 
312
                result._custom_format = custom_format
 
313
        return result
 
314
 
 
315
    def get_branch_format(self):
 
316
        result = _mod_bzrdir.BzrDirMetaFormat1.get_branch_format(self)
 
317
        if not isinstance(result, RemoteBranchFormat):
 
318
            new_result = RemoteBranchFormat()
 
319
            new_result._custom_format = result
 
320
            # cache the result
 
321
            self.set_branch_format(new_result)
 
322
            result = new_result
 
323
        return result
 
324
 
 
325
    repository_format = property(__return_repository_format,
 
326
        _mod_bzrdir.BzrDirMetaFormat1._set_repository_format) #.im_func)
 
327
 
 
328
 
 
329
class RemoteBzrDir(_mod_bzrdir.BzrDir, _RpcHelper):
95
330
    """Control directory on a remote server, accessed via bzr:// or similar."""
96
331
 
97
332
    def __init__(self, transport, format, _client=None, _force_probe=False):
100
335
        :param _client: Private parameter for testing. Disables probing and the
101
336
            use of a real bzrdir.
102
337
        """
103
 
        BzrDir.__init__(self, transport, format)
 
338
        _mod_bzrdir.BzrDir.__init__(self, transport, format)
104
339
        # this object holds a delegated bzrdir that uses file-level operations
105
340
        # to talk to the other side
106
341
        self._real_bzrdir = None
166
401
                import traceback
167
402
                warning('VFS BzrDir access triggered\n%s',
168
403
                    ''.join(traceback.format_stack()))
169
 
            self._real_bzrdir = BzrDir.open_from_transport(
 
404
            self._real_bzrdir = _mod_bzrdir.BzrDir.open_from_transport(
170
405
                self.root_transport, _server_formats=False)
171
406
            self._format._network_name = \
172
407
                self._real_bzrdir._format.network_name()
178
413
        # Prevent aliasing problems in the next_open_branch_result cache.
179
414
        # See create_branch for rationale.
180
415
        self._next_open_branch_result = None
181
 
        return BzrDir.break_lock(self)
 
416
        return _mod_bzrdir.BzrDir.break_lock(self)
182
417
 
183
418
    def _vfs_cloning_metadir(self, require_stacking=False):
184
419
        self._ensure_real()
217
452
        branch_ref, branch_name = branch_info
218
453
        format = controldir.network_format_registry.get(control_name)
219
454
        if repo_name:
220
 
            format.repository_format = repository.network_format_registry.get(
 
455
            format.repository_format = _mod_repository.network_format_registry.get(
221
456
                repo_name)
222
457
        if branch_ref == 'ref':
223
458
            # XXX: we need possible_transports here to avoid reopening the
224
459
            # connection to the referenced location
225
 
            ref_bzrdir = BzrDir.open(branch_name)
 
460
            ref_bzrdir = _mod_bzrdir.BzrDir.open(branch_name)
226
461
            branch_format = ref_bzrdir.cloning_metadir().get_branch_format()
227
462
            format.set_branch_format(branch_format)
228
463
        elif branch_ref == 'branch':
465
700
        return RemoteBzrDirConfig(self)
466
701
 
467
702
 
468
 
class RemoteRepositoryFormat(repository.RepositoryFormat):
 
703
class RemoteRepositoryFormat(_mod_repository.RepositoryFormat):
469
704
    """Format for repositories accessed over a _SmartClient.
470
705
 
471
706
    Instances of this repository are represented by RemoteRepository
490
725
    supports_leaving_lock = True
491
726
 
492
727
    def __init__(self):
493
 
        repository.RepositoryFormat.__init__(self)
 
728
        _mod_repository.RepositoryFormat.__init__(self)
494
729
        self._custom_format = None
495
730
        self._network_name = None
496
731
        self._creating_bzrdir = None
587
822
            network_name = self._network_name
588
823
        else:
589
824
            # Select the current bzrlib default and ask for that.
590
 
            reference_bzrdir_format = bzrdir.format_registry.get('default')()
 
825
            reference_bzrdir_format = _mod_bzrdir.format_registry.get('default')()
591
826
            reference_format = reference_bzrdir_format.repository_format
592
827
            network_name = reference_format.network_name()
593
828
        # 2) try direct creation via RPC
619
854
 
620
855
    def _ensure_real(self):
621
856
        if self._custom_format is None:
622
 
            self._custom_format = repository.network_format_registry.get(
 
857
            self._custom_format = _mod_repository.network_format_registry.get(
623
858
                self._network_name)
624
859
 
625
860
    @property
721
956
        # transport, but I'm not sure it's worth making this method
722
957
        # optional -- mbp 2010-04-21
723
958
        return self.bzrdir.get_repository_transport(None)
724
 
        
 
959
 
725
960
    def __str__(self):
726
961
        return "%s(%s)" % (self.__class__.__name__, self.base)
727
962
 
861
1096
        """Private method for using with old (< 1.2) servers to fallback."""
862
1097
        if revision_id is None:
863
1098
            revision_id = ''
864
 
        elif revision.is_null(revision_id):
 
1099
        elif _mod_revision.is_null(revision_id):
865
1100
            return {}
866
1101
 
867
1102
        path = self.bzrdir._path_for_remote_call(self._client)
948
1183
        """See Repository.gather_stats()."""
949
1184
        path = self.bzrdir._path_for_remote_call(self._client)
950
1185
        # revid can be None to indicate no revisions, not just NULL_REVISION
951
 
        if revid is None or revision.is_null(revid):
 
1186
        if revid is None or _mod_revision.is_null(revid):
952
1187
            fmt_revid = ''
953
1188
        else:
954
1189
            fmt_revid = revid
1375
1610
                    'revision_ids is mutually exclusive with revision_id')
1376
1611
            if revision_id is not None:
1377
1612
                revision_ids = [revision_id]
1378
 
        inter_repo = repository.InterRepository.get(other, self)
 
1613
        inter_repo = _mod_repository.InterRepository.get(other, self)
1379
1614
        return inter_repo.search_missing_revision_ids(
1380
1615
            find_ghosts=find_ghosts, revision_ids=revision_ids,
1381
1616
            if_present_ids=if_present_ids)
1397
1632
            # check that last_revision is in 'from' and then return a
1398
1633
            # no-operation.
1399
1634
            if (revision_id is not None and
1400
 
                not revision.is_null(revision_id)):
 
1635
                not _mod_revision.is_null(revision_id)):
1401
1636
                self.get_revision(revision_id)
1402
1637
            return 0, []
1403
1638
        # if there is no specific appropriate InterRepository, this will get
1404
1639
        # the InterRepository base class, which raises an
1405
1640
        # IncompatibleRepositories when asked to fetch.
1406
 
        inter = repository.InterRepository.get(source, self)
 
1641
        inter = _mod_repository.InterRepository.get(source, self)
1407
1642
        return inter.fetch(revision_id=revision_id,
1408
1643
            find_ghosts=find_ghosts, fetch_spec=fetch_spec)
1409
1644
 
1634
1869
            tmpdir = osutils.mkdtemp()
1635
1870
            try:
1636
1871
                _extract_tar(tar, tmpdir)
1637
 
                tmp_bzrdir = BzrDir.open(tmpdir)
 
1872
                tmp_bzrdir = _mod_bzrdir.BzrDir.open(tmpdir)
1638
1873
                tmp_repo = tmp_bzrdir.open_repository()
1639
1874
                tmp_repo.copy_content_into(destination, revision_id)
1640
1875
            finally:
1801
2036
            raise errors.UnexpectedSmartServerResponse(response)
1802
2037
 
1803
2038
 
1804
 
class RemoteStreamSink(repository.StreamSink):
 
2039
class RemoteStreamSink(_mod_repository.StreamSink):
1805
2040
 
1806
2041
    def _insert_real(self, stream, src_format, resume_tokens):
1807
2042
        self.target_repo._ensure_real()
1908
2143
        self._last_substream and self._last_stream so that the stream can be
1909
2144
        resumed by _resume_stream_with_vfs.
1910
2145
        """
1911
 
                    
 
2146
 
1912
2147
        stream_iter = iter(stream)
1913
2148
        for substream_kind, substream in stream_iter:
1914
2149
            if substream_kind == 'inventory-deltas':
1917
2152
                return
1918
2153
            else:
1919
2154
                yield substream_kind, substream
1920
 
            
1921
 
 
1922
 
class RemoteStreamSource(repository.StreamSource):
 
2155
 
 
2156
 
 
2157
class RemoteStreamSource(_mod_repository.StreamSource):
1923
2158
    """Stream data from a remote server."""
1924
2159
 
1925
2160
    def get_stream(self, search):
2132
2367
            network_name = self._custom_format.network_name()
2133
2368
        else:
2134
2369
            # Select the current bzrlib default and ask for that.
2135
 
            reference_bzrdir_format = bzrdir.format_registry.get('default')()
 
2370
            reference_bzrdir_format = _mod_bzrdir.format_registry.get('default')()
2136
2371
            reference_format = reference_bzrdir_format.get_branch_format()
2137
2372
            self._custom_format = reference_format
2138
2373
            network_name = reference_format.network_name()
2422
2657
            self._is_stacked = False
2423
2658
        else:
2424
2659
            self._is_stacked = True
2425
 
        
 
2660
 
2426
2661
    def _vfs_get_tags_bytes(self):
2427
2662
        self._ensure_real()
2428
2663
        return self._real_branch._get_tags_bytes()
2763
2998
        # XXX: These should be returned by the set_last_revision_info verb
2764
2999
        old_revno, old_revid = self.last_revision_info()
2765
3000
        self._run_pre_change_branch_tip_hooks(revno, revision_id)
2766
 
        revision_id = ensure_null(revision_id)
 
3001
        revision_id = _mod_revision.ensure_null(revision_id)
2767
3002
        try:
2768
3003
            response = self._call('Branch.set_last_revision_info',
2769
3004
                self._remote_path(), self._lock_token, self._repo_lock_token,