~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to scripts/merge-email-clusters.py

  • Committer: Jelmer Vernooij
  • Date: 2011-08-22 21:04:00 UTC
  • mfrom: (13261.7.21 bzr-2.4b4)
  • mto: This revision was merged to the branch mainline in revision 13941.
  • Revision ID: jelmer@canonical.com-20110822210400-3hfq31q2wgrtauad
Merge bzr-2.4b4.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python -S
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
 
 
6
 
# pylint: disable-msg=W0403
7
 
import _pythonpath
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)
16
 
from lp.registry.scripts.keyringtrustanalyser import mergeClusters
17
 
 
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:
24
 
        line = line.strip()
25
 
        if line:
26
 
            cluster.add(line)
27
 
        elif cluster:
28
 
            yield cluster
29
 
            cluster = set()
30
 
    if cluster:
31
 
        yield cluster
32
 
 
33
 
 
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
 
 
57
 
    logger.info('Setting up utilities')
58
 
    execute_zcml_for_scripts()
59
 
 
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
 
 
69
 
 
70
 
if __name__ == '__main__':
71
 
    sys.exit(main(sys.argv))