~bnrubin/+junk/lpteam

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python
cachedir = '~/.launchpadlib/cache/'
import sys
from optparse import OptionParser

def yesno(prompt):
    while True:
        q = raw_input(prompt)
        if q.lower() == 'y' or q.lower() == 'yes' or len(q) == 0:
            return True
        elif q.lower() == 'n' or q.lower() == 'no':
            return False

def addmember(member,team):
    try:
        status = team.addMember(person=member,status="Approved")
        pass
    except HTTPError as error:
        print "Error: %s" % error.content
        sys.exit(1)
                                                                

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]
    
    print 'Looking up %s' % membername
    try:
        member = launchpad.people[membername]
        print 'Found %s (%s)' % (membername,member.display_name)
    except KeyError:
        print 'Error: Launchpad account not found.'
        sys.exit(1)

    if 'ubuntumembers' in  [e.name for e in member.super_teams]:
        print "%s is an Ubuntu Member" % (membername)
        print 'IRC nicks associated with this LP account:'
        if len(member.irc_nicknames) == 0:
            print '\tNo irc information found'
        else:
            for ircinfo in member.irc_nicknames:
                print '\t%s on %s' % (ircinfo.nickname,ircinfo.network)
        
        if not yesno('Does this information look correct? [Y/n]: '):
            print 'Aborted'
            sys.exit()

        print "Attempting to add %s to '%s' (%s)..." % (membername,team.display_name,teamname)
        
        addmember(member,team)
    
    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'
            addmember(member,team)

    print 'Success!'                                                            
if __name__ == "__main__":
    main()