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