/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/directory_service.py

  • Committer: Robert Collins
  • Date: 2010-05-06 23:41:35 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506234135-yivbzczw1sejxnxc
Lock methods on ``Tree``, ``Branch`` and ``Repository`` are now
expected to return an object which can be used to unlock them. This reduces
duplicate code when using cleanups. The previous 'tokens's returned by
``Branch.lock_write`` and ``Repository.lock_write`` are now attributes
on the result of the lock_write. ``repository.RepositoryWriteLockResult``
and ``branch.BranchWriteLockResult`` document this. (Robert Collins)

``log._get_info_for_log_files`` now takes an add_cleanup callable.
(Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008, 2009, 2011 Canonical Ltd
 
1
# Copyright (C) 2008 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
20
20
to true URLs.  Examples include lp:urls and per-user location aliases.
21
21
"""
22
22
 
23
 
from __future__ import absolute_import
24
 
 
25
 
from . import (
26
 
    errors,
27
 
    registry,
28
 
    )
29
 
from .lazy_import import lazy_import
 
23
from bzrlib import errors, registry
 
24
from bzrlib.lazy_import import lazy_import
30
25
lazy_import(globals(), """
31
 
from breezy import (
32
 
    branch as _mod_branch,
33
 
    controldir as _mod_controldir,
34
 
    urlutils,
35
 
    )
 
26
from bzrlib.branch import Branch
 
27
from bzrlib import urlutils
36
28
""")
37
29
 
38
30
 
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
 
 
59
31
class DirectoryServiceRegistry(registry.Registry):
60
32
    """This object maintains and uses a list of directory services.
61
33
 
67
39
    name and URL, and return a URL.
68
40
    """
69
41
 
70
 
    def dereference(self, url, purpose=None):
 
42
    def dereference(self, url):
71
43
        """Dereference a supplied URL if possible.
72
44
 
73
45
        URLs that match a registered directory service prefix are looked up in
77
49
        requires further dereferencing.
78
50
 
79
51
        :param url: The URL to dereference
80
 
        :param purpose: Purpose of the URL ('read', 'write' or None - if not declared)
81
52
        :return: The dereferenced URL if applicable, the input URL otherwise.
82
53
        """
83
54
        match = self.get_prefix(url)
84
55
        if match is None:
85
56
            return url
86
57
        service, name = match
87
 
        directory = service()
88
 
        try:
89
 
            return directory.look_up(name, url, purpose=purpose)
90
 
        except TypeError:
91
 
            # Compatibility for plugins written for Breezy < 3.0.0
92
 
            return directory.look_up(name, url)
93
 
 
 
58
        return service().look_up(name, url)
94
59
 
95
60
directories = DirectoryServiceRegistry()
96
61
 
97
62
 
98
 
class Directory(object):
99
 
    """Abstract directory lookup class."""
100
 
 
101
 
    def look_up(self, name, url, purpose=None):
102
 
        """Look up an entry in a directory.
103
 
 
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.
108
 
        """
109
 
        raise NotImplementedError(self.look_up)
110
 
 
111
 
 
112
 
class AliasDirectory(Directory):
 
63
class AliasDirectory(object):
113
64
    """Directory lookup for locations associated with a branch.
114
65
 
115
66
    :parent, :submit, :public, :push, :this, and :bound are currently
116
67
    supported.  On error, a subclass of DirectoryLookupFailure will be raised.
117
68
    """
118
69
 
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,
131
 
                            help="This branch.")
132
 
 
133
 
    def look_up(self, name, url, purpose=None):
134
 
        branch = _mod_branch.Branch.open_containing('.')[0]
 
70
    def look_up(self, name, url):
 
71
        branch = Branch.open_containing('.')[0]
 
72
        lookups = {
 
73
            'parent': branch.get_parent,
 
74
            'submit': branch.get_submit_branch,
 
75
            'public': branch.get_public_branch,
 
76
            'bound': branch.get_bound_location,
 
77
            'push': branch.get_push_location,
 
78
            'this': lambda: branch.base
 
79
        }
135
80
        parts = url.split('/', 1)
136
81
        if len(parts) == 2:
137
82
            name, extra = parts
139
84
            (name,) = parts
140
85
            extra = None
141
86
        try:
142
 
            method = self.branch_aliases.get(name[1:])
 
87
            method = lookups[name[1:]]
143
88
        except KeyError:
144
 
            raise InvalidLocationAlias(url)
 
89
            raise errors.InvalidLocationAlias(url)
145
90
        else:
146
 
            result = method(branch)
 
91
            result = method()
147
92
        if result is None:
148
 
            raise UnsetLocationAlias(url)
 
93
            raise errors.UnsetLocationAlias(url)
149
94
        if extra is not None:
150
95
            result = urlutils.join(result, extra)
151
96
        return result
152
97
 
153
 
    @classmethod
154
 
    def help_text(cls, topic):
155
 
        alias_lines = []
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))
159
 
        return """\
160
 
Location aliases
161
 
================
162
 
 
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`.
165
 
 
166
 
The aliases are::
167
 
 
168
 
%s
169
 
For example, to push to the parent location::
170
 
 
171
 
    brz push :parent
172
 
""" % "".join(alias_lines)
173
 
 
174
 
 
175
98
directories.register(':', AliasDirectory,
176
99
                     'Easy access to remembered branch locations')
177
 
 
178
 
 
179
 
class ColocatedDirectory(Directory):
180
 
    """Directory lookup for colocated branches.
181
 
 
182
 
    co:somename will resolve to the colocated branch with "somename" in
183
 
    the current directory.
184
 
    """
185
 
 
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)})
190
 
 
191
 
 
192
 
directories.register('co:', ColocatedDirectory,
193
 
                     'Easy access to colocated branches')