1
# Copyright (C) 2008, 2009, 2011 Canonical Ltd
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.
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.
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
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Directory service registration and usage.
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.
23
from __future__ import absolute_import
29
from .lazy_import import lazy_import
30
lazy_import(globals(), """
32
branch as _mod_branch,
33
controldir as _mod_controldir,
39
class DirectoryLookupFailure(errors.BzrError):
40
"""Base type for lookup errors."""
43
class InvalidLocationAlias(DirectoryLookupFailure):
45
_fmt = '"%(alias_name)s" is not a valid location alias.'
47
def __init__(self, alias_name):
48
DirectoryLookupFailure.__init__(self, alias_name=alias_name)
51
class UnsetLocationAlias(DirectoryLookupFailure):
53
_fmt = 'No %(alias_name)s location assigned.'
55
def __init__(self, alias_name):
56
DirectoryLookupFailure.__init__(self, alias_name=alias_name[1:])
59
class DirectoryServiceRegistry(registry.Registry):
60
"""This object maintains and uses a list of directory services.
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.
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.
70
def dereference(self, url, purpose=None):
71
"""Dereference a supplied URL if possible.
73
URLs that match a registered directory service prefix are looked up in
74
it. Non-matching urls are returned verbatim.
76
This is applied only once; the resulting URL must not be one that
77
requires further dereferencing.
79
:param url: The URL to dereference
80
:param purpose: Purpose of the URL ('read', 'write' or None - if not declared)
81
:return: The dereferenced URL if applicable, the input URL otherwise.
83
match = self.get_prefix(url)
89
return directory.look_up(name, url, purpose=purpose)
91
# Compatibility for plugins written for Breezy < 3.0.0
92
return directory.look_up(name, url)
95
directories = DirectoryServiceRegistry()
98
class Directory(object):
99
"""Abstract directory lookup class."""
101
def look_up(self, name, url, purpose=None):
102
"""Look up an entry in a directory.
104
:param name: Directory name
105
:param url: The URL to dereference
106
:param purpose: Purpose of the URL ('read', 'write' or None - if not declared)
107
:return: The dereferenced URL if applicable, the input URL otherwise.
109
raise NotImplementedError(self.look_up)
112
class AliasDirectory(Directory):
113
"""Directory lookup for locations associated with a branch.
115
:parent, :submit, :public, :push, :this, and :bound are currently
116
supported. On error, a subclass of DirectoryLookupFailure will be raised.
119
branch_aliases = registry.Registry()
120
branch_aliases.register('parent', lambda b: b.get_parent(),
121
help="The parent of this branch.")
122
branch_aliases.register('submit', lambda b: b.get_submit_branch(),
123
help="The submit branch for this branch.")
124
branch_aliases.register('public', lambda b: b.get_public_branch(),
125
help="The public location of this branch.")
126
branch_aliases.register('bound', lambda b: b.get_bound_location(),
127
help="The branch this branch is bound to, for bound branches.")
128
branch_aliases.register('push', lambda b: b.get_push_location(),
129
help="The saved location used for `brz push` with no arguments.")
130
branch_aliases.register('this', lambda b: b.base,
133
def look_up(self, name, url, purpose=None):
134
branch = _mod_branch.Branch.open_containing('.')[0]
135
parts = url.split('/', 1)
142
method = self.branch_aliases.get(name[1:])
144
raise InvalidLocationAlias(url)
146
result = method(branch)
148
raise UnsetLocationAlias(url)
149
if extra is not None:
150
result = urlutils.join(result, extra)
154
def help_text(cls, topic):
156
for key in cls.branch_aliases.keys():
157
help = cls.branch_aliases.get_help(key)
158
alias_lines.append(" :%-10s%s\n" % (key, help))
163
Bazaar defines several aliases for locations associated with a branch. These
164
can be used with most commands that expect a location, such as `brz push`.
169
For example, to push to the parent location::
172
""" % "".join(alias_lines)
175
directories.register(':', AliasDirectory,
176
'Easy access to remembered branch locations')
179
class ColocatedDirectory(Directory):
180
"""Directory lookup for colocated branches.
182
co:somename will resolve to the colocated branch with "somename" in
183
the current directory.
186
def look_up(self, name, url, purpose=None):
187
dir = _mod_controldir.ControlDir.open_containing('.')[0]
188
return urlutils.join_segment_parameters(
189
dir.user_url, {"branch": urlutils.escape(name)})
192
directories.register('co:', ColocatedDirectory,
193
'Easy access to colocated branches')