/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 bzrlib/smart/bzrdir.py

  • Committer: Robert Collins
  • Date: 2010-05-06 11:08:10 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506110810-h3j07fh5gmw54s25
Cleaner matcher matching revised unlocking protocol.

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 __future__ import absolute_import
20
19
 
21
 
from ... import (
22
 
    bencode,
23
 
    branch,
24
 
    errors,
25
 
    repository,
26
 
    urlutils,
27
 
    )
28
 
from .. import (
29
 
    BzrProber,
30
 
    )
31
 
from ..bzrdir import (
 
20
from bzrlib import branch, errors, repository, urlutils
 
21
from bzrlib.bzrdir import (
32
22
    BzrDir,
33
23
    BzrDirFormat,
34
 
    )
35
 
from ...controldir import (
 
24
    BzrDirMetaFormat1,
36
25
    network_format_registry,
37
26
    )
38
 
from .request import (
 
27
from bzrlib.smart.request import (
39
28
    FailedSmartServerResponse,
40
29
    SmartServerRequest,
41
30
    SuccessfulSmartServerResponse,
53
42
            # Ideally we'd return a FailedSmartServerResponse here rather than
54
43
            # a "successful" negative, but we want to be compatibile with
55
44
            # clients that don't anticipate errors from this method.
56
 
            answer = b'no'
 
45
            answer = 'no'
57
46
        else:
58
 
            bzr_prober = BzrProber()
 
47
            default_format = BzrDirFormat.get_default_format()
 
48
            real_bzrdir = default_format.open(t, _found=True)
59
49
            try:
60
 
                bzr_prober.probe_transport(t)
 
50
                real_bzrdir._format.probe_transport(t)
61
51
            except (errors.NotBranchError, errors.UnknownFormatError):
62
 
                answer = b'no'
 
52
                answer = 'no'
63
53
            else:
64
 
                answer = b'yes'
 
54
                answer = 'yes'
65
55
        return SuccessfulSmartServerResponse((answer,))
66
56
 
67
57
 
77
67
        except errors.PathNotChild:
78
68
            # The client is trying to ask about a path that they have no access
79
69
            # to.
80
 
            return SuccessfulSmartServerResponse((b'no',))
 
70
            return SuccessfulSmartServerResponse(('no',))
81
71
        try:
82
72
            bd = BzrDir.open_from_transport(t)
83
73
        except errors.NotBranchError:
84
 
            answer = (b'no',)
 
74
            answer = ('no',)
85
75
        else:
86
 
            answer = (b'yes',)
 
76
            answer = ('yes',)
87
77
            if bd.has_workingtree():
88
 
                answer += (b'yes',)
 
78
                answer += ('yes',)
89
79
            else:
90
 
                answer += (b'no',)
 
80
                answer += ('no',)
91
81
        return SuccessfulSmartServerResponse(answer)
92
82
 
93
83
 
94
84
class SmartServerRequestBzrDir(SmartServerRequest):
95
85
 
96
86
    def do(self, path, *args):
97
 
        """Open a BzrDir at path, and return `self.do_bzrdir_request(*args)`."""
 
87
        """Open a BzrDir at path, and return self.do_bzrdir_request(*args)."""
98
88
        try:
99
89
            self._bzrdir = BzrDir.open_from_transport(
100
90
                self.transport_from_client_path(path))
101
 
        except errors.NotBranchError as e:
102
 
            return FailedSmartServerResponse((b'nobranch',))
 
91
        except errors.NotBranchError, e:
 
92
            return FailedSmartServerResponse(('nobranch',))
103
93
        return self.do_bzrdir_request(*args)
104
94
 
105
95
    def _boolean_to_yes_no(self, a_boolean):
106
96
        if a_boolean:
107
 
            return b'yes'
 
97
            return 'yes'
108
98
        else:
109
 
            return b'no'
 
99
            return 'no'
110
100
 
111
101
    def _format_to_capabilities(self, repo_format):
112
102
        rich_root = self._boolean_to_yes_no(repo_format.rich_root_data)
129
119
        return '/'.join(segments)
130
120
 
131
121
 
132
 
class SmartServerBzrDirRequestDestroyBranch(SmartServerRequestBzrDir):
133
 
 
134
 
    def do_bzrdir_request(self, name=None):
135
 
        """Destroy the branch with the specified name.
136
 
 
137
 
        New in 2.5.0.
138
 
        :return: On success, 'ok'.
139
 
        """
140
 
        try:
141
 
            self._bzrdir.destroy_branch(
142
 
                name.decode('utf-8') if name is not None else None)
143
 
        except errors.NotBranchError as e:
144
 
            return FailedSmartServerResponse((b'nobranch',))
145
 
        return SuccessfulSmartServerResponse((b'ok',))
146
 
 
147
 
 
148
 
class SmartServerBzrDirRequestHasWorkingTree(SmartServerRequestBzrDir):
149
 
 
150
 
    def do_bzrdir_request(self, name=None):
151
 
        """Check whether there is a working tree present.
152
 
 
153
 
        New in 2.5.0.
154
 
 
155
 
        :return: If there is a working tree present, 'yes'.
156
 
            Otherwise 'no'.
157
 
        """
158
 
        if self._bzrdir.has_workingtree():
159
 
            return SuccessfulSmartServerResponse((b'yes', ))
160
 
        else:
161
 
            return SuccessfulSmartServerResponse((b'no', ))
162
 
 
163
 
 
164
 
class SmartServerBzrDirRequestDestroyRepository(SmartServerRequestBzrDir):
165
 
 
166
 
    def do_bzrdir_request(self, name=None):
167
 
        """Destroy the repository.
168
 
 
169
 
        New in 2.5.0.
170
 
 
171
 
        :return: On success, 'ok'.
172
 
        """
173
 
        try:
174
 
            self._bzrdir.destroy_repository()
175
 
        except errors.NoRepositoryPresent as e:
176
 
            return FailedSmartServerResponse((b'norepository',))
177
 
        return SuccessfulSmartServerResponse((b'ok',))
178
 
 
179
 
 
180
122
class SmartServerBzrDirRequestCloningMetaDir(SmartServerRequestBzrDir):
181
123
 
182
124
    def do_bzrdir_request(self, require_stacking):
198
140
            # The server shouldn't try to resolve references, and it quite
199
141
            # possibly can't reach them anyway.  The client needs to resolve
200
142
            # the branch reference to determine the cloning_metadir.
201
 
            return FailedSmartServerResponse((b'BranchReference',))
202
 
        if require_stacking == b"True":
 
143
            return FailedSmartServerResponse(('BranchReference',))
 
144
        if require_stacking == "True":
203
145
            require_stacking = True
204
146
        else:
205
147
            require_stacking = False
206
148
        control_format = self._bzrdir.cloning_metadir(
207
149
            require_stacking=require_stacking)
208
150
        control_name = control_format.network_name()
209
 
        if not control_format.fixed_components:
210
 
            branch_name = (b'branch',
 
151
        # XXX: There should be a method that tells us that the format does/does
 
152
        # not have subformats.
 
153
        if isinstance(control_format, BzrDirMetaFormat1):
 
154
            branch_name = ('branch',
211
155
                control_format.get_branch_format().network_name())
212
156
            repository_name = control_format.repository_format.network_name()
213
157
        else:
214
158
            # Only MetaDir has delegated formats today.
215
 
            branch_name = (b'branch', b'')
216
 
            repository_name = b''
 
159
            branch_name = ('branch', '')
 
160
            repository_name = ''
217
161
        return SuccessfulSmartServerResponse((control_name, repository_name,
218
162
            branch_name))
219
163
 
220
164
 
221
 
class SmartServerBzrDirRequestCheckoutMetaDir(SmartServerRequestBzrDir):
222
 
    """Get the format to use for checkouts.
223
 
 
224
 
    New in 2.5.
225
 
 
226
 
    :return: on success, a 3-tuple of network names for (control,
227
 
        repository, branch) directories, where '' signifies "not present".
228
 
        If this BzrDir contains a branch reference then this will fail with
229
 
        BranchReference; clients should resolve branch references before
230
 
        calling this RPC (they should not try to create a checkout of a
231
 
        checkout).
232
 
    """
233
 
 
234
 
    def do_bzrdir_request(self):
235
 
        try:
236
 
            branch_ref = self._bzrdir.get_branch_reference()
237
 
        except errors.NotBranchError:
238
 
            branch_ref = None
239
 
        if branch_ref is not None:
240
 
            # The server shouldn't try to resolve references, and it quite
241
 
            # possibly can't reach them anyway.  The client needs to resolve
242
 
            # the branch reference to determine the cloning_metadir.
243
 
            return FailedSmartServerResponse((b'BranchReference',))
244
 
        control_format = self._bzrdir.checkout_metadir()
245
 
        control_name = control_format.network_name()
246
 
        if not control_format.fixed_components:
247
 
            branch_name = control_format.get_branch_format().network_name()
248
 
            repo_name = control_format.repository_format.network_name()
249
 
        else:
250
 
            branch_name = b''
251
 
            repo_name = b''
252
 
        return SuccessfulSmartServerResponse(
253
 
            (control_name, repo_name, branch_name))
254
 
 
255
 
 
256
165
class SmartServerRequestCreateBranch(SmartServerRequestBzrDir):
257
166
 
258
167
    def do(self, path, network_name):
270
179
 
271
180
        :param path: The path to the bzrdir.
272
181
        :param network_name: The network name of the branch type to create.
273
 
        :return: ('ok', branch_format, repo_path, rich_root, tree_ref,
274
 
            external_lookup, repo_format)
 
182
        :return: (ok, network_name)
275
183
        """
276
184
        bzrdir = BzrDir.open_from_transport(
277
185
            self.transport_from_client_path(path))
278
186
        format = branch.network_format_registry.get(network_name)
279
187
        bzrdir.branch_format = format
280
 
        result = format.initialize(bzrdir, name="")
 
188
        result = format.initialize(bzrdir)
281
189
        rich_root, tree_ref, external_lookup = self._format_to_capabilities(
282
190
            result.repository._format)
283
191
        branch_format = result._format.network_name()
286
194
            result.repository)
287
195
        # branch format, repo relpath, rich_root, tree_ref, external_lookup,
288
196
        # repo_network_name
289
 
        return SuccessfulSmartServerResponse((b'ok', branch_format, repo_path,
 
197
        return SuccessfulSmartServerResponse(('ok', branch_format, repo_path,
290
198
            rich_root, tree_ref, external_lookup, repo_format))
291
199
 
292
200
 
313
221
        """
314
222
        bzrdir = BzrDir.open_from_transport(
315
223
            self.transport_from_client_path(path))
316
 
        shared = shared == b'True'
 
224
        shared = shared == 'True'
317
225
        format = repository.network_format_registry.get(network_name)
318
226
        bzrdir.repository_format = format
319
227
        result = format.initialize(bzrdir, shared=shared)
320
228
        rich_root, tree_ref, external_lookup = self._format_to_capabilities(
321
229
            result._format)
322
 
        return SuccessfulSmartServerResponse((b'ok', rich_root, tree_ref,
 
230
        return SuccessfulSmartServerResponse(('ok', rich_root, tree_ref,
323
231
            external_lookup, result._format.network_name()))
324
232
 
325
233
 
365
273
        """
366
274
        try:
367
275
            path, rich_root, tree_ref, external_lookup, name = self._find(path)
368
 
            return SuccessfulSmartServerResponse((b'ok', path.encode('utf-8'), rich_root, tree_ref))
 
276
            return SuccessfulSmartServerResponse(('ok', path, rich_root, tree_ref))
369
277
        except errors.NoRepositoryPresent:
370
 
            return FailedSmartServerResponse((b'norepository', ))
 
278
            return FailedSmartServerResponse(('norepository', ))
371
279
 
372
280
 
373
281
class SmartServerRequestFindRepositoryV2(SmartServerRequestFindRepository):
390
298
        try:
391
299
            path, rich_root, tree_ref, external_lookup, name = self._find(path)
392
300
            return SuccessfulSmartServerResponse(
393
 
                (b'ok', path.encode('utf-8'), rich_root, tree_ref, external_lookup))
 
301
                ('ok', path, rich_root, tree_ref, external_lookup))
394
302
        except errors.NoRepositoryPresent:
395
 
            return FailedSmartServerResponse((b'norepository', ))
 
303
            return FailedSmartServerResponse(('norepository', ))
396
304
 
397
305
 
398
306
class SmartServerRequestFindRepositoryV3(SmartServerRequestFindRepository):
414
322
        try:
415
323
            path, rich_root, tree_ref, external_lookup, name = self._find(path)
416
324
            return SuccessfulSmartServerResponse(
417
 
                (b'ok', path.encode('utf-8'), rich_root, tree_ref, external_lookup, name))
 
325
                ('ok', path, rich_root, tree_ref, external_lookup, name))
418
326
        except errors.NoRepositoryPresent:
419
 
            return FailedSmartServerResponse((b'norepository', ))
 
327
            return FailedSmartServerResponse(('norepository', ))
420
328
 
421
329
 
422
330
class SmartServerBzrDirRequestConfigFile(SmartServerRequestBzrDir):
423
331
 
424
332
    def do_bzrdir_request(self):
425
333
        """Get the configuration bytes for a config file in bzrdir.
426
 
 
 
334
        
427
335
        The body is not utf8 decoded - it is the literal bytestream from disk.
428
336
        """
429
337
        config = self._bzrdir._get_config()
430
338
        if config is None:
431
 
            content = b''
 
339
            content = ''
432
340
        else:
433
341
            content = config._get_config_file().read()
434
342
        return SuccessfulSmartServerResponse((), content)
435
343
 
436
344
 
437
 
class SmartServerBzrDirRequestGetBranches(SmartServerRequestBzrDir):
438
 
 
439
 
    def do_bzrdir_request(self):
440
 
        """Get the branches in a control directory.
441
 
        
442
 
        The body is a bencoded dictionary, with values similar to the return
443
 
        value of the open branch request.
444
 
        """
445
 
        branches = self._bzrdir.get_branches()
446
 
        ret = {}
447
 
        for name, b in branches.items():
448
 
            if name is None:
449
 
                name = b""
450
 
            ret[name.encode('utf-8')] = (b"branch", b._format.network_name())
451
 
        return SuccessfulSmartServerResponse(
452
 
            (b"success", ), bencode.bencode(ret))
453
 
 
454
 
 
455
345
class SmartServerRequestInitializeBzrDir(SmartServerRequest):
456
346
 
457
347
    def do(self, path):
462
352
        """
463
353
        target_transport = self.transport_from_client_path(path)
464
354
        BzrDirFormat.get_default_format().initialize_on_transport(target_transport)
465
 
        return SuccessfulSmartServerResponse((b'ok', ))
 
355
        return SuccessfulSmartServerResponse(('ok', ))
466
356
 
467
357
 
468
358
class SmartServerRequestBzrDirInitializeEx(SmartServerRequestBzrDir):
470
360
    def parse_NoneTrueFalse(self, arg):
471
361
        if not arg:
472
362
            return None
473
 
        if arg == b'False':
 
363
        if arg == 'False':
474
364
            return False
475
 
        if arg == b'True':
 
365
        if arg == 'True':
476
366
            return True
477
367
        raise AssertionError("invalid arg %r" % arg)
478
368
 
481
371
 
482
372
    def _serialize_NoneTrueFalse(self, arg):
483
373
        if arg is False:
484
 
            return b'False'
 
374
            return 'False'
485
375
        if not arg:
486
 
            return b''
487
 
        return b'True'
 
376
            return ''
 
377
        return 'True'
488
378
 
489
379
    def do(self, bzrdir_network_name, path, use_existing_dir, create_prefix,
490
380
        force_new_repo, stacked_on, stack_on_pwd, repo_format_name,
509
399
        stack_on_pwd = self.parse_NoneString(stack_on_pwd)
510
400
        make_working_trees = self.parse_NoneTrueFalse(make_working_trees)
511
401
        shared_repo = self.parse_NoneTrueFalse(shared_repo)
512
 
        if stack_on_pwd == b'.':
513
 
            stack_on_pwd = target_transport.base.encode('utf-8')
 
402
        if stack_on_pwd == '.':
 
403
            stack_on_pwd = target_transport.base
514
404
        repo_format_name = self.parse_NoneString(repo_format_name)
515
405
        repo, bzrdir, stacking, repository_policy = \
516
406
            format.initialize_on_transport_ex(target_transport,
520
410
            make_working_trees=make_working_trees, shared_repo=shared_repo)
521
411
        if repo is None:
522
412
            repo_path = ''
523
 
            repo_name = b''
524
 
            rich_root = tree_ref = external_lookup = b''
525
 
            repo_bzrdir_name = b''
 
413
            repo_name = ''
 
414
            rich_root = tree_ref = external_lookup = ''
 
415
            repo_bzrdir_name = ''
526
416
            final_stack = None
527
417
            final_stack_pwd = None
528
 
            repo_lock_token = b''
 
418
            repo_lock_token = ''
529
419
        else:
530
420
            repo_path = self._repo_relpath(bzrdir.root_transport, repo)
531
421
            if repo_path == '':
533
423
            rich_root, tree_ref, external_lookup = self._format_to_capabilities(
534
424
                repo._format)
535
425
            repo_name = repo._format.network_name()
536
 
            repo_bzrdir_name = repo.controldir._format.network_name()
 
426
            repo_bzrdir_name = repo.bzrdir._format.network_name()
537
427
            final_stack = repository_policy._stack_on
538
428
            final_stack_pwd = repository_policy._stack_on_pwd
539
429
            # It is returned locked, but we need to do the lock to get the lock
540
430
            # token.
541
431
            repo.unlock()
542
 
            repo_lock_token = repo.lock_write().repository_token or b''
 
432
            repo_lock_token = repo.lock_write() or ''
543
433
            if repo_lock_token:
544
434
                repo.leave_lock_in_place()
545
435
            repo.unlock()
558
448
                self._root_client_path, client_path)
559
449
            final_stack_pwd = '.'
560
450
 
561
 
        return SuccessfulSmartServerResponse((repo_path.encode('utf-8'),
562
 
            rich_root, tree_ref, external_lookup, repo_name, repo_bzrdir_name,
 
451
        return SuccessfulSmartServerResponse((repo_path, rich_root, tree_ref,
 
452
            external_lookup, repo_name, repo_bzrdir_name,
563
453
            bzrdir._format.network_name(),
564
 
            self._serialize_NoneTrueFalse(stacking), final_stack.encode('utf-8'),
565
 
            final_stack_pwd.encode('utf-8'), repo_lock_token))
 
454
            self._serialize_NoneTrueFalse(stacking), final_stack,
 
455
            final_stack_pwd, repo_lock_token))
566
456
 
567
457
 
568
458
class SmartServerRequestOpenBranch(SmartServerRequestBzrDir):
572
462
        try:
573
463
            reference_url = self._bzrdir.get_branch_reference()
574
464
            if reference_url is None:
575
 
                reference_url = ''
576
 
            return SuccessfulSmartServerResponse((b'ok', reference_url.encode('utf-8')))
577
 
        except errors.NotBranchError as e:
578
 
            return FailedSmartServerResponse((b'nobranch',))
 
465
                return SuccessfulSmartServerResponse(('ok', ''))
 
466
            else:
 
467
                return SuccessfulSmartServerResponse(('ok', reference_url))
 
468
        except errors.NotBranchError, e:
 
469
            return FailedSmartServerResponse(('nobranch',))
579
470
 
580
471
 
581
472
class SmartServerRequestOpenBranchV2(SmartServerRequestBzrDir):
587
478
            if reference_url is None:
588
479
                br = self._bzrdir.open_branch(ignore_fallbacks=True)
589
480
                format = br._format.network_name()
590
 
                return SuccessfulSmartServerResponse((b'branch', format))
 
481
                return SuccessfulSmartServerResponse(('branch', format))
591
482
            else:
592
 
                return SuccessfulSmartServerResponse((b'ref', reference_url.encode('utf-8')))
593
 
        except errors.NotBranchError as e:
594
 
            return FailedSmartServerResponse((b'nobranch',))
 
483
                return SuccessfulSmartServerResponse(('ref', reference_url))
 
484
        except errors.NotBranchError, e:
 
485
            return FailedSmartServerResponse(('nobranch',))
595
486
 
596
487
 
597
488
class SmartServerRequestOpenBranchV3(SmartServerRequestBzrDir):
611
502
            if reference_url is None:
612
503
                br = self._bzrdir.open_branch(ignore_fallbacks=True)
613
504
                format = br._format.network_name()
614
 
                return SuccessfulSmartServerResponse((b'branch', format))
 
505
                return SuccessfulSmartServerResponse(('branch', format))
615
506
            else:
616
 
                return SuccessfulSmartServerResponse((b'ref', reference_url.encode('utf-8')))
617
 
        except errors.NotBranchError as e:
 
507
                return SuccessfulSmartServerResponse(('ref', reference_url))
 
508
        except errors.NotBranchError, e:
618
509
            # Stringify the exception so that its .detail attribute will be
619
510
            # filled out.
620
511
            str(e)
621
 
            resp = (b'nobranch',)
 
512
            resp = ('nobranch',)
622
513
            detail = e.detail
623
514
            if detail:
624
515
                if detail.startswith(': '):
625
516
                    detail = detail[2:]
626
 
                resp += (detail.encode('utf-8'),)
 
517
                resp += (detail,)
627
518
            return FailedSmartServerResponse(resp)
628
519