2200.1.22
by James Henstridge
driver scripts for finding and merging email clusters |
1 |
import sys |
2 |
import logging |
|
3 |
import optparse |
|
4 |
||
5 |
import _pythonpath |
|
3127.1.3
by James Henstridge
Fix find-email-clusters.py to not use pyme constants. |
6 |
|
7 |
from canonical.lp import initZopeless |
|
2200.1.22
by James Henstridge
driver scripts for finding and merging email clusters |
8 |
from canonical.launchpad.scripts import ( |
9 |
execute_zcml_for_scripts, logger_options, logger as logger_from_options) |
|
10 |
from canonical.launchpad.scripts.keyringtrustanalyser import * |
|
11 |
||
12 |
def readClusters(fp): |
|
13 |
"""Read clusters of email addresses from the file (separated by blank
|
|
14 |
lines), and yield them as sets."""
|
|
15 |
cluster = set() |
|
16 |
for line in fp: |
|
17 |
line = line.strip() |
|
2200.1.23
by James Henstridge
fix some bugs in merge-email-clusters |
18 |
if line: |
2200.1.22
by James Henstridge
driver scripts for finding and merging email clusters |
19 |
cluster.add(line) |
20 |
elif cluster: |
|
2200.1.23
by James Henstridge
fix some bugs in merge-email-clusters |
21 |
yield cluster |
2200.1.22
by James Henstridge
driver scripts for finding and merging email clusters |
22 |
cluster = set() |
23 |
if cluster: |
|
24 |
yield cluster |
|
25 |
||
26 |
def main(argv): |
|
27 |
parser = optparse.OptionParser( |
|
28 |
description="This script reads a list of email address clusters. " |
|
29 |
"and updates the Launchpad database to match by adding email "
|
|
30 |
"addresses to existing accounts, merging accounts and "
|
|
31 |
"creating new accounts") |
|
32 |
parser.add_option('-i', '--input', metavar='FILE', action='store', |
|
33 |
help='Read clusters from the given file', |
|
34 |
type='string', dest='input', default=None) |
|
35 |
||
36 |
logger_options(parser, logging.WARNING) |
|
37 |
||
38 |
options, args = parser.parse_args(argv[1:]) |
|
39 |
||
40 |
# get logger
|
|
41 |
logger = logger_from_options(options) |
|
42 |
||
43 |
if options.input is not None: |
|
44 |
logger.debug('openning %s', options.input) |
|
45 |
fp = open(options.input, 'r') |
|
46 |
else: |
|
47 |
fp = sys.stdin |
|
48 |
||
49 |
logger.info('Connecting to database') |
|
50 |
ztm = initZopeless() |
|
51 |
||
52 |
logger.info('Setting up utilities') |
|
53 |
execute_zcml_for_scripts() |
|
54 |
||
55 |
mergeClusters(readClusters(fp), ztm, logger) |
|
56 |
||
57 |
logger.info('Done') |
|
58 |
||
59 |
return 0 |
|
60 |
||
61 |
if __name__ == '__main__': |
|
62 |
sys.exit(main(sys.argv)) |
|
63 |