~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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/python2.4
#
# 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 sys
import logging
import optparse

import _pythonpath

import gpgme

from canonical.launchpad.scripts import (
    execute_zcml_for_scripts, logger_options, logger as logger_from_options)
from lp.registry.scripts.keyringtrustanalyser import (
    addOtherKeyring, addTrustedKeyring, findEmailClusters)


validity_map = {
    'UNDEFINED': gpgme.VALIDITY_UNDEFINED,
    'NEVER':     gpgme.VALIDITY_NEVER,
    'MARGINAL':  gpgme.VALIDITY_MARGINAL,
    'FULL':      gpgme.VALIDITY_FULL,
    'ULTIMATE':  gpgme.VALIDITY_ULTIMATE,
    }


def main(argv):
    parser = optparse.OptionParser(
        usage="usage: %prog [options] keyrings ...",
        description="This script inferrs clusters of "
        "email addresses belonging to a single user "
        "from the user IDs attached to PGP keys.")
    parser.add_option('-o', '--output', metavar='FILE', action='store',
                      help='Output clusters to given file',
                      type='string', dest='output', default=None)
    parser.add_option('--trust', metavar='KEYRING', action='append',
                      help='Trust the owners of keys on this keyring',
                      type='string', dest='trust', default=[])
    parser.add_option('--owner-trust', metavar='TRUST', action='store',
                      help='What level of trust to assign to trusted keys',
                      type='string', dest='owner_trust', default='ULTIMATE')
    parser.add_option('--min-valid', metavar='TRUST', action='store',
                      help='Minimum trust necessary for a user ID to '
                      'be considered valid',
                      type='string', dest='minvalid', default='MARGINAL')

    logger_options(parser, logging.WARNING)

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

    # map validity options
    if options.owner_trust.upper() not in validity_map:
        sys.stderr.write('%s: unknown owner trust value %s'
                         % (argv[0], options.owner_trust))
        return 1
    options.owner_trust = validity_map[options.owner_trust.upper()]

    if options.minvalid.upper() not in validity_map:
        sys.stderr.write('%s: unknown min valid value %s'
                         % (argv[0], options.minvalid))
        return 1
    options.minvalid = validity_map[options.minvalid.upper()]

    # get logger
    logger = logger_from_options(options)

    if options.output is not None:
        logger.debug('openning %s', options.output)
        fp = open(options.output, 'w')
    else:
        fp = sys.stdout

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

    logger.info('Loading trusted keyrings')
    for keyring in options.trust:
        logger.info('Loading %s', keyring)
        addTrustedKeyring(keyring, options.owner_trust)

    logger.info('Loading other keyrings')
    for keyring in args:
        logger.info('Loading %s', keyring)
        addOtherKeyring(keyring)

    logger.info('Computing address clusters')
    for cluster in findEmailClusters(options.minvalid):
        for email in cluster:
            fp.write('%s\n' % email)
        fp.write('\n')

    logger.info('Done')

    return 0


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