~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/smart/bzrdir.py

  • Committer: Robert Collins
  • Date: 2009-03-31 00:12:10 UTC
  • mto: This revision was merged to the branch mainline in revision 4219.
  • Revision ID: robertc@robertcollins.net-20090331001210-fufeq2heozx9jne0
Fix Tree.get_symlink_target to decode from the disk encoding to get a unicode encoded string.

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
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
 
    BzrProber,
26
 
    )
27
 
from bzrlib.controldir import (
28
 
    network_format_registry,
29
 
    )
 
20
from bzrlib import branch, errors, repository
 
21
from bzrlib.bzrdir import BzrDir, BzrDirFormat, BzrDirMetaFormat1
30
22
from bzrlib.smart.request import (
31
23
    FailedSmartServerResponse,
32
24
    SmartServerRequest,
37
29
class SmartServerRequestOpenBzrDir(SmartServerRequest):
38
30
 
39
31
    def do(self, path):
 
32
        from bzrlib.bzrdir import BzrDirFormat
40
33
        try:
41
34
            t = self.transport_from_client_path(path)
42
35
        except errors.PathNotChild:
47
40
            # clients that don't anticipate errors from this method.
48
41
            answer = 'no'
49
42
        else:
50
 
            bzr_prober = BzrProber()
 
43
            default_format = BzrDirFormat.get_default_format()
 
44
            real_bzrdir = default_format.open(t, _found=True)
51
45
            try:
52
 
                bzr_prober.probe_transport(t)
 
46
                real_bzrdir._format.probe_transport(t)
53
47
            except (errors.NotBranchError, errors.UnknownFormatError):
54
48
                answer = 'no'
55
49
            else:
57
51
        return SuccessfulSmartServerResponse((answer,))
58
52
 
59
53
 
60
 
class SmartServerRequestOpenBzrDir_2_1(SmartServerRequest):
61
 
 
62
 
    def do(self, path):
63
 
        """Is there a BzrDir present, and if so does it have a working tree?
64
 
 
65
 
        New in 2.1.
66
 
        """
67
 
        try:
68
 
            t = self.transport_from_client_path(path)
69
 
        except errors.PathNotChild:
70
 
            # The client is trying to ask about a path that they have no access
71
 
            # to.
72
 
            return SuccessfulSmartServerResponse(('no',))
73
 
        try:
74
 
            bd = BzrDir.open_from_transport(t)
75
 
        except errors.NotBranchError:
76
 
            answer = ('no',)
77
 
        else:
78
 
            answer = ('yes',)
79
 
            if bd.has_workingtree():
80
 
                answer += ('yes',)
81
 
            else:
82
 
                answer += ('no',)
83
 
        return SuccessfulSmartServerResponse(answer)
84
 
 
85
 
 
86
54
class SmartServerRequestBzrDir(SmartServerRequest):
87
55
 
88
56
    def do(self, path, *args):
90
58
        try:
91
59
            self._bzrdir = BzrDir.open_from_transport(
92
60
                self.transport_from_client_path(path))
93
 
        except errors.NotBranchError, e:
94
 
            return FailedSmartServerResponse(('nobranch',))
 
61
        except errors.NotBranchError:
 
62
            return FailedSmartServerResponse(('nobranch', ))
95
63
        return self.do_bzrdir_request(*args)
96
64
 
97
65
    def _boolean_to_yes_no(self, a_boolean):
112
80
        """Get the relative path for repository from current_transport."""
113
81
        # the relpath of the bzrdir in the found repository gives us the
114
82
        # path segments to pop-out.
115
 
        relpath = repository.user_transport.relpath(
 
83
        relpath = repository.bzrdir.root_transport.relpath(
116
84
            current_transport.base)
117
85
        if len(relpath):
118
86
            segments = ['..'] * len(relpath.split('/'))
207
175
 
208
176
        This operates precisely like 'bzrdir.create_repository'.
209
177
 
210
 
        If a bzrdir is not present, an exception is propagated
 
178
        If a bzrdir is not present, an exception is propogated
211
179
        rather than 'no branch' because these are different conditions (and
212
180
        this method should only be called after establishing that a bzr dir
213
181
        exists anyway).
264
232
 
265
233
        This operates precisely like 'bzrdir.find_repository'.
266
234
 
267
 
        If a bzrdir is not present, an exception is propagated
 
235
        If a bzrdir is not present, an exception is propogated
268
236
        rather than 'no branch' because these are different conditions.
269
237
 
270
238
        This is the initial version of this method introduced with the smart
287
255
 
288
256
        This operates precisely like 'bzrdir.find_repository'.
289
257
 
290
 
        If a bzrdir is not present, an exception is propagated
 
258
        If a bzrdir is not present, an exception is propogated
291
259
        rather than 'no branch' because these are different conditions.
292
260
 
293
261
        This is the second edition of this method introduced in bzr 1.3, which
329
297
            return FailedSmartServerResponse(('norepository', ))
330
298
 
331
299
 
332
 
class SmartServerBzrDirRequestConfigFile(SmartServerRequestBzrDir):
333
 
 
334
 
    def do_bzrdir_request(self):
335
 
        """Get the configuration bytes for a config file in bzrdir.
336
 
        
337
 
        The body is not utf8 decoded - it is the literal bytestream from disk.
338
 
        """
339
 
        config = self._bzrdir._get_config()
340
 
        if config is None:
341
 
            content = ''
342
 
        else:
343
 
            content = config._get_config_file().read()
344
 
        return SuccessfulSmartServerResponse((), content)
345
 
 
346
 
 
347
300
class SmartServerRequestInitializeBzrDir(SmartServerRequest):
348
301
 
349
302
    def do(self, path):
357
310
        return SuccessfulSmartServerResponse(('ok', ))
358
311
 
359
312
 
360
 
class SmartServerRequestBzrDirInitializeEx(SmartServerRequestBzrDir):
361
 
 
362
 
    def parse_NoneTrueFalse(self, arg):
363
 
        if not arg:
364
 
            return None
365
 
        if arg == 'False':
366
 
            return False
367
 
        if arg == 'True':
368
 
            return True
369
 
        raise AssertionError("invalid arg %r" % arg)
370
 
 
371
 
    def parse_NoneString(self, arg):
372
 
        return arg or None
373
 
 
374
 
    def _serialize_NoneTrueFalse(self, arg):
375
 
        if arg is False:
376
 
            return 'False'
377
 
        if not arg:
378
 
            return ''
379
 
        return 'True'
380
 
 
381
 
    def do(self, bzrdir_network_name, path, use_existing_dir, create_prefix,
382
 
        force_new_repo, stacked_on, stack_on_pwd, repo_format_name,
383
 
        make_working_trees, shared_repo):
384
 
        """Initialize a bzrdir at path as per
385
 
        BzrDirFormat.initialize_on_transport_ex.
386
 
 
387
 
        New in 1.16.  (Replaces BzrDirFormat.initialize_ex verb from 1.15).
388
 
 
389
 
        :return: return SuccessfulSmartServerResponse((repo_path, rich_root,
390
 
            tree_ref, external_lookup, repo_network_name,
391
 
            repo_bzrdir_network_name, bzrdir_format_network_name,
392
 
            NoneTrueFalse(stacking), final_stack, final_stack_pwd,
393
 
            repo_lock_token))
394
 
        """
395
 
        target_transport = self.transport_from_client_path(path)
396
 
        format = network_format_registry.get(bzrdir_network_name)
397
 
        use_existing_dir = self.parse_NoneTrueFalse(use_existing_dir)
398
 
        create_prefix = self.parse_NoneTrueFalse(create_prefix)
399
 
        force_new_repo = self.parse_NoneTrueFalse(force_new_repo)
400
 
        stacked_on = self.parse_NoneString(stacked_on)
401
 
        stack_on_pwd = self.parse_NoneString(stack_on_pwd)
402
 
        make_working_trees = self.parse_NoneTrueFalse(make_working_trees)
403
 
        shared_repo = self.parse_NoneTrueFalse(shared_repo)
404
 
        if stack_on_pwd == '.':
405
 
            stack_on_pwd = target_transport.base
406
 
        repo_format_name = self.parse_NoneString(repo_format_name)
407
 
        repo, bzrdir, stacking, repository_policy = \
408
 
            format.initialize_on_transport_ex(target_transport,
409
 
            use_existing_dir=use_existing_dir, create_prefix=create_prefix,
410
 
            force_new_repo=force_new_repo, stacked_on=stacked_on,
411
 
            stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
412
 
            make_working_trees=make_working_trees, shared_repo=shared_repo)
413
 
        if repo is None:
414
 
            repo_path = ''
415
 
            repo_name = ''
416
 
            rich_root = tree_ref = external_lookup = ''
417
 
            repo_bzrdir_name = ''
418
 
            final_stack = None
419
 
            final_stack_pwd = None
420
 
            repo_lock_token = ''
421
 
        else:
422
 
            repo_path = self._repo_relpath(bzrdir.root_transport, repo)
423
 
            if repo_path == '':
424
 
                repo_path = '.'
425
 
            rich_root, tree_ref, external_lookup = self._format_to_capabilities(
426
 
                repo._format)
427
 
            repo_name = repo._format.network_name()
428
 
            repo_bzrdir_name = repo.bzrdir._format.network_name()
429
 
            final_stack = repository_policy._stack_on
430
 
            final_stack_pwd = repository_policy._stack_on_pwd
431
 
            # It is returned locked, but we need to do the lock to get the lock
432
 
            # token.
433
 
            repo.unlock()
434
 
            repo_lock_token = repo.lock_write().repository_token or ''
435
 
            if repo_lock_token:
436
 
                repo.leave_lock_in_place()
437
 
            repo.unlock()
438
 
        final_stack = final_stack or ''
439
 
        final_stack_pwd = final_stack_pwd or ''
440
 
 
441
 
        # We want this to be relative to the bzrdir.
442
 
        if final_stack_pwd:
443
 
            final_stack_pwd = urlutils.relative_url(
444
 
                target_transport.base, final_stack_pwd)
445
 
 
446
 
        # Can't meaningfully return a root path.
447
 
        if final_stack.startswith('/'):
448
 
            client_path = self._root_client_path + final_stack[1:]
449
 
            final_stack = urlutils.relative_url(
450
 
                self._root_client_path, client_path)
451
 
            final_stack_pwd = '.'
452
 
 
453
 
        return SuccessfulSmartServerResponse((repo_path, rich_root, tree_ref,
454
 
            external_lookup, repo_name, repo_bzrdir_name,
455
 
            bzrdir._format.network_name(),
456
 
            self._serialize_NoneTrueFalse(stacking), final_stack,
457
 
            final_stack_pwd, repo_lock_token))
458
 
 
459
 
 
460
313
class SmartServerRequestOpenBranch(SmartServerRequestBzrDir):
461
314
 
462
315
    def do_bzrdir_request(self):
467
320
                return SuccessfulSmartServerResponse(('ok', ''))
468
321
            else:
469
322
                return SuccessfulSmartServerResponse(('ok', reference_url))
470
 
        except errors.NotBranchError, e:
471
 
            return FailedSmartServerResponse(('nobranch',))
 
323
        except errors.NotBranchError:
 
324
            return FailedSmartServerResponse(('nobranch', ))
472
325
 
473
326
 
474
327
class SmartServerRequestOpenBranchV2(SmartServerRequestBzrDir):
483
336
                return SuccessfulSmartServerResponse(('branch', format))
484
337
            else:
485
338
                return SuccessfulSmartServerResponse(('ref', reference_url))
486
 
        except errors.NotBranchError, e:
487
 
            return FailedSmartServerResponse(('nobranch',))
488
 
 
489
 
 
490
 
class SmartServerRequestOpenBranchV3(SmartServerRequestBzrDir):
491
 
 
492
 
    def do_bzrdir_request(self):
493
 
        """Open a branch at path and return the reference or format.
494
 
        
495
 
        This version introduced in 2.1.
496
 
 
497
 
        Differences to SmartServerRequestOpenBranchV2:
498
 
          * can return 2-element ('nobranch', extra), where 'extra' is a string
499
 
            with an explanation like 'location is a repository'.  Previously
500
 
            a 'nobranch' response would never have more than one element.
501
 
        """
502
 
        try:
503
 
            reference_url = self._bzrdir.get_branch_reference()
504
 
            if reference_url is None:
505
 
                br = self._bzrdir.open_branch(ignore_fallbacks=True)
506
 
                format = br._format.network_name()
507
 
                return SuccessfulSmartServerResponse(('branch', format))
508
 
            else:
509
 
                return SuccessfulSmartServerResponse(('ref', reference_url))
510
 
        except errors.NotBranchError, e:
511
 
            # Stringify the exception so that its .detail attribute will be
512
 
            # filled out.
513
 
            str(e)
514
 
            resp = ('nobranch',)
515
 
            detail = e.detail
516
 
            if detail:
517
 
                if detail.startswith(': '):
518
 
                    detail = detail[2:]
519
 
                resp += (detail,)
520
 
            return FailedSmartServerResponse(resp)
521
 
 
 
339
        except errors.NotBranchError:
 
340
            return FailedSmartServerResponse(('nobranch', ))