~bzr-pqm/bzr/bzr.dev

5752.3.8 by John Arbash Meinel
Merge bzr.dev 5764 to resolve release-notes (aka NEWS) conflicts
1
# Copyright (C) 2008, 2009, 2011 Canonical Ltd
3251.3.1 by Aaron Bentley
Add support for directory services
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
3251.3.1 by Aaron Bentley
Add support for directory services
16
3251.3.5 by Aaron Bentley
Update docstring
17
"""Directory service registration and usage.
18
19
Directory services are utilities that provide a mapping from URL-like strings
20
to true URLs.  Examples include lp:urls and per-user location aliases.
21
"""
22
6379.6.7 by Jelmer Vernooij
Move importing from future until after doc string, otherwise the doc string will disappear.
23
from __future__ import absolute_import
24
5753.2.2 by Jelmer Vernooij
Remove some unnecessary imports, clean up lazy imports.
25
from bzrlib import (
26
    errors,
27
    registry,
28
    )
3224.5.31 by Andrew Bennetts
A couple more lazy imports, helps 'bzr log --line -r -1' a little.
29
from bzrlib.lazy_import import lazy_import
30
lazy_import(globals(), """
5753.2.2 by Jelmer Vernooij
Remove some unnecessary imports, clean up lazy imports.
31
from bzrlib import (
32
    branch as _mod_branch,
33
    urlutils,
34
    )
3224.5.31 by Andrew Bennetts
A couple more lazy imports, helps 'bzr log --line -r -1' a little.
35
""")
3625.1.2 by Michael Hudson
import urlutils and write urlutils.join rather than join
36
3251.3.1 by Aaron Bentley
Add support for directory services
37
38
class DirectoryServiceRegistry(registry.Registry):
3251.3.5 by Aaron Bentley
Update docstring
39
    """This object maintains and uses a list of directory services.
40
41
    Directory services may be registered via the standard Registry methods.
42
    They will be invoked if their key is a prefix of the supplied URL.
43
44
    Each item registered should be a factory of objects that provide a look_up
45
    method, as invoked by dereference.  Specifically, look_up should accept a
46
    name and URL, and return a URL.
47
    """
3251.3.1 by Aaron Bentley
Add support for directory services
48
49
    def dereference(self, url):
3251.3.3 by Aaron Bentley
Add docstring.
50
        """Dereference a supplied URL if possible.
51
52
        URLs that match a registered directory service prefix are looked up in
53
        it.  Non-matching urls are returned verbatim.
3251.3.5 by Aaron Bentley
Update docstring
54
55
        This is applied only once; the resulting URL must not be one that
56
        requires further dereferencing.
57
3251.3.3 by Aaron Bentley
Add docstring.
58
        :param url: The URL to dereference
59
        :return: The dereferenced URL if applicable, the input URL otherwise.
60
        """
3251.3.1 by Aaron Bentley
Add support for directory services
61
        match = self.get_prefix(url)
62
        if match is None:
63
            return url
64
        service, name = match
65
        return service().look_up(name, url)
66
67
directories = DirectoryServiceRegistry()
3512.2.1 by Aaron Bentley
Add support for branch-associated locations
68
69
class AliasDirectory(object):
70
    """Directory lookup for locations associated with a branch.
71
3512.2.4 by Aaron Bentley
Fix spacing
72
    :parent, :submit, :public, :push, :this, and :bound are currently
3512.2.2 by Aaron Bentley
Add :push and :this
73
    supported.  On error, a subclass of DirectoryLookupFailure will be raised.
3512.2.1 by Aaron Bentley
Add support for branch-associated locations
74
    """
75
6319.2.1 by Jelmer Vernooij
Allow registering custom location aliases.
76
    branch_aliases = registry.Registry()
77
    branch_aliases.register('parent', lambda b: b.get_parent(),
6319.2.2 by Jelmer Vernooij
Fix capitalization.
78
        help="The parent of this branch.")
6319.2.1 by Jelmer Vernooij
Allow registering custom location aliases.
79
    branch_aliases.register('submit', lambda b: b.get_submit_branch(),
6319.2.2 by Jelmer Vernooij
Fix capitalization.
80
        help="The submit branch for this branch.")
6319.2.1 by Jelmer Vernooij
Allow registering custom location aliases.
81
    branch_aliases.register('public', lambda b: b.get_public_branch(),
6319.2.2 by Jelmer Vernooij
Fix capitalization.
82
        help="The public location of this branch.")
6319.2.1 by Jelmer Vernooij
Allow registering custom location aliases.
83
    branch_aliases.register('bound', lambda b: b.get_bound_location(),
6319.2.2 by Jelmer Vernooij
Fix capitalization.
84
        help="The branch this branch is bound to, for bound branches.")
6319.2.1 by Jelmer Vernooij
Allow registering custom location aliases.
85
    branch_aliases.register('push', lambda b: b.get_push_location(),
6319.2.2 by Jelmer Vernooij
Fix capitalization.
86
        help="The saved location used for `bzr push` with no arguments.")
6319.2.1 by Jelmer Vernooij
Allow registering custom location aliases.
87
    branch_aliases.register('this', lambda b: b.base,
6319.2.2 by Jelmer Vernooij
Fix capitalization.
88
        help="This branch.")
6319.2.1 by Jelmer Vernooij
Allow registering custom location aliases.
89
3512.2.1 by Aaron Bentley
Add support for branch-associated locations
90
    def look_up(self, name, url):
5753.2.2 by Jelmer Vernooij
Remove some unnecessary imports, clean up lazy imports.
91
        branch = _mod_branch.Branch.open_containing('.')[0]
3714.2.2 by Aaron Bentley
Tweak logic to reduce string searching
92
        parts = url.split('/', 1)
93
        if len(parts) == 2:
94
            name, extra = parts
3625.1.1 by Michael Hudson
Allow appending path segments to the :<name> style aliases.
95
        else:
3714.2.2 by Aaron Bentley
Tweak logic to reduce string searching
96
            (name,) = parts
3625.1.1 by Michael Hudson
Allow appending path segments to the :<name> style aliases.
97
            extra = None
3512.2.1 by Aaron Bentley
Add support for branch-associated locations
98
        try:
6319.2.1 by Jelmer Vernooij
Allow registering custom location aliases.
99
            method = self.branch_aliases.get(name[1:])
3512.2.1 by Aaron Bentley
Add support for branch-associated locations
100
        except KeyError:
101
            raise errors.InvalidLocationAlias(url)
102
        else:
6319.2.1 by Jelmer Vernooij
Allow registering custom location aliases.
103
            result = method(branch)
3512.2.1 by Aaron Bentley
Add support for branch-associated locations
104
        if result is None:
105
            raise errors.UnsetLocationAlias(url)
3625.1.1 by Michael Hudson
Allow appending path segments to the :<name> style aliases.
106
        if extra is not None:
3625.1.2 by Michael Hudson
import urlutils and write urlutils.join rather than join
107
            result = urlutils.join(result, extra)
3512.2.1 by Aaron Bentley
Add support for branch-associated locations
108
        return result
109
6319.2.1 by Jelmer Vernooij
Allow registering custom location aliases.
110
    @classmethod
111
    def help_text(cls, topic):
112
        alias_lines = []
113
        for key in cls.branch_aliases.keys():
114
            help = cls.branch_aliases.get_help(key)
115
            alias_lines.append("  :%-10s%s\n" % (key, help))
116
        return """\
117
Location aliases
118
================
119
120
Bazaar defines several aliases for locations associated with a branch.  These
121
can be used with most commands that expect a location, such as `bzr push`.
122
123
The aliases are::
124
125
%s
126
For example, to push to the parent location::
127
128
    bzr push :parent
129
""" % "".join(alias_lines)
130
131
3512.2.1 by Aaron Bentley
Add support for branch-associated locations
132
directories.register(':', AliasDirectory,
133
                     'Easy access to remembered branch locations')