3
0
mirror of https://git.launchpad.net/ubuntu-dev-tools synced 2025-03-13 08:01:09 +00:00
Anders Kaseorg f762242ebb * ubuntutools/lp/functions.py:
- Simplify isLPTeamMember.

<Laney> andersk: what does super_teams do?
<andersk> Laney: I found super_teams using dir(launchpad.me).  It
returns the collection of teams that the person is a member of.  I'm
not sure it's actually documented.
<Laney> andersk: I've found the documentation to be a bit
lacking. Does it count indirect memberships too?
<andersk> Laney: yes, it appears to.
<Laney> andersk: If that's right then your version is better than mine
<Laney> andersk: Could you prepare a branch for merging? I'd just do
it directly but I'm a bit busy now so can't, and I'm afraid I'll
forget
2009-05-10 17:41:03 -04:00

163 lines
5.4 KiB
Python

#
# 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.
#
import cookie
import urlopener as lp_urlopener
import urllib2
import sys
from udtexceptions import PackageNotFoundException, TeamNotFoundException, SeriesNotFoundException
import libsupport as lp_libsupport
import launchpadlib
from re import findall
# Takes time to initialise - move to top level so we only pay the penalty
# once. Should probably make this a proper class so we can instansiate
# singleton-style (lazily).
launchpad = lp_libsupport.get_launchpad("ubuntu-dev-tools")
def ubuntuDevelopmentSeries():
""" Get the string repr of the current Ubuntu development series """
ubuntu = launchpad.distributions['ubuntu']
return ubuntu.current_series.name
def _ubuntuSeries(name):
""" Get the LP representation of a series
returns the LP API repr of a series passed by name (e.g. 'karmic')
If the series is not found: raise SeriesNotFoundException
"""
ubuntu = launchpad.distributions['ubuntu']
try:
return ubuntu.getSeries(name_or_version=name)
except launchpadlib.errors.HTTPError:
raise SeriesNotFoundException('The series %s was not found' % name)
def _ubuntuSourcePackage(package, series):
""" Finds an Ubuntu source package on LP
returns LP API repr of the source package
If the package does not exist: raise PackageNotFoundException
"""
try:
lpseries = _ubuntuSeries(series)
ubuntu = launchpad.distributions['ubuntu']
u_archive = ubuntu.main_archive
component = u_archive.getPublishedSources(source_name=package, status="Published",
exact_match=True, distro_series=lpseries)[0]
return component
except IndexError:
raise PackageNotFoundException("The package %s does not exist in the Ubuntu main archive" %
package)
def packageVersion(package, series=ubuntuDevelopmentSeries()):
""" 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
"""
return _ubuntuSourcePackage(package, series).source_package_version
def packageComponent(package, series=ubuntuDevelopmentSeries()):
""" Retrieves the component for a given source package
returns unicode string representation of component
If the package does not exist: raise PackageNotFoundException
"""
return _ubuntuSourcePackage(package, series).component_name
def canUploadPackage(package, series=ubuntuDevelopmentSeries()):
""" 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
"""
ubuntu = launchpad.distributions['ubuntu']
u_archive = ubuntu.main_archive
uploaders = u_archive.getUploadersForComponent(component_name=packageComponent(package, series))
for permission in uploaders:
current_uploader = permission.person
if _findMember(current_uploader, launchpad.me):
return True
return False
def _findMember(haystack, needle):
""" 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:
return (str(haystack) == str(needle))
elif haystack.is_valid: # is a team
members = haystack.members
for m in members:
if _findMember(m, needle):
return True
return False
def isLPTeamMember(team):
""" Checks if the user is a member of a certain team on Launchpad.
Uses the LP API.
If the user is a member of the team: return True.
If the user is not a member of the team: return False.
"""
return any(t.name == team for t in launchpad.me.super_teams)
def isPerPackageUploader(package):
# Checks if the user has upload privileges for a certain package.
me = findall('~(\S+)', '%s' % launchpad.me)[0]
main_archive = launchpad.distributions["ubuntu"].main_archive
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