/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: 2009-03-08 06:18:06 UTC
  • mto: This revision was merged to the branch mainline in revision 4102.
  • Revision ID: robertc@robertcollins.net-20090308061806-uvu37ccjrrzuspei
Refactor profiling exception handling to restore clear layers - command handling in commands.py, profiling in lsprof.py.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
"""Server-side bzrdir related request implmentations."""
18
18
 
19
19
 
20
 
from bzrlib import branch, errors, repository, urlutils
21
 
from bzrlib.bzrdir import (
22
 
    BzrDir,
23
 
    BzrDirFormat,
24
 
    BzrDirMetaFormat1,
25
 
    network_format_registry,
26
 
    )
 
20
from bzrlib import branch, errors, repository
 
21
from bzrlib.bzrdir import BzrDir, BzrDirFormat, BzrDirMetaFormat1
27
22
from bzrlib.smart.request import (
28
23
    FailedSmartServerResponse,
29
24
    SmartServerRequest,
34
29
class SmartServerRequestOpenBzrDir(SmartServerRequest):
35
30
 
36
31
    def do(self, path):
 
32
        from bzrlib.bzrdir import BzrDirFormat
37
33
        try:
38
34
            t = self.transport_from_client_path(path)
39
35
        except errors.PathNotChild:
55
51
        return SuccessfulSmartServerResponse((answer,))
56
52
 
57
53
 
58
 
class SmartServerRequestOpenBzrDir_2_1(SmartServerRequest):
59
 
 
60
 
    def do(self, path):
61
 
        """Is there a BzrDir present, and if so does it have a working tree?
62
 
 
63
 
        New in 2.1.
64
 
        """
65
 
        try:
66
 
            t = self.transport_from_client_path(path)
67
 
        except errors.PathNotChild:
68
 
            # The client is trying to ask about a path that they have no access
69
 
            # to.
70
 
            return SuccessfulSmartServerResponse(('no',))
71
 
        try:
72
 
            bd = BzrDir.open_from_transport(t)
73
 
        except errors.NotBranchError:
74
 
            answer = ('no',)
75
 
        else:
76
 
            answer = ('yes',)
77
 
            if bd.has_workingtree():
78
 
                answer += ('yes',)
79
 
            else:
80
 
                answer += ('no',)
81
 
        return SuccessfulSmartServerResponse(answer)
82
 
 
83
 
 
84
54
class SmartServerRequestBzrDir(SmartServerRequest):
85
55
 
86
56
    def do(self, path, *args):
87
57
        """Open a BzrDir at path, and return self.do_bzrdir_request(*args)."""
88
 
        try:
89
 
            self._bzrdir = BzrDir.open_from_transport(
90
 
                self.transport_from_client_path(path))
91
 
        except errors.NotBranchError, e:
92
 
            return FailedSmartServerResponse(('nobranch',))
 
58
        self._bzrdir = BzrDir.open_from_transport(
 
59
            self.transport_from_client_path(path))
93
60
        return self.do_bzrdir_request(*args)
94
61
 
95
62
    def _boolean_to_yes_no(self, a_boolean):
110
77
        """Get the relative path for repository from current_transport."""
111
78
        # the relpath of the bzrdir in the found repository gives us the
112
79
        # path segments to pop-out.
113
 
        relpath = repository.user_transport.relpath(
 
80
        relpath = repository.bzrdir.root_transport.relpath(
114
81
            current_transport.base)
115
82
        if len(relpath):
116
83
            segments = ['..'] * len(relpath.split('/'))
122
89
class SmartServerBzrDirRequestCloningMetaDir(SmartServerRequestBzrDir):
123
90
 
124
91
    def do_bzrdir_request(self, require_stacking):
125
 
        """Get the format that should be used when cloning from this dir.
126
 
 
127
 
        New in 1.13.
128
 
        
129
 
        :return: on success, a 3-tuple of network names for (control,
130
 
            repository, branch) directories, where '' signifies "not present".
131
 
            If this BzrDir contains a branch reference then this will fail with
132
 
            BranchReference; clients should resolve branch references before
133
 
            calling this RPC.
134
 
        """
 
92
        """Get the format that should be used when cloning from this dir."""
135
93
        try:
136
94
            branch_ref = self._bzrdir.get_branch_reference()
137
95
        except errors.NotBranchError:
138
96
            branch_ref = None
139
 
        if branch_ref is not None:
140
 
            # The server shouldn't try to resolve references, and it quite
141
 
            # possibly can't reach them anyway.  The client needs to resolve
142
 
            # the branch reference to determine the cloning_metadir.
143
 
            return FailedSmartServerResponse(('BranchReference',))
144
97
        if require_stacking == "True":
145
98
            require_stacking = True
146
99
        else:
148
101
        control_format = self._bzrdir.cloning_metadir(
149
102
            require_stacking=require_stacking)
150
103
        control_name = control_format.network_name()
151
 
        # XXX: There should be a method that tells us that the format does/does
152
 
        # not have subformats.
 
104
        # XXX: There should be a method that tells us that the format does/does not
 
105
        # have subformats.
153
106
        if isinstance(control_format, BzrDirMetaFormat1):
154
 
            branch_name = ('branch',
155
 
                control_format.get_branch_format().network_name())
 
107
            if branch_ref is not None:
 
108
                # If there's a branch reference, the client will have to resolve
 
109
                # the branch reference to figure out the cloning metadir
 
110
                branch_name = ('reference', branch_ref)
 
111
            else:
 
112
                branch_name = (
 
113
                    'direct', control_format.get_branch_format().network_name())
156
114
            repository_name = control_format.repository_format.network_name()
157
115
        else:
158
116
            # Only MetaDir has delegated formats today.
159
 
            branch_name = ('branch', '')
 
117
            branch_name = ''
160
118
            repository_name = ''
161
119
        return SuccessfulSmartServerResponse((control_name, repository_name,
162
120
            branch_name))
205
163
 
206
164
        This operates precisely like 'bzrdir.create_repository'.
207
165
 
208
 
        If a bzrdir is not present, an exception is propagated
 
166
        If a bzrdir is not present, an exception is propogated
209
167
        rather than 'no branch' because these are different conditions (and
210
168
        this method should only be called after establishing that a bzr dir
211
169
        exists anyway).
262
220
 
263
221
        This operates precisely like 'bzrdir.find_repository'.
264
222
 
265
 
        If a bzrdir is not present, an exception is propagated
 
223
        If a bzrdir is not present, an exception is propogated
266
224
        rather than 'no branch' because these are different conditions.
267
225
 
268
226
        This is the initial version of this method introduced with the smart
285
243
 
286
244
        This operates precisely like 'bzrdir.find_repository'.
287
245
 
288
 
        If a bzrdir is not present, an exception is propagated
 
246
        If a bzrdir is not present, an exception is propogated
289
247
        rather than 'no branch' because these are different conditions.
290
248
 
291
249
        This is the second edition of this method introduced in bzr 1.3, which
327
285
            return FailedSmartServerResponse(('norepository', ))
328
286
 
329
287
 
330
 
class SmartServerBzrDirRequestConfigFile(SmartServerRequestBzrDir):
331
 
 
332
 
    def do_bzrdir_request(self):
333
 
        """Get the configuration bytes for a config file in bzrdir.
334
 
        
335
 
        The body is not utf8 decoded - it is the literal bytestream from disk.
336
 
        """
337
 
        config = self._bzrdir._get_config()
338
 
        if config is None:
339
 
            content = ''
340
 
        else:
341
 
            content = config._get_config_file().read()
342
 
        return SuccessfulSmartServerResponse((), content)
343
 
 
344
 
 
345
288
class SmartServerRequestInitializeBzrDir(SmartServerRequest):
346
289
 
347
290
    def do(self, path):
355
298
        return SuccessfulSmartServerResponse(('ok', ))
356
299
 
357
300
 
358
 
class SmartServerRequestBzrDirInitializeEx(SmartServerRequestBzrDir):
359
 
 
360
 
    def parse_NoneTrueFalse(self, arg):
361
 
        if not arg:
362
 
            return None
363
 
        if arg == 'False':
364
 
            return False
365
 
        if arg == 'True':
366
 
            return True
367
 
        raise AssertionError("invalid arg %r" % arg)
368
 
 
369
 
    def parse_NoneString(self, arg):
370
 
        return arg or None
371
 
 
372
 
    def _serialize_NoneTrueFalse(self, arg):
373
 
        if arg is False:
374
 
            return 'False'
375
 
        if not arg:
376
 
            return ''
377
 
        return 'True'
378
 
 
379
 
    def do(self, bzrdir_network_name, path, use_existing_dir, create_prefix,
380
 
        force_new_repo, stacked_on, stack_on_pwd, repo_format_name,
381
 
        make_working_trees, shared_repo):
382
 
        """Initialize a bzrdir at path as per
383
 
        BzrDirFormat.initialize_on_transport_ex.
384
 
 
385
 
        New in 1.16.  (Replaces BzrDirFormat.initialize_ex verb from 1.15).
386
 
 
387
 
        :return: return SuccessfulSmartServerResponse((repo_path, rich_root,
388
 
            tree_ref, external_lookup, repo_network_name,
389
 
            repo_bzrdir_network_name, bzrdir_format_network_name,
390
 
            NoneTrueFalse(stacking), final_stack, final_stack_pwd,
391
 
            repo_lock_token))
 
301
class SmartServerRequestOpenBranch(SmartServerRequest):
 
302
 
 
303
    def do(self, path):
 
304
        """try to open a branch at path and return ok/nobranch.
 
305
 
 
306
        If a bzrdir is not present, an exception is propogated
 
307
        rather than 'no branch' because these are different conditions.
392
308
        """
393
 
        target_transport = self.transport_from_client_path(path)
394
 
        format = network_format_registry.get(bzrdir_network_name)
395
 
        use_existing_dir = self.parse_NoneTrueFalse(use_existing_dir)
396
 
        create_prefix = self.parse_NoneTrueFalse(create_prefix)
397
 
        force_new_repo = self.parse_NoneTrueFalse(force_new_repo)
398
 
        stacked_on = self.parse_NoneString(stacked_on)
399
 
        stack_on_pwd = self.parse_NoneString(stack_on_pwd)
400
 
        make_working_trees = self.parse_NoneTrueFalse(make_working_trees)
401
 
        shared_repo = self.parse_NoneTrueFalse(shared_repo)
402
 
        if stack_on_pwd == '.':
403
 
            stack_on_pwd = target_transport.base
404
 
        repo_format_name = self.parse_NoneString(repo_format_name)
405
 
        repo, bzrdir, stacking, repository_policy = \
406
 
            format.initialize_on_transport_ex(target_transport,
407
 
            use_existing_dir=use_existing_dir, create_prefix=create_prefix,
408
 
            force_new_repo=force_new_repo, stacked_on=stacked_on,
409
 
            stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
410
 
            make_working_trees=make_working_trees, shared_repo=shared_repo)
411
 
        if repo is None:
412
 
            repo_path = ''
413
 
            repo_name = ''
414
 
            rich_root = tree_ref = external_lookup = ''
415
 
            repo_bzrdir_name = ''
416
 
            final_stack = None
417
 
            final_stack_pwd = None
418
 
            repo_lock_token = ''
419
 
        else:
420
 
            repo_path = self._repo_relpath(bzrdir.root_transport, repo)
421
 
            if repo_path == '':
422
 
                repo_path = '.'
423
 
            rich_root, tree_ref, external_lookup = self._format_to_capabilities(
424
 
                repo._format)
425
 
            repo_name = repo._format.network_name()
426
 
            repo_bzrdir_name = repo.bzrdir._format.network_name()
427
 
            final_stack = repository_policy._stack_on
428
 
            final_stack_pwd = repository_policy._stack_on_pwd
429
 
            # It is returned locked, but we need to do the lock to get the lock
430
 
            # token.
431
 
            repo.unlock()
432
 
            repo_lock_token = repo.lock_write() or ''
433
 
            if repo_lock_token:
434
 
                repo.leave_lock_in_place()
435
 
            repo.unlock()
436
 
        final_stack = final_stack or ''
437
 
        final_stack_pwd = final_stack_pwd or ''
438
 
 
439
 
        # We want this to be relative to the bzrdir.
440
 
        if final_stack_pwd:
441
 
            final_stack_pwd = urlutils.relative_url(
442
 
                target_transport.base, final_stack_pwd)
443
 
 
444
 
        # Can't meaningfully return a root path.
445
 
        if final_stack.startswith('/'):
446
 
            client_path = self._root_client_path + final_stack[1:]
447
 
            final_stack = urlutils.relative_url(
448
 
                self._root_client_path, client_path)
449
 
            final_stack_pwd = '.'
450
 
 
451
 
        return SuccessfulSmartServerResponse((repo_path, rich_root, tree_ref,
452
 
            external_lookup, repo_name, repo_bzrdir_name,
453
 
            bzrdir._format.network_name(),
454
 
            self._serialize_NoneTrueFalse(stacking), final_stack,
455
 
            final_stack_pwd, repo_lock_token))
456
 
 
457
 
 
458
 
class SmartServerRequestOpenBranch(SmartServerRequestBzrDir):
459
 
 
460
 
    def do_bzrdir_request(self):
461
 
        """open a branch at path and return the branch reference or branch."""
 
309
        bzrdir = BzrDir.open_from_transport(
 
310
            self.transport_from_client_path(path))
462
311
        try:
463
 
            reference_url = self._bzrdir.get_branch_reference()
 
312
            reference_url = bzrdir.get_branch_reference()
464
313
            if reference_url is None:
465
314
                return SuccessfulSmartServerResponse(('ok', ''))
466
315
            else:
467
316
                return SuccessfulSmartServerResponse(('ok', reference_url))
468
 
        except errors.NotBranchError, e:
469
 
            return FailedSmartServerResponse(('nobranch',))
470
 
 
471
 
 
472
 
class SmartServerRequestOpenBranchV2(SmartServerRequestBzrDir):
473
 
 
474
 
    def do_bzrdir_request(self):
475
 
        """open a branch at path and return the reference or format."""
476
 
        try:
477
 
            reference_url = self._bzrdir.get_branch_reference()
478
 
            if reference_url is None:
479
 
                br = self._bzrdir.open_branch(ignore_fallbacks=True)
480
 
                format = br._format.network_name()
481
 
                return SuccessfulSmartServerResponse(('branch', format))
482
 
            else:
483
 
                return SuccessfulSmartServerResponse(('ref', reference_url))
484
 
        except errors.NotBranchError, e:
485
 
            return FailedSmartServerResponse(('nobranch',))
486
 
 
487
 
 
488
 
class SmartServerRequestOpenBranchV3(SmartServerRequestBzrDir):
489
 
 
490
 
    def do_bzrdir_request(self):
491
 
        """Open a branch at path and return the reference or format.
492
 
        
493
 
        This version introduced in 2.1.
494
 
 
495
 
        Differences to SmartServerRequestOpenBranchV2:
496
 
          * can return 2-element ('nobranch', extra), where 'extra' is a string
497
 
            with an explanation like 'location is a repository'.  Previously
498
 
            a 'nobranch' response would never have more than one element.
499
 
        """
500
 
        try:
501
 
            reference_url = self._bzrdir.get_branch_reference()
502
 
            if reference_url is None:
503
 
                br = self._bzrdir.open_branch(ignore_fallbacks=True)
504
 
                format = br._format.network_name()
505
 
                return SuccessfulSmartServerResponse(('branch', format))
506
 
            else:
507
 
                return SuccessfulSmartServerResponse(('ref', reference_url))
508
 
        except errors.NotBranchError, e:
509
 
            # Stringify the exception so that its .detail attribute will be
510
 
            # filled out.
511
 
            str(e)
512
 
            resp = ('nobranch',)
513
 
            detail = e.detail
514
 
            if detail:
515
 
                if detail.startswith(': '):
516
 
                    detail = detail[2:]
517
 
                resp += (detail,)
518
 
            return FailedSmartServerResponse(resp)
519
 
 
 
317
        except errors.NotBranchError:
 
318
            return FailedSmartServerResponse(('nobranch', ))