/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
23
from . import (
5753.2.2 by Jelmer Vernooij
Remove some unnecessary imports, clean up lazy imports.
24
    errors,
25
    registry,
26
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
27
from .lazy_import import lazy_import
3224.5.31 by Andrew Bennetts
A couple more lazy imports, helps 'bzr log --line -r -1' a little.
28
lazy_import(globals(), """
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
29
from breezy import (
5753.2.2 by Jelmer Vernooij
Remove some unnecessary imports, clean up lazy imports.
30
    branch as _mod_branch,
6511.3.1 by Jelmer Vernooij
Add 'co:' directory service.
31
    controldir as _mod_controldir,
5753.2.2 by Jelmer Vernooij
Remove some unnecessary imports, clean up lazy imports.
32
    urlutils,
33
    )
3224.5.31 by Andrew Bennetts
A couple more lazy imports, helps 'bzr log --line -r -1' a little.
34
""")
3625.1.2 by Michael Hudson
import urlutils and write urlutils.join rather than join
35
3251.3.1 by Aaron Bentley
Add support for directory services
36
6734.1.15 by Jelmer Vernooij
Move errors for breezy.directory_service.
37
class DirectoryLookupFailure(errors.BzrError):
38
    """Base type for lookup errors."""
39
40
41
class InvalidLocationAlias(DirectoryLookupFailure):
42
43
    _fmt = '"%(alias_name)s" is not a valid location alias.'
44
45
    def __init__(self, alias_name):
46
        DirectoryLookupFailure.__init__(self, alias_name=alias_name)
47
48
49
class UnsetLocationAlias(DirectoryLookupFailure):
50
51
    _fmt = 'No %(alias_name)s location assigned.'
52
53
    def __init__(self, alias_name):
54
        DirectoryLookupFailure.__init__(self, alias_name=alias_name[1:])
55
56
3251.3.1 by Aaron Bentley
Add support for directory services
57
class DirectoryServiceRegistry(registry.Registry):
3251.3.5 by Aaron Bentley
Update docstring
58
    """This object maintains and uses a list of directory services.
59
60
    Directory services may be registered via the standard Registry methods.
61
    They will be invoked if their key is a prefix of the supplied URL.
62
63
    Each item registered should be a factory of objects that provide a look_up
64
    method, as invoked by dereference.  Specifically, look_up should accept a
65
    name and URL, and return a URL.
66
    """
3251.3.1 by Aaron Bentley
Add support for directory services
67
7268.11.2 by Jelmer Vernooij
Add purpose argument.
68
    def dereference(self, url, purpose=None):
3251.3.3 by Aaron Bentley
Add docstring.
69
        """Dereference a supplied URL if possible.
70
71
        URLs that match a registered directory service prefix are looked up in
72
        it.  Non-matching urls are returned verbatim.
3251.3.5 by Aaron Bentley
Update docstring
73
74
        This is applied only once; the resulting URL must not be one that
75
        requires further dereferencing.
76
3251.3.3 by Aaron Bentley
Add docstring.
77
        :param url: The URL to dereference
7268.11.4 by Jelmer Vernooij
Actually pass in purpose in a couple of places.
78
        :param purpose: Purpose of the URL ('read', 'write' or None - if not declared)
3251.3.3 by Aaron Bentley
Add docstring.
79
        :return: The dereferenced URL if applicable, the input URL otherwise.
80
        """
3251.3.1 by Aaron Bentley
Add support for directory services
81
        match = self.get_prefix(url)
82
        if match is None:
83
            return url
84
        service, name = match
7268.11.3 by Jelmer Vernooij
Add backwards compatibility for older users of the directory API.
85
        directory = service()
86
        try:
87
            return directory.look_up(name, url, purpose=purpose)
88
        except TypeError:
89
            # Compatibility for plugins written for Breezy < 3.0.0
90
            return directory.look_up(name, url)
3251.3.1 by Aaron Bentley
Add support for directory services
91
7143.15.2 by Jelmer Vernooij
Run autopep8.
92
3251.3.1 by Aaron Bentley
Add support for directory services
93
directories = DirectoryServiceRegistry()
3512.2.1 by Aaron Bentley
Add support for branch-associated locations
94
7143.15.2 by Jelmer Vernooij
Run autopep8.
95
7268.11.2 by Jelmer Vernooij
Add purpose argument.
96
class Directory(object):
97
    """Abstract directory lookup class."""
98
99
    def look_up(self, name, url, purpose=None):
100
        """Look up an entry in a directory.
101
102
        :param name: Directory name
103
        :param url: The URL to dereference
7268.11.4 by Jelmer Vernooij
Actually pass in purpose in a couple of places.
104
        :param purpose: Purpose of the URL ('read', 'write' or None - if not declared)
7268.11.2 by Jelmer Vernooij
Add purpose argument.
105
        :return: The dereferenced URL if applicable, the input URL otherwise.
106
        """
107
        raise NotImplementedError(self.look_up)
108
109
110
class AliasDirectory(Directory):
3512.2.1 by Aaron Bentley
Add support for branch-associated locations
111
    """Directory lookup for locations associated with a branch.
112
3512.2.4 by Aaron Bentley
Fix spacing
113
    :parent, :submit, :public, :push, :this, and :bound are currently
3512.2.2 by Aaron Bentley
Add :push and :this
114
    supported.  On error, a subclass of DirectoryLookupFailure will be raised.
3512.2.1 by Aaron Bentley
Add support for branch-associated locations
115
    """
116
6319.2.1 by Jelmer Vernooij
Allow registering custom location aliases.
117
    branch_aliases = registry.Registry()
118
    branch_aliases.register('parent', lambda b: b.get_parent(),
7143.15.2 by Jelmer Vernooij
Run autopep8.
119
                            help="The parent of this branch.")
6319.2.1 by Jelmer Vernooij
Allow registering custom location aliases.
120
    branch_aliases.register('submit', lambda b: b.get_submit_branch(),
7143.15.2 by Jelmer Vernooij
Run autopep8.
121
                            help="The submit branch for this branch.")
6319.2.1 by Jelmer Vernooij
Allow registering custom location aliases.
122
    branch_aliases.register('public', lambda b: b.get_public_branch(),
7143.15.2 by Jelmer Vernooij
Run autopep8.
123
                            help="The public location of this branch.")
6319.2.1 by Jelmer Vernooij
Allow registering custom location aliases.
124
    branch_aliases.register('bound', lambda b: b.get_bound_location(),
7143.15.2 by Jelmer Vernooij
Run autopep8.
125
                            help="The branch this branch is bound to, for bound branches.")
6319.2.1 by Jelmer Vernooij
Allow registering custom location aliases.
126
    branch_aliases.register('push', lambda b: b.get_push_location(),
7143.15.2 by Jelmer Vernooij
Run autopep8.
127
                            help="The saved location used for `brz push` with no arguments.")
6319.2.1 by Jelmer Vernooij
Allow registering custom location aliases.
128
    branch_aliases.register('this', lambda b: b.base,
7143.15.2 by Jelmer Vernooij
Run autopep8.
129
                            help="This branch.")
6319.2.1 by Jelmer Vernooij
Allow registering custom location aliases.
130
7268.11.2 by Jelmer Vernooij
Add purpose argument.
131
    def look_up(self, name, url, purpose=None):
5753.2.2 by Jelmer Vernooij
Remove some unnecessary imports, clean up lazy imports.
132
        branch = _mod_branch.Branch.open_containing('.')[0]
3714.2.2 by Aaron Bentley
Tweak logic to reduce string searching
133
        parts = url.split('/', 1)
134
        if len(parts) == 2:
135
            name, extra = parts
3625.1.1 by Michael Hudson
Allow appending path segments to the :<name> style aliases.
136
        else:
3714.2.2 by Aaron Bentley
Tweak logic to reduce string searching
137
            (name,) = parts
3625.1.1 by Michael Hudson
Allow appending path segments to the :<name> style aliases.
138
            extra = None
3512.2.1 by Aaron Bentley
Add support for branch-associated locations
139
        try:
6319.2.1 by Jelmer Vernooij
Allow registering custom location aliases.
140
            method = self.branch_aliases.get(name[1:])
3512.2.1 by Aaron Bentley
Add support for branch-associated locations
141
        except KeyError:
6734.1.15 by Jelmer Vernooij
Move errors for breezy.directory_service.
142
            raise InvalidLocationAlias(url)
3512.2.1 by Aaron Bentley
Add support for branch-associated locations
143
        else:
6319.2.1 by Jelmer Vernooij
Allow registering custom location aliases.
144
            result = method(branch)
3512.2.1 by Aaron Bentley
Add support for branch-associated locations
145
        if result is None:
6734.1.15 by Jelmer Vernooij
Move errors for breezy.directory_service.
146
            raise UnsetLocationAlias(url)
3625.1.1 by Michael Hudson
Allow appending path segments to the :<name> style aliases.
147
        if extra is not None:
3625.1.2 by Michael Hudson
import urlutils and write urlutils.join rather than join
148
            result = urlutils.join(result, extra)
3512.2.1 by Aaron Bentley
Add support for branch-associated locations
149
        return result
150
6319.2.1 by Jelmer Vernooij
Allow registering custom location aliases.
151
    @classmethod
152
    def help_text(cls, topic):
153
        alias_lines = []
154
        for key in cls.branch_aliases.keys():
155
            help = cls.branch_aliases.get_help(key)
156
            alias_lines.append("  :%-10s%s\n" % (key, help))
157
        return """\
158
Location aliases
159
================
160
161
Bazaar defines several aliases for locations associated with a branch.  These
6681.2.4 by Jelmer Vernooij
More renames.
162
can be used with most commands that expect a location, such as `brz push`.
6319.2.1 by Jelmer Vernooij
Allow registering custom location aliases.
163
164
The aliases are::
165
166
%s
167
For example, to push to the parent location::
168
6681.2.4 by Jelmer Vernooij
More renames.
169
    brz push :parent
6319.2.1 by Jelmer Vernooij
Allow registering custom location aliases.
170
""" % "".join(alias_lines)
171
172
3512.2.1 by Aaron Bentley
Add support for branch-associated locations
173
directories.register(':', AliasDirectory,
174
                     'Easy access to remembered branch locations')
6511.3.1 by Jelmer Vernooij
Add 'co:' directory service.
175
176
7268.11.2 by Jelmer Vernooij
Add purpose argument.
177
class ColocatedDirectory(Directory):
6511.3.1 by Jelmer Vernooij
Add 'co:' directory service.
178
    """Directory lookup for colocated branches.
179
180
    co:somename will resolve to the colocated branch with "somename" in
181
    the current directory.
182
    """
183
7268.11.2 by Jelmer Vernooij
Add purpose argument.
184
    def look_up(self, name, url, purpose=None):
6511.3.1 by Jelmer Vernooij
Add 'co:' directory service.
185
        dir = _mod_controldir.ControlDir.open_containing('.')[0]
7265.6.1 by Jelmer Vernooij
Reformatting.
186
        return urlutils.join_segment_parameters(
187
            dir.user_url, {"branch": urlutils.escape(name)})
6511.3.1 by Jelmer Vernooij
Add 'co:' directory service.
188
189
190
directories.register('co:', ColocatedDirectory,
191
                     'Easy access to colocated branches')