~abentley/lp-dev-utils/testr-remote

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
#! /usr/bin/env python

"""Find the email addresses for people names."""

import csv
import sys
import traceback

from launchpadlib import errors
from launchpadlib.launchpad import Launchpad

## import httplib2
## httplib2.debuglevel = 1

app = Launchpad.login_with(
    'findaddrs', service_root='https://api.launchpad.net', version='devel')


reader = csv.reader(open(sys.argv[1]))
# Skip the header row.
reader.next()

addresses = {}
missing = set()


def matcher(person, domain):
    matches = set()
    # Try to find the longest matching address the person has.
    all_addresses = set(email.email
                        for email in person.confirmed_email_addresses)
    if person.preferred_email_address is not None:
        all_addresses.add(person.preferred_email_address.email)
    for email in all_addresses:
        localpart, hostname = email.rsplit('@', 1)
        if hostname == domain:
            matches.add(email)
    if len(matches) > 0:
        return max(matches, key=len)
    return None


def find_email_for_person(person):
    match = matcher(person, 'canonical.com')
    if match is not None:
        return match
    return matcher(person, 'ubuntu.com')


def guess_person(fullname):
    print 'guessing', fullname
    try:
        people = app.people.findPerson(text=fullname)
    except errors.HTTPError:
        traceback.print_exc()
        print 'Bad person!', fullname
        return
    for person in people:
        if fullname in addresses:
            continue
        email = find_email_for_person(person)
        if email is None:
            missing.add(fullname)
        else:
            missing.discard(fullname)
            addresses[fullname] = email


for row in reader:
    fullname = '%s %s' % (row[1], row[0])
    guess_person(fullname)


print '%s found' % len(addresses)
found = open('found.txt', 'w')
for fullname, address in addresses.items():
    print >> found, address
print '%s missing:' % len(missing)
for fullname in missing:
    print '    ', fullname