~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
4536.1.3 by Brad Crittenden
re-adding files that somehow disappeared.
8
9
import logging
10
import sys
11
14612.2.7 by William Grant
scripts
12
from lp.registry.scripts.entitlement import (
13
    EntitlementExchange,
14
    EntitlementImporter,
15
    )
8356.1.9 by Leonard Richardson
Renamed the base script module in scripts/, which module_rename.py didn't touch because it wasn't under lib/.
16
from lp.services.scripts.base import LaunchpadScript
4536.1.3 by Brad Crittenden
re-adding files that somehow disappeared.
17
18
19
class ImportEntitlementsScript(LaunchpadScript):
20
    """Script for to import entitlement data into Launchpad."""
21
22
    description = "Create or update entitlements."
23
    usage = ("usage: %s [-c|--create | -u|--update] file_name" %
24
             sys.argv[0])
25
26
    loglevel = logging.INFO
27
28
    def add_my_options(self):
29
        """See `LaunchpadScript`."""
30
        self.parser.add_option(
31
            '-c', '--create', action='store_const', const='create',
32
            help='Create new entitlements', dest='action')
33
        self.parser.add_option(
34
            '-u', '--update', action='store_const', const='update',
35
            help='Update existing entitlements', dest='action')
36
        self.parser.add_option(
37
            '-f', '--infile', action='store', default='-',
38
            help='Input file name ("-" for stdin)', dest='in_file_name')
39
        self.parser.add_option(
40
            '-o', '--outfile', action='store', default='-',
41
            help='Output file name ("-" for stdout)', dest='out_file_name')
42
43
    def main(self):
44
        """See `LaunchpadScript`."""
45
46
        action = self.options.action
47
48
        if self.options.in_file_name == '-':
49
            in_file = sys.stdin
50
        else:
51
            in_file = open(self.options.in_file_name, "rb")
52
53
        if self.options.out_file_name == '-':
54
            out_file = sys.stdout
55
        else:
56
            out_file = open(self.options.out_file_name, "wb")
57
58
        # get a reader and writer
59
        reader = EntitlementExchange.readerFactory(in_file)
60
        entitlement_writer = EntitlementImporter(self.logger)
61
        importer = EntitlementImporter(self.logger)
62
        if action == 'create':
63
            out_data = importer.createEntitlements(reader)
64
        elif action == 'update':
65
            out_data = importer.updateEntitlements(reader)
66
        elif action is None:
67
            self.logger.error("No action specified.  Use either -c or -u.")
68
            return 1
69
        else:
70
            self.logger.error("Invalid action: %s\n" % action)
71
            return 1
72
73
        self.txn.commit()
74
75
        if out_data:
76
            writer = EntitlementExchange.writerFactory(out_file)
77
            writer.writerows(out_data)
78
        return 0
79
80
if __name__ == '__main__':
8486.12.1 by Celso Providelo
Fixing the 'fixable' scripts and adding a checker with the really-broken ones blacklisted.
81
    script = ImportEntitlementsScript(
14565.2.15 by Curtis Hovey
Moved canonical.launchpad.scripts __init__ to lp.services.scripts.
82
        'lp.services.scripts.entitlements')
4536.1.3 by Brad Crittenden
re-adding files that somehow disappeared.
83
    script.run()