~launchpad-pqm/launchpad/devel

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
#!/usr/bin/python -S
#
# Copyright 2009 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

# pylint: disable-msg=W0403
import _pythonpath

import sys
import logging
import optparse

from canonical.lp import initZopeless
from canonical.launchpad.scripts import (
    execute_zcml_for_scripts, logger_options, logger as logger_from_options)
from lp.registry.scripts.keyringtrustanalyser import mergeClusters


def readClusters(fp):
    """Read clusters of email addresses from the file (separated by blank
    lines), and yield them as sets."""
    cluster = set()
    for line in fp:
        line = line.strip()
        if line:
            cluster.add(line)
        elif cluster:
            yield cluster
            cluster = set()
    if cluster:
        yield cluster


def main(argv):
    parser = optparse.OptionParser(
        description="This script reads a list of email address clusters. "
        "and updates the Launchpad database to match by adding email "
        "addresses to existing accounts, merging accounts and "
        "creating new accounts")
    parser.add_option('-i', '--input', metavar='FILE', action='store',
                      help='Read clusters from the given file',
                      type='string', dest='input', default=None)

    logger_options(parser, logging.WARNING)

    options, args = parser.parse_args(argv[1:])

    # get logger
    logger = logger_from_options(options)

    if options.input is not None:
        logger.debug('openning %s', options.input)
        fp = open(options.input, 'r')
    else:
        fp = sys.stdin

    logger.info('Setting up utilities')
    execute_zcml_for_scripts()

    logger.info('Connecting to database')
    ztm = initZopeless()

    mergeClusters(readClusters(fp), ztm, logger)

    logger.info('Done')

    return 0


if __name__ == '__main__':
    sys.exit(main(sys.argv))