/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4797.89.1 by John Arbash Meinel
Cherrypick the launchpad plugin changes for bug #609187.
1
# Copyright (C) 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
"""Tools for dealing with the Launchpad API without using launchpadlib.
18
19
The api itself is a RESTful interface, so we can make HTTP queries directly.
20
loading launchpadlib itself has a fairly high overhead (just calling
21
Launchpad.login_anonymously() takes a 500ms once the WADL is cached, and 5+s to
22
get the WADL.
23
"""
24
25
try:
26
    # Use simplejson if available, much faster, and can be easily installed in
27
    # older versions of python
28
    import simplejson as json
29
except ImportError:
30
    # Is present since python 2.6
31
    try:
32
        import json
33
    except ImportError:
34
        json = None
35
36
import time
37
import urllib
38
import urllib2
39
40
from bzrlib import (
41
    revision,
42
    trace,
43
    )
44
45
46
class LatestPublication(object):
47
    """Encapsulate how to find the latest publication for a given project."""
48
49
    LP_API_ROOT = 'https://api.launchpad.net/1.0'
50
51
    def __init__(self, archive, series, project):
52
        self._archive = archive
53
        self._project = project
54
        self._setup_series_and_pocket(series)
55
56
    def _setup_series_and_pocket(self, series):
57
        """Parse the 'series' info into a series and a pocket.
58
59
        eg::
60
            _setup_series_and_pocket('natty-proposed')
61
            => _series == 'natty'
62
               _pocket == 'Proposed'
63
        """
64
        self._series = series
65
        self._pocket = None
66
        if self._series is not None and '-' in self._series:
67
            self._series, self._pocket = self._series.split('-', 1)
68
            self._pocket = self._pocket.title()
69
        else:
70
            self._pocket = 'Release'
71
72
    def _archive_URL(self):
73
        """Return the Launchpad 'Archive' URL that we will query.
74
        This is everything in the URL except the query parameters.
75
        """
76
        return '%s/%s/+archive/primary' % (self.LP_API_ROOT, self._archive)
77
78
    def _publication_status(self):
79
        """Handle the 'status' field.
80
        It seems that Launchpad tracks all 'debian' packages as 'Pending', while
81
        for 'ubuntu' we care about the 'Published' packages.
82
        """
83
        if self._archive == 'debian':
84
            # Launchpad only tracks debian packages as "Pending", it doesn't mark
85
            # them Published
86
            return 'Pending'
87
        return 'Published'
88
89
    def _query_params(self):
90
        """Get the parameters defining our query.
91
        This defines the actions we are making against the archive.
92
        :return: A dict of query parameters.
93
        """
94
        params = {'ws.op': 'getPublishedSources',
95
                  'exact_match': 'true',
96
                  # If we need to use "" shouldn't we quote the project somehow?
97
                  'source_name': '"%s"' % (self._project,),
98
                  'status': self._publication_status(),
99
                  # We only need the latest one, the results seem to be properly
100
                  # most-recent-debian-version sorted
101
                  'ws.size': '1',
102
        }
103
        if self._series is not None:
104
            params['distro_series'] = '/%s/%s' % (self._archive, self._series)
105
        if self._pocket is not None:
106
            params['pocket'] = self._pocket
107
        return params
108
109
    def _query_URL(self):
110
        """Create the full URL that we need to query, including parameters."""
111
        params = self._query_params()
112
        # We sort to give deterministic results for testing
113
        encoded = urllib.urlencode(sorted(params.items()))
114
        return '%s?%s' % (self._archive_URL(), encoded)
115
116
    def _get_lp_info(self):
117
        """Place an actual HTTP query against the Launchpad service."""
118
        if json is None:
119
            return None
120
        query_URL = self._query_URL()
121
        try:
122
            req = urllib2.Request(query_URL)
123
            response = urllib2.urlopen(req)
124
            json_info = response.read()
125
        # TODO: We haven't tested the HTTPError
126
        except (urllib2.URLError, urllib2.HTTPError), e:
127
            trace.mutter('failed to place query to %r' % (query_URL,))
128
            trace.log_exception_quietly()
129
            return None
130
        return json_info
131
132
    def _parse_json_info(self, json_info):
133
        """Parse the json response from Launchpad into objects."""
134
        if json is None:
135
            return None
136
        try:
137
            return json.loads(json_info)
138
        except Exception:
139
            trace.mutter('Failed to parse json info: %r' % (json_info,))
140
            trace.log_exception_quietly()
141
            return None
142
143
    def get_latest_version(self):
144
        """Get the latest published version for the given package."""
145
        json_info = self._get_lp_info()
146
        if json_info is None:
147
            return None
148
        info = self._parse_json_info(json_info)
149
        if info is None:
150
            return None
151
        try:
152
            entries = info['entries']
153
            if len(entries) == 0:
154
                return None
155
            return entries[0]['source_package_version']
156
        except KeyError:
157
            trace.log_exception_quietly()
158
            return None
159
160
    def place(self):
161
        """Text-form for what location this represents.
162
163
        Example::
164
            ubuntu, natty => Ubuntu Natty
165
            ubuntu, natty-proposed => Ubuntu Natty Proposed
166
        :return: A string representing the location we are checking.
167
        """
168
        place = self._archive
169
        if self._series is not None:
170
            place = '%s %s' % (place, self._series)
171
        if self._pocket is not None and self._pocket != 'Release':
172
            place = '%s %s' % (place, self._pocket)
173
        return place.title()
174
175
176
def get_latest_publication(archive, series, project):
177
    """Get the most recent publication for a given project.
178
179
    :param archive: Either 'ubuntu' or 'debian'
180
    :param series: Something like 'natty', 'sid', etc. Can be set as None. Can
181
        also include a pocket such as 'natty-proposed'.
182
    :param project: Something like 'bzr'
183
    :return: A version string indicating the most-recent version published in
184
        Launchpad. Might return None if there is an error.
185
    """
186
    lp = LatestPublication(archive, series, project)
187
    return lp.get_latest_version()
188
189
190
def get_most_recent_tag(tag_dict, the_branch):
191
    """Get the most recent revision that has been tagged."""
192
    # Note: this assumes that a given rev won't get tagged multiple times. But
193
    #       it should be valid for the package importer branches that we care
194
    #       about
195
    reverse_dict = dict((rev, tag) for tag, rev in tag_dict.iteritems())
196
    the_branch.lock_read()
197
    try:
198
        history = the_branch.repository.iter_reverse_revision_history(
199
            the_branch.last_revision())
200
        for rev_id in history:
201
            if rev_id in reverse_dict:
202
                return reverse_dict[rev_id]
203
    finally:
204
        the_branch.unlock()
205
206
207
def _get_newest_versions(the_branch, latest_pub):
208
    """Get information about how 'fresh' this packaging branch is.
209
210
    :param the_branch: The Branch to check
211
    :param latest_pub: The LatestPublication used to check most recent
212
        published version.
213
    :return: (latest_ver, branch_latest_ver)
214
    """
215
    t = time.time()
216
    latest_ver = latest_pub.get_latest_version()
217
    t_latest_ver = time.time() - t
218
    trace.mutter('LatestPublication.get_latest_version took: %.3fs'
219
                 % (t_latest_ver,))
220
    if latest_ver is None:
221
        return None, None
222
    t = time.time()
223
    tags = the_branch.tags.get_tag_dict()
224
    t_tag_dict = time.time() - t
225
    trace.mutter('LatestPublication.get_tag_dict took: %.3fs' % (t_tag_dict,))
226
    if latest_ver in tags:
227
        # branch might have a newer tag, but we don't really care
228
        return latest_ver, latest_ver
229
    else:
230
        best_tag = get_most_recent_tag(tags, the_branch)
231
        return latest_ver, best_tag
232
233
234
def _report_freshness(latest_ver, branch_latest_ver, place, verbosity,
235
                      report_func):
236
    """Report if the branch is up-to-date."""
237
    if latest_ver is None:
238
        if verbosity == 'all':
239
            report_func('Most recent %s version: MISSING' % (place,))
240
        elif verbosity == 'short':
241
            report_func('%s is MISSING a version' % (place,))
242
        return
243
    elif latest_ver == branch_latest_ver:
244
        if verbosity == 'minimal':
245
            return
246
        elif verbosity == 'short':
247
            report_func('%s is CURRENT in %s' % (latest_ver, place))
248
        else:
249
            report_func('Most recent %s version: %s\n'
250
                       'Packaging branch status: CURRENT'
251
                       % (place, latest_ver))
252
    else:
253
        if verbosity in ('minimal', 'short'):
254
            if branch_latest_ver is None:
255
                branch_latest_ver = 'Branch'
256
            report_func('%s is OUT-OF-DATE, %s has %s'
257
                        % (branch_latest_ver, place, latest_ver))
258
        else:
259
            report_func('Most recent %s version: %s\n'
260
                        'Packaging branch version: %s\n'
261
                        'Packaging branch status: OUT-OF-DATE'
262
                        % (place, latest_ver, branch_latest_ver))
263
264
265
def report_freshness(the_branch, verbosity, latest_pub):
266
    """Report to the user how up-to-date the packaging branch is.
267
268
    :param the_branch: A Branch object
269
    :param verbosity: Can be one of:
270
        off: Do not print anything, and skip all checks.
271
        all: Print all information that we have in a verbose manner, this
272
             includes misses, etc.
273
        short: Print information, but only one-line summaries
274
        minimal: Only print a one-line summary when the package branch is
275
                 out-of-date
276
    :param latest_pub: A LatestPublication instance
277
    """
278
    if verbosity == 'off':
279
        return
280
    if verbosity is None:
281
        verbosity = 'all'
282
    latest_ver, branch_ver = _get_newest_versions(the_branch, latest_pub)
283
    place = latest_pub.place()
284
    _report_freshness(latest_ver, branch_ver, place, verbosity,
285
                      trace.note)