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