/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 breezy/directory_service.py

  • Committer: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2020-08-23 01:15:41 UTC
  • mfrom: (7520.1.4 merge-3.1)
  • Revision ID: breezy.the.bot@gmail.com-20200823011541-nv0oh7nzaganx2qy
Merge lp:brz/3.1.

Merged from https://code.launchpad.net/~jelmer/brz/merge-3.1/+merge/389690

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008 Canonical Ltd
 
1
# Copyright (C) 2008, 2009, 2011 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 bzrlib import errors, registry
24
 
from bzrlib.lazy_import import lazy_import
 
23
from . import (
 
24
    errors,
 
25
    registry,
 
26
    )
 
27
from .lazy_import import lazy_import
25
28
lazy_import(globals(), """
26
 
from bzrlib.branch import Branch
27
 
from bzrlib import urlutils
 
29
from breezy import (
 
30
    branch as _mod_branch,
 
31
    controldir as _mod_controldir,
 
32
    urlutils,
 
33
    )
28
34
""")
29
35
 
30
36
 
 
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
 
31
57
class DirectoryServiceRegistry(registry.Registry):
32
58
    """This object maintains and uses a list of directory services.
33
59
 
39
65
    name and URL, and return a URL.
40
66
    """
41
67
 
42
 
    def dereference(self, url):
 
68
    def dereference(self, url, purpose=None):
43
69
        """Dereference a supplied URL if possible.
44
70
 
45
71
        URLs that match a registered directory service prefix are looked up in
49
75
        requires further dereferencing.
50
76
 
51
77
        :param url: The URL to dereference
 
78
        :param purpose: Purpose of the URL ('read', 'write' or None - if not declared)
52
79
        :return: The dereferenced URL if applicable, the input URL otherwise.
53
80
        """
54
81
        match = self.get_prefix(url)
55
82
        if match is None:
56
83
            return url
57
84
        service, name = match
58
 
        return service().look_up(name, url)
 
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)
 
91
 
59
92
 
60
93
directories = DirectoryServiceRegistry()
61
94
 
62
95
 
63
 
class AliasDirectory(object):
 
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
 
104
        :param purpose: Purpose of the URL ('read', 'write' or None - if not declared)
 
105
        :return: The dereferenced URL if applicable, the input URL otherwise.
 
106
        """
 
107
        raise NotImplementedError(self.look_up)
 
108
 
 
109
 
 
110
class AliasDirectory(Directory):
64
111
    """Directory lookup for locations associated with a branch.
65
112
 
66
113
    :parent, :submit, :public, :push, :this, and :bound are currently
67
114
    supported.  On error, a subclass of DirectoryLookupFailure will be raised.
68
115
    """
69
116
 
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
 
        }
 
117
    branch_aliases = registry.Registry()
 
118
    branch_aliases.register('parent', lambda b: b.get_parent(),
 
119
                            help="The parent of this branch.")
 
120
    branch_aliases.register('submit', lambda b: b.get_submit_branch(),
 
121
                            help="The submit branch for this branch.")
 
122
    branch_aliases.register('public', lambda b: b.get_public_branch(),
 
123
                            help="The public location of this branch.")
 
124
    branch_aliases.register('bound', lambda b: b.get_bound_location(),
 
125
                            help="The branch this branch is bound to, for bound branches.")
 
126
    branch_aliases.register('push', lambda b: b.get_push_location(),
 
127
                            help="The saved location used for `brz push` with no arguments.")
 
128
    branch_aliases.register('this', lambda b: b.base,
 
129
                            help="This branch.")
 
130
 
 
131
    def look_up(self, name, url, purpose=None):
 
132
        branch = _mod_branch.Branch.open_containing('.')[0]
80
133
        parts = url.split('/', 1)
81
134
        if len(parts) == 2:
82
135
            name, extra = parts
84
137
            (name,) = parts
85
138
            extra = None
86
139
        try:
87
 
            method = lookups[name[1:]]
 
140
            method = self.branch_aliases.get(name[1:])
88
141
        except KeyError:
89
 
            raise errors.InvalidLocationAlias(url)
 
142
            raise InvalidLocationAlias(url)
90
143
        else:
91
 
            result = method()
 
144
            result = method(branch)
92
145
        if result is None:
93
 
            raise errors.UnsetLocationAlias(url)
 
146
            raise UnsetLocationAlias(url)
94
147
        if extra is not None:
95
148
            result = urlutils.join(result, extra)
96
149
        return result
97
150
 
 
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
 
162
can be used with most commands that expect a location, such as `brz push`.
 
163
 
 
164
The aliases are::
 
165
 
 
166
%s
 
167
For example, to push to the parent location::
 
168
 
 
169
    brz push :parent
 
170
""" % "".join(alias_lines)
 
171
 
 
172
 
98
173
directories.register(':', AliasDirectory,
99
174
                     'Easy access to remembered branch locations')
 
175
 
 
176
 
 
177
class ColocatedDirectory(Directory):
 
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
 
 
184
    def look_up(self, name, url, purpose=None):
 
185
        dir = _mod_controldir.ControlDir.open_containing('.')[0]
 
186
        return urlutils.join_segment_parameters(
 
187
            dir.user_url, {"branch": urlutils.escape(name)})
 
188
 
 
189
 
 
190
directories.register('co:', ColocatedDirectory,
 
191
                     'Easy access to colocated branches')