2009-01-19 17:55:00 +00:00
|
|
|
#
|
2009-01-25 10:04:06 +00:00
|
|
|
# libsupport.py - functions which add launchpadlib support to the Ubuntu
|
2009-01-19 17:55:00 +00:00
|
|
|
# Developer Tools package.
|
|
|
|
#
|
2009-01-19 17:57:46 +00:00
|
|
|
# Copyright (C) 2009 Markus Korn <thekorn@gmx.de>
|
2009-01-19 17:55:00 +00:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 3
|
|
|
|
# of the License, or (at your option) any later version.
|
2010-12-03 00:06:43 +01:00
|
|
|
#
|
2009-01-19 17:55:00 +00:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# Please see the /usr/share/common-licenses/GPL file for the full text of
|
|
|
|
# the GNU General Public License license.
|
|
|
|
#
|
|
|
|
|
|
|
|
# Modules.
|
2009-01-21 10:50:26 +00:00
|
|
|
import glob
|
2009-01-19 17:55:00 +00:00
|
|
|
import os
|
2009-05-27 16:48:32 -04:00
|
|
|
import sys
|
2009-01-19 17:55:00 +00:00
|
|
|
import urllib
|
|
|
|
import urlparse
|
2009-05-27 16:48:32 -04:00
|
|
|
import httplib2
|
|
|
|
|
2011-02-24 16:25:20 +02:00
|
|
|
from launchpadlib.launchpad import Launchpad
|
|
|
|
from launchpadlib.errors import HTTPError
|
2009-01-19 17:55:00 +00:00
|
|
|
|
2010-03-20 18:27:31 +01:00
|
|
|
from ubuntutools.lp import (service, api_version)
|
2010-02-12 18:47:02 +01:00
|
|
|
|
2011-02-24 16:25:20 +02:00
|
|
|
def get_launchpad(consumer, server=service, cache=None):
|
|
|
|
return Launchpad.login_with(consumer, server, cache, version=api_version)
|
2010-12-03 00:06:43 +01:00
|
|
|
|
2009-01-19 17:55:00 +00:00
|
|
|
def query_to_dict(query_string):
|
|
|
|
result = dict()
|
|
|
|
options = filter(None, query_string.split("&"))
|
|
|
|
for opt in options:
|
|
|
|
key, value = opt.split("=")
|
|
|
|
result.setdefault(key, set()).add(value)
|
|
|
|
return result
|
2010-12-03 00:06:43 +01:00
|
|
|
|
2009-01-19 17:55:00 +00:00
|
|
|
def translate_web_api(url, launchpad):
|
|
|
|
scheme, netloc, path, query, fragment = urlparse.urlsplit(url)
|
|
|
|
query = query_to_dict(query)
|
2010-09-22 11:41:58 +02:00
|
|
|
|
|
|
|
differences = set(netloc.split('.')).symmetric_difference(
|
|
|
|
set(launchpad._root_uri.host.split('.')))
|
|
|
|
if ('staging' in differences or 'edge' in differences):
|
2010-12-23 20:42:21 +01:00
|
|
|
raise ValueError("url conflict (url: %s, root: %s" %
|
|
|
|
(url, launchpad._root_uri))
|
2009-01-19 17:55:00 +00:00
|
|
|
if path.endswith("/+bugs"):
|
|
|
|
path = path[:-6]
|
|
|
|
if "ws.op" in query:
|
|
|
|
raise ValueError("Invalid web url, url: %s" %url)
|
|
|
|
query["ws.op"] = "searchTasks"
|
|
|
|
scheme, netloc, api_path, _, _ = urlparse.urlsplit(str(launchpad._root_uri))
|
|
|
|
query = urllib.urlencode(query)
|
2010-12-23 20:42:21 +01:00
|
|
|
url = urlparse.urlunsplit((scheme, netloc, api_path + path.lstrip("/"),
|
|
|
|
query, fragment))
|
2009-01-19 17:55:00 +00:00
|
|
|
return url
|
2010-12-03 00:06:43 +01:00
|
|
|
|
2009-01-19 17:55:00 +00:00
|
|
|
def translate_api_web(self_url):
|
2010-03-20 18:27:31 +01:00
|
|
|
return self_url.replace("api.", "").replace("%s/" % (api_version), "")
|
2010-12-03 00:06:43 +01:00
|
|
|
|
2009-01-19 17:55:00 +00:00
|
|
|
LEVEL = {
|
|
|
|
0: "UNAUTHORIZED",
|
|
|
|
1: "READ_PUBLIC",
|
|
|
|
2: "WRITE_PUBLIC",
|
|
|
|
3: "READ_PRIVATE",
|
|
|
|
4: "WRITE_PRIVATE"
|
|
|
|
}
|
2010-12-03 00:06:43 +01:00
|
|
|
|
2009-01-19 17:55:00 +00:00
|
|
|
def approve_application(credentials, email, password, level, web_root,
|
|
|
|
context):
|
|
|
|
authorization_url = credentials.get_request_token(context, web_root)
|
|
|
|
if level in LEVEL:
|
2010-12-23 20:42:21 +01:00
|
|
|
level = 'field.actions.%s' % LEVEL[level]
|
2009-01-19 17:55:00 +00:00
|
|
|
elif level in LEVEL.values():
|
2010-12-23 20:42:21 +01:00
|
|
|
level = 'field.actions.%s' % level
|
|
|
|
elif (str(level).startswith("field.actions") and
|
|
|
|
str(level).split(".")[-1] in LEVEL):
|
2009-01-19 17:55:00 +00:00
|
|
|
pass
|
|
|
|
else:
|
|
|
|
raise ValueError("Unknown access level '%s'" %level)
|
|
|
|
|
|
|
|
params = {level: 1,
|
|
|
|
"oauth_token": credentials._request_token.key,
|
|
|
|
"lp.context": context or ""}
|
2010-12-03 00:06:43 +01:00
|
|
|
|
2009-01-19 17:55:00 +00:00
|
|
|
lp_creds = ":".join((email, password))
|
2010-12-23 20:42:21 +01:00
|
|
|
basic_auth = "Basic %s" % (lp_creds.encode('base64'))
|
2009-01-19 17:55:00 +00:00
|
|
|
headers = {'Authorization': basic_auth}
|
|
|
|
response, content = httplib2.Http().request(authorization_url,
|
|
|
|
method="POST", body=urllib.urlencode(params), headers=headers)
|
|
|
|
if int(response["status"]) != 200:
|
|
|
|
if not 300 <= int(response["status"]) <= 400: # this means redirection
|
|
|
|
raise HTTPError(response, content)
|
|
|
|
credentials.exchange_request_token_for_access_token(web_root)
|
|
|
|
return credentials
|