~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/plugins/launchpad/lp_directory.py

  • Committer: Jelmer Vernooij
  • Date: 2011-05-10 07:46:15 UTC
  • mfrom: (5844 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5845.
  • Revision ID: jelmer@samba.org-20110510074615-eptod049ndjxc4i7
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2010 Canonical Ltd
 
1
# Copyright (C) 2007-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
65
65
        """See DirectoryService.look_up"""
66
66
        return self._resolve(url)
67
67
 
68
 
    def _resolve(self, url,
69
 
                 _request_factory=ResolveLaunchpadPathRequest,
70
 
                 _lp_login=None):
71
 
        """Resolve the base URL for this transport."""
 
68
    def _resolve_locally(self, path, url, _request_factory):
 
69
        # This is the best I could work out about XMLRPC. If an lp: url
 
70
        # includes ~user, then it is specially validated. Otherwise, it is just
 
71
        # sent to +branch/$path.
 
72
        _, netloc, _, _, _ = urlsplit(url)
 
73
        if netloc == '':
 
74
            netloc = LaunchpadService.DEFAULT_INSTANCE
 
75
        base_url = LaunchpadService.LAUNCHPAD_DOMAINS[netloc]
 
76
        base = 'bzr+ssh://bazaar.%s/' % (base_url,)
 
77
        maybe_invalid = False
 
78
        if path.startswith('~'):
 
79
            # A ~user style path, validate it a bit.
 
80
            # If a path looks fishy, fall back to asking XMLRPC to
 
81
            # resolve it for us. That way we still get their nicer error
 
82
            # messages.
 
83
            parts = path.split('/')
 
84
            if (len(parts) < 3
 
85
                or (parts[1] in ('ubuntu', 'debian') and len(parts) < 5)):
 
86
                # This special case requires 5-parts to be valid.
 
87
                maybe_invalid = True
 
88
        else:
 
89
            base += '+branch/'
 
90
        if maybe_invalid:
 
91
            return self._resolve_via_xmlrpc(path, url, _request_factory)
 
92
        return {'urls': [base + path]}
 
93
 
 
94
    def _resolve_via_xmlrpc(self, path, url, _request_factory):
 
95
        service = LaunchpadService.for_url(url)
 
96
        resolve = _request_factory(path)
 
97
        try:
 
98
            result = resolve.submit(service)
 
99
        except xmlrpclib.Fault, fault:
 
100
            raise errors.InvalidURL(
 
101
                path=url, extra=fault.faultString)
 
102
        return result
 
103
 
 
104
    def _update_url_scheme(self, url):
72
105
        # Do ubuntu: and debianlp: expansions.
73
106
        scheme, netloc, path, query, fragment = urlsplit(url)
74
107
        if scheme in ('ubuntu', 'debianlp'):
106
139
                series=series,
107
140
                project=project)
108
141
            scheme, netloc, path, query, fragment = urlsplit(url)
109
 
        service = LaunchpadService.for_url(url)
110
 
        if _lp_login is None:
111
 
            _lp_login = get_lp_login()
112
 
        path = path.strip('/')
 
142
        return url, path
 
143
 
 
144
    def _expand_user(self, path, url, lp_login):
113
145
        if path.startswith('~/'):
114
 
            if _lp_login is None:
 
146
            if lp_login is None:
115
147
                raise errors.InvalidURL(path=url,
116
148
                    extra='Cannot resolve "~" to your username.'
117
149
                          ' See "bzr help launchpad-login"')
118
 
            path = '~' + _lp_login + path[1:]
119
 
        resolve = _request_factory(path)
120
 
        try:
121
 
            result = resolve.submit(service)
122
 
        except xmlrpclib.Fault, fault:
123
 
            raise errors.InvalidURL(
124
 
                path=url, extra=fault.faultString)
 
150
            path = '~' + lp_login + path[1:]
 
151
        return path
 
152
 
 
153
    def _resolve(self, url,
 
154
                 _request_factory=ResolveLaunchpadPathRequest,
 
155
                 _lp_login=None):
 
156
        """Resolve the base URL for this transport."""
 
157
        url, path = self._update_url_scheme(url)
 
158
        if _lp_login is None:
 
159
            _lp_login = get_lp_login()
 
160
        path = path.strip('/')
 
161
        path = self._expand_user(path, url, _lp_login)
 
162
        if _lp_login is not None:
 
163
            result = self._resolve_locally(path, url, _request_factory)
 
164
            if 'launchpad' in debug.debug_flags:
 
165
                local_res = result
 
166
                result = self._resolve_via_xmlrpc(path, url, _request_factory)
 
167
                trace.note('resolution for %s\n  local: %s\n remote: %s'
 
168
                           % (url, local_res['urls'], result['urls']))
 
169
        else:
 
170
            result = self._resolve_via_xmlrpc(path, url, _request_factory)
125
171
 
126
172
        if 'launchpad' in debug.debug_flags:
127
173
            trace.mutter("resolve_lp_path(%r) == %r", url, result)