~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/directory_service.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2007-03-28 06:58:22 UTC
  • mfrom: (2379.2.3 hpss-chroot)
  • Revision ID: pqm@pqm.ubuntu.com-20070328065822-999550a858a3ced3
(robertc) Fix chroot urls to not expose the url of the transport they are protecting, allowing regular url operations to work on them. (Robert Collins, Andrew Bennetts)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008, 2009, 2011 Canonical Ltd
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
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
 
 
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
 
 
23
 
from __future__ import absolute_import
24
 
 
25
 
from bzrlib import (
26
 
    errors,
27
 
    registry,
28
 
    )
29
 
from bzrlib.lazy_import import lazy_import
30
 
lazy_import(globals(), """
31
 
from bzrlib import (
32
 
    branch as _mod_branch,
33
 
    controldir as _mod_controldir,
34
 
    urlutils,
35
 
    )
36
 
""")
37
 
 
38
 
 
39
 
class DirectoryServiceRegistry(registry.Registry):
40
 
    """This object maintains and uses a list of directory services.
41
 
 
42
 
    Directory services may be registered via the standard Registry methods.
43
 
    They will be invoked if their key is a prefix of the supplied URL.
44
 
 
45
 
    Each item registered should be a factory of objects that provide a look_up
46
 
    method, as invoked by dereference.  Specifically, look_up should accept a
47
 
    name and URL, and return a URL.
48
 
    """
49
 
 
50
 
    def dereference(self, url):
51
 
        """Dereference a supplied URL if possible.
52
 
 
53
 
        URLs that match a registered directory service prefix are looked up in
54
 
        it.  Non-matching urls are returned verbatim.
55
 
 
56
 
        This is applied only once; the resulting URL must not be one that
57
 
        requires further dereferencing.
58
 
 
59
 
        :param url: The URL to dereference
60
 
        :return: The dereferenced URL if applicable, the input URL otherwise.
61
 
        """
62
 
        match = self.get_prefix(url)
63
 
        if match is None:
64
 
            return url
65
 
        service, name = match
66
 
        return service().look_up(name, url)
67
 
 
68
 
directories = DirectoryServiceRegistry()
69
 
 
70
 
class AliasDirectory(object):
71
 
    """Directory lookup for locations associated with a branch.
72
 
 
73
 
    :parent, :submit, :public, :push, :this, and :bound are currently
74
 
    supported.  On error, a subclass of DirectoryLookupFailure will be raised.
75
 
    """
76
 
 
77
 
    branch_aliases = registry.Registry()
78
 
    branch_aliases.register('parent', lambda b: b.get_parent(),
79
 
        help="The parent of this branch.")
80
 
    branch_aliases.register('submit', lambda b: b.get_submit_branch(),
81
 
        help="The submit branch for this branch.")
82
 
    branch_aliases.register('public', lambda b: b.get_public_branch(),
83
 
        help="The public location of this branch.")
84
 
    branch_aliases.register('bound', lambda b: b.get_bound_location(),
85
 
        help="The branch this branch is bound to, for bound branches.")
86
 
    branch_aliases.register('push', lambda b: b.get_push_location(),
87
 
        help="The saved location used for `bzr push` with no arguments.")
88
 
    branch_aliases.register('this', lambda b: b.base,
89
 
        help="This branch.")
90
 
 
91
 
    def look_up(self, name, url):
92
 
        branch = _mod_branch.Branch.open_containing('.')[0]
93
 
        parts = url.split('/', 1)
94
 
        if len(parts) == 2:
95
 
            name, extra = parts
96
 
        else:
97
 
            (name,) = parts
98
 
            extra = None
99
 
        try:
100
 
            method = self.branch_aliases.get(name[1:])
101
 
        except KeyError:
102
 
            raise errors.InvalidLocationAlias(url)
103
 
        else:
104
 
            result = method(branch)
105
 
        if result is None:
106
 
            raise errors.UnsetLocationAlias(url)
107
 
        if extra is not None:
108
 
            result = urlutils.join(result, extra)
109
 
        return result
110
 
 
111
 
    @classmethod
112
 
    def help_text(cls, topic):
113
 
        alias_lines = []
114
 
        for key in cls.branch_aliases.keys():
115
 
            help = cls.branch_aliases.get_help(key)
116
 
            alias_lines.append("  :%-10s%s\n" % (key, help))
117
 
        return """\
118
 
Location aliases
119
 
================
120
 
 
121
 
Bazaar defines several aliases for locations associated with a branch.  These
122
 
can be used with most commands that expect a location, such as `bzr push`.
123
 
 
124
 
The aliases are::
125
 
 
126
 
%s
127
 
For example, to push to the parent location::
128
 
 
129
 
    bzr push :parent
130
 
""" % "".join(alias_lines)
131
 
 
132
 
 
133
 
directories.register(':', AliasDirectory,
134
 
                     'Easy access to remembered branch locations')
135
 
 
136
 
 
137
 
class ColocatedDirectory(object):
138
 
    """Directory lookup for colocated branches.
139
 
 
140
 
    co:somename will resolve to the colocated branch with "somename" in
141
 
    the current directory.
142
 
    """
143
 
 
144
 
    def look_up(self, name, url):
145
 
        dir = _mod_controldir.ControlDir.open_containing('.')[0]
146
 
        return urlutils.join_segment_parameters(dir.user_url,
147
 
            {"branch": urlutils.escape(name)})
148
 
 
149
 
 
150
 
directories.register('co:', ColocatedDirectory,
151
 
                     'Easy access to colocated branches')
152