2009-01-25 10:04:06 +00:00
|
|
|
#
|
|
|
|
# functions.py - various Launchpad-related functions for the Ubuntu Developer
|
|
|
|
# Tools package
|
|
|
|
#
|
|
|
|
# Copyright (C) 2008, 2009 Jonathan Davies <jpds@ubuntu.com>
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
|
2009-02-06 12:13:59 +01:00
|
|
|
import urllib2
|
|
|
|
import sys
|
2009-05-17 16:06:26 +01:00
|
|
|
from udtexceptions import PackageNotFoundException, SeriesNotFoundException
|
2009-06-11 20:23:30 +02:00
|
|
|
from lpapiwrapper import Launchpad, LpApiWrapper
|
2009-02-19 23:59:12 +01:00
|
|
|
import launchpadlib
|
|
|
|
from re import findall
|
2009-06-11 20:23:30 +02:00
|
|
|
import warnings
|
|
|
|
|
|
|
|
# http://wiki.python.org/moin/PythonDecoratorLibrary#GeneratingDeprecationWarnings
|
|
|
|
def deprecated(func):
|
|
|
|
"""This is a decorator which can be used to mark functions
|
|
|
|
as deprecated. It will result in a warning being emitted
|
|
|
|
when the function is used."""
|
|
|
|
def new_func(*args, **kwargs):
|
|
|
|
warnings.warn("Call to deprecated function %s." % func.__name__, DeprecationWarning, 2)
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
new_func.__name__ = func.__name__
|
|
|
|
new_func.__doc__ = func.__doc__
|
|
|
|
new_func.__dict__.update(func.__dict__)
|
|
|
|
return new_func
|
|
|
|
|
2009-01-19 17:55:00 +00:00
|
|
|
|
2009-06-11 16:33:47 +02:00
|
|
|
# Singleton to access LP API
|
|
|
|
launchpad = Launchpad
|
2009-05-09 20:09:56 +01:00
|
|
|
|
2009-06-09 10:34:21 +02:00
|
|
|
def _ubuntuSourcePackage(package, series, pocket = 'Release'):
|
2009-05-09 20:09:56 +01:00
|
|
|
""" Finds an Ubuntu source package on LP
|
|
|
|
|
|
|
|
returns LP API repr of the source package
|
|
|
|
If the package does not exist: raise PackageNotFoundException
|
|
|
|
"""
|
2009-06-11 20:23:30 +02:00
|
|
|
lpapiwrapper = LpApiWrapper()
|
|
|
|
return lpapiwrapper.getUbuntuSourcePackage(package, series, pocket)
|
2009-05-09 20:09:56 +01:00
|
|
|
|
2009-06-11 20:36:11 +02:00
|
|
|
def packageVersion(package, series=None):
|
2009-05-09 20:09:56 +01:00
|
|
|
""" Retrieves the version of a given source package in the current
|
|
|
|
development distroseries
|
|
|
|
|
|
|
|
returns unicode string repr of source package version
|
|
|
|
If the package does not exist: raise PackageNotFoundException
|
|
|
|
"""
|
2009-06-11 20:36:11 +02:00
|
|
|
if not series:
|
|
|
|
series = LpApiWrapper.getUbuntuDevelopmentSeries()
|
2009-05-09 20:09:56 +01:00
|
|
|
|
|
|
|
return _ubuntuSourcePackage(package, series).source_package_version
|
|
|
|
|
2009-06-11 20:36:11 +02:00
|
|
|
def packageComponent(package, series=None):
|
2009-05-09 20:09:56 +01:00
|
|
|
""" Retrieves the component for a given source package
|
|
|
|
|
|
|
|
returns unicode string representation of component
|
|
|
|
If the package does not exist: raise PackageNotFoundException
|
|
|
|
"""
|
2009-06-11 20:36:11 +02:00
|
|
|
if not series:
|
|
|
|
series = LpApiWrapper.getUbuntuDevelopmentSeries()
|
2009-05-09 20:09:56 +01:00
|
|
|
|
|
|
|
return _ubuntuSourcePackage(package, series).component_name
|
|
|
|
|
2009-06-11 20:36:11 +02:00
|
|
|
def canUploadPackage(package, series=None):
|
2009-05-09 20:09:56 +01:00
|
|
|
""" Checks whether the user can upload package to Ubuntu's main archive
|
|
|
|
|
|
|
|
Uses LP API to do this.
|
|
|
|
|
|
|
|
If the user can upload the package: return True.
|
|
|
|
If the user cannot upload the package: return False.
|
|
|
|
If the package does not exist: raise PackageNotFoundException
|
|
|
|
"""
|
2009-06-11 20:36:11 +02:00
|
|
|
if not series:
|
|
|
|
series = LpApiWrapper.getUbuntuDevelopmentSeries()
|
2009-05-09 20:09:56 +01:00
|
|
|
|
2009-06-11 20:23:30 +02:00
|
|
|
u_archive = LpApiWrapper.getUbuntuArchive()
|
2009-05-09 20:09:56 +01:00
|
|
|
|
|
|
|
uploaders = u_archive.getUploadersForComponent(component_name=packageComponent(package, series))
|
|
|
|
|
|
|
|
for permission in uploaders:
|
|
|
|
current_uploader = permission.person
|
2009-05-17 16:06:26 +01:00
|
|
|
if _findMember(current_uploader):
|
2009-05-09 20:09:56 +01:00
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
2009-05-17 16:06:26 +01:00
|
|
|
def _findMember(haystack):
|
2009-05-09 20:09:56 +01:00
|
|
|
""" Find a person in a haystack. A haystack can consist of either people or teams.
|
|
|
|
|
|
|
|
If the needle is in the haystack: return True
|
|
|
|
If the needle is not in the haystack: return False
|
|
|
|
"""
|
|
|
|
|
|
|
|
if not haystack.is_team:
|
2009-05-17 16:06:26 +01:00
|
|
|
return (str(haystack) == str(launchpad.me))
|
2009-05-09 21:08:46 +01:00
|
|
|
elif haystack.is_valid: # is a team
|
2009-05-17 16:06:26 +01:00
|
|
|
return isLPTeamMember(haystack.name)
|
2009-05-09 20:09:56 +01:00
|
|
|
|
|
|
|
return False
|
|
|
|
|
2009-01-19 17:55:00 +00:00
|
|
|
def isLPTeamMember(team):
|
|
|
|
""" Checks if the user is a member of a certain team on Launchpad.
|
|
|
|
|
2009-05-09 20:09:56 +01:00
|
|
|
Uses the LP API.
|
2009-01-19 17:55:00 +00:00
|
|
|
|
|
|
|
If the user is a member of the team: return True.
|
|
|
|
If the user is not a member of the team: return False.
|
|
|
|
"""
|
|
|
|
|
2009-05-10 17:41:03 -04:00
|
|
|
return any(t.name == team for t in launchpad.me.super_teams)
|
2009-02-19 23:59:12 +01:00
|
|
|
|
|
|
|
def isPerPackageUploader(package):
|
|
|
|
# Checks if the user has upload privileges for a certain package.
|
|
|
|
|
|
|
|
me = findall('~(\S+)', '%s' % launchpad.me)[0]
|
2009-06-11 20:23:30 +02:00
|
|
|
main_archive = LpApiWrapper.getUbuntuArchive()
|
2009-02-19 23:59:12 +01:00
|
|
|
try:
|
|
|
|
perms = main_archive.getUploadersForPackage(source_package_name=package)
|
|
|
|
except launchpadlib.errors.HTTPError:
|
|
|
|
return False
|
|
|
|
for perm in perms:
|
|
|
|
if perm.person.name == me:
|
|
|
|
return True
|
|
|
|
|