~launchpad-pqm/launchpad/devel

10637.3.1 by Guilherme Salgado
Use the default python version instead of a hard-coded version
1
#!/usr/bin/python -S
8687.15.22 by Karl Fogel
Add the copyright header block to the remaining .py files.
2
#
3
# Copyright 2009 Canonical Ltd.  This software is licensed under the
4
# GNU Affero General Public License version 3 (see the file LICENSE).
5
8486.12.1 by Celso Providelo
Fixing the 'fixable' scripts and adding a checker with the really-broken ones blacklisted.
6
# pylint: disable-msg=W0403
9641.1.7 by Gary Poster
fix scripts to work
7
import _pythonpath
2200.1.22 by James Henstridge
driver scripts for finding and merging email clusters
8
9
import sys
10
import logging
11
import optparse
12
13
from canonical.lp import initZopeless
14
from canonical.launchpad.scripts import (
15
    execute_zcml_for_scripts, logger_options, logger as logger_from_options)
8486.12.1 by Celso Providelo
Fixing the 'fixable' scripts and adding a checker with the really-broken ones blacklisted.
16
from lp.registry.scripts.keyringtrustanalyser import mergeClusters
17
2200.1.22 by James Henstridge
driver scripts for finding and merging email clusters
18
19
def readClusters(fp):
20
    """Read clusters of email addresses from the file (separated by blank
21
    lines), and yield them as sets."""
22
    cluster = set()
23
    for line in fp:
2200.1.23 by James Henstridge
fix some bugs in merge-email-clusters
24
        line = line.strip()
2200.1.22 by James Henstridge
driver scripts for finding and merging email clusters
25
        if line:
26
            cluster.add(line)
2200.1.23 by James Henstridge
fix some bugs in merge-email-clusters
27
        elif cluster:
2200.1.22 by James Henstridge
driver scripts for finding and merging email clusters
28
            yield cluster
29
            cluster = set()
30
    if cluster:
31
        yield cluster
32
8486.12.1 by Celso Providelo
Fixing the 'fixable' scripts and adding a checker with the really-broken ones blacklisted.
33
2200.1.22 by James Henstridge
driver scripts for finding and merging email clusters
34
def main(argv):
35
    parser = optparse.OptionParser(
36
        description="This script reads a list of email address clusters. "
37
        "and updates the Launchpad database to match by adding email "
38
        "addresses to existing accounts, merging accounts and "
39
        "creating new accounts")
40
    parser.add_option('-i', '--input', metavar='FILE', action='store',
41
                      help='Read clusters from the given file',
42
                      type='string', dest='input', default=None)
43
44
    logger_options(parser, logging.WARNING)
45
46
    options, args = parser.parse_args(argv[1:])
47
48
    # get logger
49
    logger = logger_from_options(options)
50
51
    if options.input is not None:
52
        logger.debug('openning %s', options.input)
53
        fp = open(options.input, 'r')
54
    else:
55
        fp = sys.stdin
56
5821.5.14 by James Henstridge
Run execute_zcml_for_scripts() before initZopeless() in many tests.
57
    logger.info('Setting up utilities')
58
    execute_zcml_for_scripts()
59
2200.1.22 by James Henstridge
driver scripts for finding and merging email clusters
60
    logger.info('Connecting to database')
61
    ztm = initZopeless()
62
63
    mergeClusters(readClusters(fp), ztm, logger)
64
65
    logger.info('Done')
66
67
    return 0
68
8486.12.1 by Celso Providelo
Fixing the 'fixable' scripts and adding a checker with the really-broken ones blacklisted.
69
2200.1.22 by James Henstridge
driver scripts for finding and merging email clusters
70
if __name__ == '__main__':
71
    sys.exit(main(sys.argv))