1
# Copyright (C) 2011 Canonical Ltd
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.
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.
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
17
"""Tools for dealing with the Launchpad API without using launchpadlib.
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
26
# Use simplejson if available, much faster, and can be easily installed in
27
# older versions of python
28
import simplejson as json
30
# Is present since python 2.6
39
from bzrlib import trace
42
DEFAULT_SERIES = 'oneiric'
44
class LatestPublication(object):
45
"""Encapsulate how to find the latest publication for a given project."""
47
LP_API_ROOT = 'https://api.launchpad.net/1.0'
49
def __init__(self, archive, series, project):
50
self._archive = archive
51
self._project = project
52
self._setup_series_and_pocket(series)
54
def _setup_series_and_pocket(self, series):
55
"""Parse the 'series' info into a series and a pocket.
58
_setup_series_and_pocket('natty-proposed')
64
if self._series is not None and '-' in self._series:
65
self._series, self._pocket = self._series.split('-', 1)
66
self._pocket = self._pocket.title()
68
self._pocket = 'Release'
70
def _archive_URL(self):
71
"""Return the Launchpad 'Archive' URL that we will query.
72
This is everything in the URL except the query parameters.
74
return '%s/%s/+archive/primary' % (self.LP_API_ROOT, self._archive)
76
def _publication_status(self):
77
"""Handle the 'status' field.
78
It seems that Launchpad tracks all 'debian' packages as 'Pending', while
79
for 'ubuntu' we care about the 'Published' packages.
81
if self._archive == 'debian':
82
# Launchpad only tracks debian packages as "Pending", it doesn't mark
87
def _query_params(self):
88
"""Get the parameters defining our query.
89
This defines the actions we are making against the archive.
90
:return: A dict of query parameters.
92
params = {'ws.op': 'getPublishedSources',
93
'exact_match': 'true',
94
# If we need to use "" shouldn't we quote the project somehow?
95
'source_name': '"%s"' % (self._project,),
96
'status': self._publication_status(),
97
# We only need the latest one, the results seem to be properly
98
# most-recent-debian-version sorted
101
if self._series is not None:
102
params['distro_series'] = '/%s/%s' % (self._archive, self._series)
103
if self._pocket is not None:
104
params['pocket'] = self._pocket
107
def _query_URL(self):
108
"""Create the full URL that we need to query, including parameters."""
109
params = self._query_params()
110
# We sort to give deterministic results for testing
111
encoded = urllib.urlencode(sorted(params.items()))
112
return '%s?%s' % (self._archive_URL(), encoded)
114
def _get_lp_info(self):
115
"""Place an actual HTTP query against the Launchpad service."""
118
query_URL = self._query_URL()
120
req = urllib2.Request(query_URL)
121
response = urllib2.urlopen(req)
122
json_info = response.read()
123
# XXX: We haven't tested the HTTPError
124
except (urllib2.URLError, urllib2.HTTPError), e:
125
trace.mutter('failed to place query to %r' % (query_URL,))
126
trace.log_exception_quietly()
130
def _parse_json_info(self, json_info):
131
"""Parse the json response from Launchpad into objects."""
135
return json.loads(json_info)
137
trace.mutter('Failed to parse json info: %r' % (json_info,))
138
trace.log_exception_quietly()
141
def get_latest_version(self):
142
"""Get the latest published version for the given package."""
143
json_info = self._get_lp_info()
144
if json_info is None:
146
info = self._parse_json_info(json_info)
150
entries = info['entries']
151
if len(entries) == 0:
153
return entries[0]['source_package_version']
155
trace.log_exception_quietly()
159
def get_latest_publication(archive, series, project):
160
"""Get the most recent publication for a given project.
162
:param archive: Either 'ubuntu' or 'debian'
163
:param series: Something like 'natty', 'sid', etc. Can be set as None. Can
164
also include a pocket such as 'natty-proposed'.
165
:param project: Something like 'bzr'
166
:return: A version string indicating the most-recent version published in
167
Launchpad. Might return None if there is an error.
169
lp = LatestPublication(archive, series, project)
170
return lp.get_latest_version()