From f5c6695e13d1b7ed1b45036f0c853255ca4e4e3c Mon Sep 17 00:00:00 2001 From: Michael Bienia Date: Sat, 20 Feb 2010 18:13:15 +0100 Subject: [PATCH] ubuntutools/lp/lpapicache.py: * Make PersonTeam.getMe() a class property (PersonTeam.me). ubuntutools/requestsync/lp.py, ubuntu-build: * Update for the above mentioned change. --- ubuntu-build | 4 ++-- ubuntutools/lp/lpapicache.py | 32 ++++++++++++++++++++------------ ubuntutools/requestsync/lp.py | 4 ++-- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/ubuntu-build b/ubuntu-build index 93fa7de..cc7f93d 100755 --- a/ubuntu-build +++ b/ubuntu-build @@ -145,7 +145,7 @@ if not options.batch: # Operations that are remaining may only be done by Ubuntu developers (retry) # or buildd admins (rescore). Check if the proper permissions are in place. - me = PersonTeam.getMe() + me = PersonTeam.me if op == "rescore": necessaryPrivs = me.isLpTeamMember('launchpad-buildd-admins') if op == "retry": necessaryPrivs = me.canUploadPackage( ubuntu_archive, distroseries, sources.getPackageName(), sources.getComponent()) @@ -216,7 +216,7 @@ if release and '-' in release: sys.exit(1) ubuntu_archive = Distribution('ubuntu').getArchive() -me = PersonTeam.getMe() +me = PersonTeam.me # Check permisions (part 1): Rescoring can only be done by buildd admins can_rescore = options.priority and me.isLpTeamMember('launchpad-buildd-admins') or False diff --git a/ubuntutools/lp/lpapicache.py b/ubuntutools/lp/lpapicache.py index 5c5a42a..da7ee31 100644 --- a/ubuntutools/lp/lpapicache.py +++ b/ubuntutools/lp/lpapicache.py @@ -73,7 +73,7 @@ class MetaWrapper(type): def __init__(cls, name, bases, attrd): super(MetaWrapper, cls).__init__(name, bases, attrd) if 'resource_type' not in attrd: - raise TypeError('Class needs an associated resource type') + raise TypeError('Class "%s" needs an associated resource type' % name) cls._cache = dict() @@ -367,17 +367,34 @@ class SourcePackagePublishingHistory(BaseWrapper): self.getPackageName(), '\n'.join(res)) +class MetaPersonTeam(MetaWrapper): + @property + def me(cls): + '''The PersonTeam object of the currently authenticated LP user or + None when anonymously logged in. + ''' + if '_me' not in cls.__dict__: + try: + cls._me = PersonTeam(Launchpad.me) + except HTTPError, error: + if error.response.status == 401: + # Anonymous login + cls._me = None + else: + raise + return cls._me + class PersonTeam(BaseWrapper): ''' Wrapper class around a LP person or team object. ''' + __metaclass__ = MetaPersonTeam + resource_type = ( lookup_service_root(service) + 'beta/#person', lookup_service_root(service) + 'beta/#team', ) - _me = None # the PersonTeam object of the currently authenticated LP user - def __init__(self, *args): # Don't share _upload_{pkg,comp} between different PersonTeams if '_upload_pkg' not in self.__dict__: @@ -403,15 +420,6 @@ class PersonTeam(BaseWrapper): cached = PersonTeam(Launchpad.people[person_or_team]) return cached - @classmethod - def getMe(cls): - ''' - Returns a PersonTeam object of the currently authenticated LP user. - ''' - if not cls._me: - cls._me = PersonTeam(Launchpad.me) - return cls._me - def isLpTeamMember(self, team): ''' Checks if the user is a member of a certain team on Launchpad. diff --git a/ubuntutools/requestsync/lp.py b/ubuntutools/requestsync/lp.py index 1a97dac..5256836 100644 --- a/ubuntutools/requestsync/lp.py +++ b/ubuntutools/requestsync/lp.py @@ -52,7 +52,7 @@ def needSponsorship(name, component, release): archive = Distribution('ubuntu').getArchive() distroseries = Distribution('ubuntu').getSeries(release) - need_sponsor = not PersonTeam.getMe().canUploadPackage(archive, distroseries, name, component) + need_sponsor = not PersonTeam.me.canUploadPackage(archive, distroseries, name, component) if need_sponsor: print '''You are not able to upload this package directly to Ubuntu. Your sync request shall require an approval by a member of the appropriate @@ -105,7 +105,7 @@ def postBug(srcpkg, subscribe, status, bugtitle, bugtext): # newly created bugreports have only one task task = bug.bug_tasks[0] # only members of ubuntu-bugcontrol can set importance - if PersonTeam.getMe().isLpTeamMember('ubuntu-bugcontrol'): + if PersonTeam.me.isLpTeamMember('ubuntu-bugcontrol'): task.importance = 'Wishlist' task.status = status task.lp_save()