Saving time with launchpadlib
One of the many tasks that the Ubuntu IRC Council is responsible for is granting Ubuntu member hostname cloaks to people who have gained Ubuntu membership. We currently use a Launchpad team to keep track of the people that have been cloaked. When someone requests a cloak in #ubuntu-irc, there are a number of steps that we need to go:
- Get the user’s Launchpad page
- Check if they’re indeed an Ubuntu member
- Go to the cloaked Ubuntu members team
- Remember the user’s Launchpad id and add them to the team
- Ask freenode staff to apply the cloak
Now this doesn’t sound that difficult, but we’re all busy and we know that Launchpad isn’t exactly the fastest site around. So, in order to make this easier for myself, I put together a small python script using launchpadlib to do all the dirty work for me. It also gave me an excuse to play with optparse, which I have been looking to have a reason to use.
The script gives me the ability to check a member’s status and add them to the cloak team in one step. I can also choose to use the launchpad staging server to do a dry-run if I’m just testing.
This was my first time using launchpadlib to update information on Launchpad itself. I have to say that it was a lot easier than I had originally thought it would be.
As of revision 9, the code is as follows:
#!/usr/bin/env python cachedir = '~/.launchpadlib/cache/' import sys from optparse import OptionParser def main(): usage = 'usage: %prog [options] username' parser = OptionParser(usage) parser.add_option('-f','--force',action='store_true',dest='force', help='add user regardless of membership in ubuntumembers') parser.add_option('-s','--staging',action='store_true',dest='staging', help='preform events against the launchpad staging server') options,args = parser.parse_args() if len(args) != 1: print 'Error: incorrect number of arguments' parser.print_help() sys.exit(2) from launchpadlib.launchpad import Launchpad,STAGING_SERVICE_ROOT,EDGE_SERVICE_ROOT from launchpadlib.errors import HTTPError if options.staging: print 'Warning: using staging.launchpad.net' service = STAGING_SERVICE_ROOT else: service = EDGE_SERVICE_ROOT launchpad = Launchpad.login_with('irc_lpteam',service,cachedir) membername = args[0] teamname='ubuntu-irc-cloaks' team = launchpad.people[teamname] member = launchpad.people[membername] if 'ubuntumembers' in [e.name for e in member.super_teams]: print "'%s' (%s) is an Ubuntu Member" % (member.display_name,membername) print "Attempting to add '%s' (%s) to '%s' (%s)..." % (member.display_name,membername,team.display_name,teamname) try: status = team.addMember(person=member,status="Approved") except HTTPError as error: print "Error: %s has occurred" % error sys.exit(1) else: print "'%s' (%s) does not appear to be an Ubuntu Member." % (member.display_name,membername) if options.force: print 'Warning: force enabled, adding to team anyway' try: status = team.addMember(person=member,status="Approved") except HTTPError as error: print "Error: %s has occurred" % error sys.exit(1) print 'Success!' if __name__ == "__main__": main()
The latest code is available here for anyone who wants to play around with it themselves.
Note: I have filed a bug against launchpad that could make it easier to add people to teams.
Nice! Feel free to add this to https://help.launchpad.net/API/Examples or https://help.launchpad.net/API/Uses.
Also, if you print out `error.content`, rather than just `error`, I think you’ll get more useful information.
Comment by jml — December 9, 2009 @ 2:12 am