~hopem/charms/precise/ci-configurator/1383923

« back to all changes in this revision

Viewing changes to scripts/query_lp_members.py

[hopem,r=wolsen]

Merge changes/fixes that were applied to the dtag ci-configurator but
not applied to trunk first.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
# Synchronize Gerrit users from Launchpad.
17
17
 
18
18
import os
 
19
import re
19
20
import sys
20
21
import yaml
21
22
import urllib2
34
35
GERRIT_CREDENTIALS = LAUNCHPAD_DIR+'/creds'
35
36
 
36
37
# check parameters from command line
37
 
if len(sys.argv)<3:
 
38
if len(sys.argv) < 3:
38
39
    print "ERROR: Please send user and private key in parameters."
39
40
    sys.exit(1)
40
41
 
59
60
                                 GERRIT_CACHE_DIR,
60
61
                                 credentials_file=GERRIT_CREDENTIALS)
61
62
 
 
63
 
62
64
def get_openid(lp_user):
63
65
    k = dict(id=randomString(16, '0123456789abcdef'))
64
66
    openid_consumer = consumer.Consumer(k, None)
80
82
NEED_FLUSH = False
81
83
SEEN_LOGINS = set()
82
84
 
 
85
 
 
86
def assert_is_valid_email(email):
 
87
    if not email or not re.search('.+?@.+?\..+?', email):
 
88
        msg = "invalid email address '%s'" % (email)
 
89
        raise Exception(msg)
 
90
 
 
91
 
83
92
# Recurse members_details to return a list of (final)users as a tuples:
84
93
# (login, full_name, email, ssh_keys, openid)
85
94
def get_all_users(members_details, team_name):
86
95
    users = []
87
96
    for detail in members_details:
88
 
        user = None
89
 
 
90
97
        # detail.self_link ==
91
98
        # 'https://api.launchpad.net/1.0/~team/+member/${username}'
92
99
        login = detail.self_link.split('/')[-1]
98
105
            continue
99
106
        # Avoid re-visiting SEEN_LOGINS
100
107
        if login in SEEN_LOGINS:
101
 
           continue
102
 
        SEEN_LOGINS.add(login)
 
108
            print ("'%s' details already identified - skipping alternate" %
 
109
                   (login))
 
110
            continue
 
111
 
103
112
        print '{}-entry: {}/{}'.format('T' if member.is_team else 'U', team_name, login)
104
113
 
105
114
        # If is_team recurse down(branch), else add this user details(leaf) to users
106
115
        if member.is_team:
107
116
            try:
108
117
                users.extend(get_all_users(member.members_details, "{}/{}".format(team_name, member.name)))
109
 
            except Unauthorized as e:
 
118
            except Unauthorized:
110
119
                print "WARN: skipping team={}/{} (Unauthorized)".format(team_name, member.name)
111
120
                pass
112
121
        else:
113
122
            openid = get_openid(login)
114
123
            full_name = member.display_name.encode('ascii', 'replace')
115
124
            email = ''
 
125
            errmsg = ("failed to get valid email address for '%s' (%s) - "
 
126
                      "skipping")
116
127
            try:
117
128
                email = member.preferred_email_address.email
 
129
                assert_is_valid_email(email)
 
130
            except Exception as exc:
 
131
                print (errmsg % (login, str(exc)))
 
132
                continue
118
133
            except:
119
 
                email = login
 
134
                # Do catchall just in case an exception is raised that does not
 
135
                # inherit Exception.
 
136
                print (errmsg % (login, 'no exception info available'))
 
137
                continue
120
138
 
121
139
            ssh_keys = tuple(
122
140
                "{} {} {}".format(get_type(key.keytype), key.keytext, key.comment).strip()
123
141
                for key in member.sshkeys
124
142
            )
125
143
            users.append((login, full_name, email, ssh_keys, openid),)
 
144
 
 
145
        # Only remember login if it was actually used.
 
146
        SEEN_LOGINS.add(login)
 
147
 
126
148
    # Return a list with user details tuple
127
149
    return users
128
150
 
145
167
        final_users.extend(get_all_users(team.members_details, team_todo))
146
168
 
147
169
    if final_users:
148
 
         NEED_FLUSH = True
 
170
        NEED_FLUSH = True
149
171
 
150
172
    # add all the users
151
173
    try: