/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to brzlib/smart/bzrdir.py

  • Committer: Jelmer Vernooij
  • Date: 2017-05-21 12:41:27 UTC
  • mto: This revision was merged to the branch mainline in revision 6623.
  • Revision ID: jelmer@jelmer.uk-20170521124127-iv8etg0vwymyai6y
s/bzr/brz/ in apport config.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Server-side bzrdir related request implmentations."""
18
18
 
19
 
from ... import (
 
19
from __future__ import absolute_import
 
20
 
 
21
from brzlib import (
20
22
    bencode,
21
23
    branch,
22
24
    errors,
23
25
    repository,
24
26
    urlutils,
25
27
    )
26
 
from .. import (
27
 
    BzrProber,
28
 
    )
29
 
from ..bzrdir import (
 
28
from brzlib.bzrdir import (
30
29
    BzrDir,
31
30
    BzrDirFormat,
 
31
    BzrProber,
32
32
    )
33
 
from ...controldir import (
 
33
from brzlib.controldir import (
34
34
    network_format_registry,
35
35
    )
36
 
from .request import (
 
36
from brzlib.smart.request import (
37
37
    FailedSmartServerResponse,
38
38
    SmartServerRequest,
39
39
    SuccessfulSmartServerResponse,
51
51
            # Ideally we'd return a FailedSmartServerResponse here rather than
52
52
            # a "successful" negative, but we want to be compatibile with
53
53
            # clients that don't anticipate errors from this method.
54
 
            answer = b'no'
 
54
            answer = 'no'
55
55
        else:
56
56
            bzr_prober = BzrProber()
57
57
            try:
58
58
                bzr_prober.probe_transport(t)
59
59
            except (errors.NotBranchError, errors.UnknownFormatError):
60
 
                answer = b'no'
 
60
                answer = 'no'
61
61
            else:
62
 
                answer = b'yes'
 
62
                answer = 'yes'
63
63
        return SuccessfulSmartServerResponse((answer,))
64
64
 
65
65
 
75
75
        except errors.PathNotChild:
76
76
            # The client is trying to ask about a path that they have no access
77
77
            # to.
78
 
            return SuccessfulSmartServerResponse((b'no',))
 
78
            return SuccessfulSmartServerResponse(('no',))
79
79
        try:
80
80
            bd = BzrDir.open_from_transport(t)
81
81
        except errors.NotBranchError:
82
 
            answer = (b'no',)
 
82
            answer = ('no',)
83
83
        else:
84
 
            answer = (b'yes',)
 
84
            answer = ('yes',)
85
85
            if bd.has_workingtree():
86
 
                answer += (b'yes',)
 
86
                answer += ('yes',)
87
87
            else:
88
 
                answer += (b'no',)
 
88
                answer += ('no',)
89
89
        return SuccessfulSmartServerResponse(answer)
90
90
 
91
91
 
96
96
        try:
97
97
            self._bzrdir = BzrDir.open_from_transport(
98
98
                self.transport_from_client_path(path))
99
 
        except errors.NotBranchError as e:
100
 
            return FailedSmartServerResponse((b'nobranch',))
 
99
        except errors.NotBranchError, e:
 
100
            return FailedSmartServerResponse(('nobranch',))
101
101
        return self.do_bzrdir_request(*args)
102
102
 
103
103
    def _boolean_to_yes_no(self, a_boolean):
104
104
        if a_boolean:
105
 
            return b'yes'
 
105
            return 'yes'
106
106
        else:
107
 
            return b'no'
 
107
            return 'no'
108
108
 
109
109
    def _format_to_capabilities(self, repo_format):
110
110
        rich_root = self._boolean_to_yes_no(repo_format.rich_root_data)
136
136
        :return: On success, 'ok'.
137
137
        """
138
138
        try:
139
 
            self._bzrdir.destroy_branch(
140
 
                name.decode('utf-8') if name is not None else None)
141
 
        except errors.NotBranchError as e:
142
 
            return FailedSmartServerResponse((b'nobranch',))
143
 
        return SuccessfulSmartServerResponse((b'ok',))
 
139
            self._bzrdir.destroy_branch(name)
 
140
        except errors.NotBranchError, e:
 
141
            return FailedSmartServerResponse(('nobranch',))
 
142
        return SuccessfulSmartServerResponse(('ok',))
144
143
 
145
144
 
146
145
class SmartServerBzrDirRequestHasWorkingTree(SmartServerRequestBzrDir):
154
153
            Otherwise 'no'.
155
154
        """
156
155
        if self._bzrdir.has_workingtree():
157
 
            return SuccessfulSmartServerResponse((b'yes', ))
 
156
            return SuccessfulSmartServerResponse(('yes', ))
158
157
        else:
159
 
            return SuccessfulSmartServerResponse((b'no', ))
 
158
            return SuccessfulSmartServerResponse(('no', ))
160
159
 
161
160
 
162
161
class SmartServerBzrDirRequestDestroyRepository(SmartServerRequestBzrDir):
170
169
        """
171
170
        try:
172
171
            self._bzrdir.destroy_repository()
173
 
        except errors.NoRepositoryPresent as e:
174
 
            return FailedSmartServerResponse((b'norepository',))
175
 
        return SuccessfulSmartServerResponse((b'ok',))
 
172
        except errors.NoRepositoryPresent, e:
 
173
            return FailedSmartServerResponse(('norepository',))
 
174
        return SuccessfulSmartServerResponse(('ok',))
176
175
 
177
176
 
178
177
class SmartServerBzrDirRequestCloningMetaDir(SmartServerRequestBzrDir):
181
180
        """Get the format that should be used when cloning from this dir.
182
181
 
183
182
        New in 1.13.
184
 
 
 
183
        
185
184
        :return: on success, a 3-tuple of network names for (control,
186
185
            repository, branch) directories, where '' signifies "not present".
187
186
            If this BzrDir contains a branch reference then this will fail with
196
195
            # The server shouldn't try to resolve references, and it quite
197
196
            # possibly can't reach them anyway.  The client needs to resolve
198
197
            # the branch reference to determine the cloning_metadir.
199
 
            return FailedSmartServerResponse((b'BranchReference',))
200
 
        if require_stacking == b"True":
 
198
            return FailedSmartServerResponse(('BranchReference',))
 
199
        if require_stacking == "True":
201
200
            require_stacking = True
202
201
        else:
203
202
            require_stacking = False
205
204
            require_stacking=require_stacking)
206
205
        control_name = control_format.network_name()
207
206
        if not control_format.fixed_components:
208
 
            branch_name = (b'branch',
209
 
                           control_format.get_branch_format().network_name())
 
207
            branch_name = ('branch',
 
208
                control_format.get_branch_format().network_name())
210
209
            repository_name = control_format.repository_format.network_name()
211
210
        else:
212
211
            # Only MetaDir has delegated formats today.
213
 
            branch_name = (b'branch', b'')
214
 
            repository_name = b''
 
212
            branch_name = ('branch', '')
 
213
            repository_name = ''
215
214
        return SuccessfulSmartServerResponse((control_name, repository_name,
216
 
                                              branch_name))
 
215
            branch_name))
217
216
 
218
217
 
219
218
class SmartServerBzrDirRequestCheckoutMetaDir(SmartServerRequestBzrDir):
238
237
            # The server shouldn't try to resolve references, and it quite
239
238
            # possibly can't reach them anyway.  The client needs to resolve
240
239
            # the branch reference to determine the cloning_metadir.
241
 
            return FailedSmartServerResponse((b'BranchReference',))
 
240
            return FailedSmartServerResponse(('BranchReference',))
242
241
        control_format = self._bzrdir.checkout_metadir()
243
242
        control_name = control_format.network_name()
244
243
        if not control_format.fixed_components:
245
244
            branch_name = control_format.get_branch_format().network_name()
246
245
            repo_name = control_format.repository_format.network_name()
247
246
        else:
248
 
            branch_name = b''
249
 
            repo_name = b''
 
247
            branch_name = ''
 
248
            repo_name = ''
250
249
        return SuccessfulSmartServerResponse(
251
250
            (control_name, repo_name, branch_name))
252
251
 
281
280
        branch_format = result._format.network_name()
282
281
        repo_format = result.repository._format.network_name()
283
282
        repo_path = self._repo_relpath(bzrdir.root_transport,
284
 
                                       result.repository)
 
283
            result.repository)
285
284
        # branch format, repo relpath, rich_root, tree_ref, external_lookup,
286
285
        # repo_network_name
287
 
        return SuccessfulSmartServerResponse((b'ok', branch_format, repo_path,
288
 
                                              rich_root, tree_ref, external_lookup, repo_format))
 
286
        return SuccessfulSmartServerResponse(('ok', branch_format, repo_path,
 
287
            rich_root, tree_ref, external_lookup, repo_format))
289
288
 
290
289
 
291
290
class SmartServerRequestCreateRepository(SmartServerRequestBzrDir):
311
310
        """
312
311
        bzrdir = BzrDir.open_from_transport(
313
312
            self.transport_from_client_path(path))
314
 
        shared = shared == b'True'
 
313
        shared = shared == 'True'
315
314
        format = repository.network_format_registry.get(network_name)
316
315
        bzrdir.repository_format = format
317
316
        result = format.initialize(bzrdir, shared=shared)
318
317
        rich_root, tree_ref, external_lookup = self._format_to_capabilities(
319
318
            result._format)
320
 
        return SuccessfulSmartServerResponse((b'ok', rich_root, tree_ref,
321
 
                                              external_lookup, result._format.network_name()))
 
319
        return SuccessfulSmartServerResponse(('ok', rich_root, tree_ref,
 
320
            external_lookup, result._format.network_name()))
322
321
 
323
322
 
324
323
class SmartServerRequestFindRepository(SmartServerRequestBzrDir):
363
362
        """
364
363
        try:
365
364
            path, rich_root, tree_ref, external_lookup, name = self._find(path)
366
 
            return SuccessfulSmartServerResponse((b'ok', path.encode('utf-8'), rich_root, tree_ref))
 
365
            return SuccessfulSmartServerResponse(('ok', path, rich_root, tree_ref))
367
366
        except errors.NoRepositoryPresent:
368
 
            return FailedSmartServerResponse((b'norepository', ))
 
367
            return FailedSmartServerResponse(('norepository', ))
369
368
 
370
369
 
371
370
class SmartServerRequestFindRepositoryV2(SmartServerRequestFindRepository):
388
387
        try:
389
388
            path, rich_root, tree_ref, external_lookup, name = self._find(path)
390
389
            return SuccessfulSmartServerResponse(
391
 
                (b'ok', path.encode('utf-8'), rich_root, tree_ref, external_lookup))
 
390
                ('ok', path, rich_root, tree_ref, external_lookup))
392
391
        except errors.NoRepositoryPresent:
393
 
            return FailedSmartServerResponse((b'norepository', ))
 
392
            return FailedSmartServerResponse(('norepository', ))
394
393
 
395
394
 
396
395
class SmartServerRequestFindRepositoryV3(SmartServerRequestFindRepository):
412
411
        try:
413
412
            path, rich_root, tree_ref, external_lookup, name = self._find(path)
414
413
            return SuccessfulSmartServerResponse(
415
 
                (b'ok', path.encode('utf-8'), rich_root, tree_ref, external_lookup, name))
 
414
                ('ok', path, rich_root, tree_ref, external_lookup, name))
416
415
        except errors.NoRepositoryPresent:
417
 
            return FailedSmartServerResponse((b'norepository', ))
 
416
            return FailedSmartServerResponse(('norepository', ))
418
417
 
419
418
 
420
419
class SmartServerBzrDirRequestConfigFile(SmartServerRequestBzrDir):
421
420
 
422
421
    def do_bzrdir_request(self):
423
422
        """Get the configuration bytes for a config file in bzrdir.
424
 
 
 
423
        
425
424
        The body is not utf8 decoded - it is the literal bytestream from disk.
426
425
        """
427
426
        config = self._bzrdir._get_config()
428
427
        if config is None:
429
 
            content = b''
 
428
            content = ''
430
429
        else:
431
430
            content = config._get_config_file().read()
432
431
        return SuccessfulSmartServerResponse((), content)
436
435
 
437
436
    def do_bzrdir_request(self):
438
437
        """Get the branches in a control directory.
439
 
 
 
438
        
440
439
        The body is a bencoded dictionary, with values similar to the return
441
440
        value of the open branch request.
442
441
        """
443
442
        branches = self._bzrdir.get_branches()
444
443
        ret = {}
445
 
        for name, b in branches.items():
 
444
        for name, b in branches.iteritems():
446
445
            if name is None:
447
 
                name = b""
448
 
            ret[name.encode('utf-8')] = (b"branch", b._format.network_name())
 
446
                name = ""
 
447
            ret[name] = ("branch", b._format.network_name())
449
448
        return SuccessfulSmartServerResponse(
450
 
            (b"success", ), bencode.bencode(ret))
 
449
            ("success", ), bencode.bencode(ret))
451
450
 
452
451
 
453
452
class SmartServerRequestInitializeBzrDir(SmartServerRequest):
460
459
        """
461
460
        target_transport = self.transport_from_client_path(path)
462
461
        BzrDirFormat.get_default_format().initialize_on_transport(target_transport)
463
 
        return SuccessfulSmartServerResponse((b'ok', ))
 
462
        return SuccessfulSmartServerResponse(('ok', ))
464
463
 
465
464
 
466
465
class SmartServerRequestBzrDirInitializeEx(SmartServerRequestBzrDir):
468
467
    def parse_NoneTrueFalse(self, arg):
469
468
        if not arg:
470
469
            return None
471
 
        if arg == b'False':
 
470
        if arg == 'False':
472
471
            return False
473
 
        if arg == b'True':
 
472
        if arg == 'True':
474
473
            return True
475
474
        raise AssertionError("invalid arg %r" % arg)
476
475
 
477
 
    def parse_NoneBytestring(self, arg):
 
476
    def parse_NoneString(self, arg):
478
477
        return arg or None
479
478
 
480
 
    def parse_NoneString(self, arg):
481
 
        if not arg:
482
 
            return None
483
 
        return arg.decode('utf-8')
484
 
 
485
479
    def _serialize_NoneTrueFalse(self, arg):
486
480
        if arg is False:
487
 
            return b'False'
 
481
            return 'False'
488
482
        if not arg:
489
 
            return b''
490
 
        return b'True'
 
483
            return ''
 
484
        return 'True'
491
485
 
492
486
    def do(self, bzrdir_network_name, path, use_existing_dir, create_prefix,
493
 
           force_new_repo, stacked_on, stack_on_pwd, repo_format_name,
494
 
           make_working_trees, shared_repo):
 
487
        force_new_repo, stacked_on, stack_on_pwd, repo_format_name,
 
488
        make_working_trees, shared_repo):
495
489
        """Initialize a bzrdir at path as per
496
490
        BzrDirFormat.initialize_on_transport_ex.
497
491
 
512
506
        stack_on_pwd = self.parse_NoneString(stack_on_pwd)
513
507
        make_working_trees = self.parse_NoneTrueFalse(make_working_trees)
514
508
        shared_repo = self.parse_NoneTrueFalse(shared_repo)
515
 
        if stack_on_pwd == b'.':
516
 
            stack_on_pwd = target_transport.base.encode('utf-8')
517
 
        repo_format_name = self.parse_NoneBytestring(repo_format_name)
 
509
        if stack_on_pwd == '.':
 
510
            stack_on_pwd = target_transport.base
 
511
        repo_format_name = self.parse_NoneString(repo_format_name)
518
512
        repo, bzrdir, stacking, repository_policy = \
519
513
            format.initialize_on_transport_ex(target_transport,
520
 
                                              use_existing_dir=use_existing_dir, create_prefix=create_prefix,
521
 
                                              force_new_repo=force_new_repo, stacked_on=stacked_on,
522
 
                                              stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
523
 
                                              make_working_trees=make_working_trees, shared_repo=shared_repo)
 
514
            use_existing_dir=use_existing_dir, create_prefix=create_prefix,
 
515
            force_new_repo=force_new_repo, stacked_on=stacked_on,
 
516
            stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
 
517
            make_working_trees=make_working_trees, shared_repo=shared_repo)
524
518
        if repo is None:
525
519
            repo_path = ''
526
 
            repo_name = b''
527
 
            rich_root = tree_ref = external_lookup = b''
528
 
            repo_bzrdir_name = b''
 
520
            repo_name = ''
 
521
            rich_root = tree_ref = external_lookup = ''
 
522
            repo_bzrdir_name = ''
529
523
            final_stack = None
530
524
            final_stack_pwd = None
531
 
            repo_lock_token = b''
 
525
            repo_lock_token = ''
532
526
        else:
533
527
            repo_path = self._repo_relpath(bzrdir.root_transport, repo)
534
528
            if repo_path == '':
536
530
            rich_root, tree_ref, external_lookup = self._format_to_capabilities(
537
531
                repo._format)
538
532
            repo_name = repo._format.network_name()
539
 
            repo_bzrdir_name = repo.controldir._format.network_name()
 
533
            repo_bzrdir_name = repo.bzrdir._format.network_name()
540
534
            final_stack = repository_policy._stack_on
541
535
            final_stack_pwd = repository_policy._stack_on_pwd
542
536
            # It is returned locked, but we need to do the lock to get the lock
543
537
            # token.
544
538
            repo.unlock()
545
 
            repo_lock_token = repo.lock_write().repository_token or b''
 
539
            repo_lock_token = repo.lock_write().repository_token or ''
546
540
            if repo_lock_token:
547
541
                repo.leave_lock_in_place()
548
542
            repo.unlock()
561
555
                self._root_client_path, client_path)
562
556
            final_stack_pwd = '.'
563
557
 
564
 
        return SuccessfulSmartServerResponse((repo_path.encode('utf-8'),
565
 
                                              rich_root, tree_ref, external_lookup, repo_name, repo_bzrdir_name,
566
 
                                              bzrdir._format.network_name(),
567
 
                                              self._serialize_NoneTrueFalse(
568
 
                                                  stacking), final_stack.encode('utf-8'),
569
 
                                              final_stack_pwd.encode('utf-8'), repo_lock_token))
 
558
        return SuccessfulSmartServerResponse((repo_path, rich_root, tree_ref,
 
559
            external_lookup, repo_name, repo_bzrdir_name,
 
560
            bzrdir._format.network_name(),
 
561
            self._serialize_NoneTrueFalse(stacking), final_stack,
 
562
            final_stack_pwd, repo_lock_token))
570
563
 
571
564
 
572
565
class SmartServerRequestOpenBranch(SmartServerRequestBzrDir):
576
569
        try:
577
570
            reference_url = self._bzrdir.get_branch_reference()
578
571
            if reference_url is None:
579
 
                reference_url = ''
580
 
            return SuccessfulSmartServerResponse((b'ok', reference_url.encode('utf-8')))
581
 
        except errors.NotBranchError as e:
582
 
            return FailedSmartServerResponse((b'nobranch',))
 
572
                return SuccessfulSmartServerResponse(('ok', ''))
 
573
            else:
 
574
                return SuccessfulSmartServerResponse(('ok', reference_url))
 
575
        except errors.NotBranchError, e:
 
576
            return FailedSmartServerResponse(('nobranch',))
583
577
 
584
578
 
585
579
class SmartServerRequestOpenBranchV2(SmartServerRequestBzrDir):
591
585
            if reference_url is None:
592
586
                br = self._bzrdir.open_branch(ignore_fallbacks=True)
593
587
                format = br._format.network_name()
594
 
                return SuccessfulSmartServerResponse((b'branch', format))
 
588
                return SuccessfulSmartServerResponse(('branch', format))
595
589
            else:
596
 
                return SuccessfulSmartServerResponse((b'ref', reference_url.encode('utf-8')))
597
 
        except errors.NotBranchError as e:
598
 
            return FailedSmartServerResponse((b'nobranch',))
 
590
                return SuccessfulSmartServerResponse(('ref', reference_url))
 
591
        except errors.NotBranchError, e:
 
592
            return FailedSmartServerResponse(('nobranch',))
599
593
 
600
594
 
601
595
class SmartServerRequestOpenBranchV3(SmartServerRequestBzrDir):
602
596
 
603
597
    def do_bzrdir_request(self):
604
598
        """Open a branch at path and return the reference or format.
605
 
 
 
599
        
606
600
        This version introduced in 2.1.
607
601
 
608
602
        Differences to SmartServerRequestOpenBranchV2:
615
609
            if reference_url is None:
616
610
                br = self._bzrdir.open_branch(ignore_fallbacks=True)
617
611
                format = br._format.network_name()
618
 
                return SuccessfulSmartServerResponse((b'branch', format))
 
612
                return SuccessfulSmartServerResponse(('branch', format))
619
613
            else:
620
 
                return SuccessfulSmartServerResponse((b'ref', reference_url.encode('utf-8')))
621
 
        except errors.NotBranchError as e:
 
614
                return SuccessfulSmartServerResponse(('ref', reference_url))
 
615
        except errors.NotBranchError, e:
622
616
            # Stringify the exception so that its .detail attribute will be
623
617
            # filled out.
624
618
            str(e)
625
 
            resp = (b'nobranch',)
 
619
            resp = ('nobranch',)
626
620
            detail = e.detail
627
621
            if detail:
628
622
                if detail.startswith(': '):
629
623
                    detail = detail[2:]
630
 
                resp += (detail.encode('utf-8'),)
 
624
                resp += (detail,)
631
625
            return FailedSmartServerResponse(resp)
 
626