~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to scripts/entitlements-to-lp.py

  • Committer: Curtis Hovey
  • Date: 2007-07-14 15:58:21 UTC
  • mfrom: (4557 launchpad)
  • mto: This revision was merged to the branch mainline in revision 4603.
  • Revision ID: curtis.hovey@canonical.com-20070714155821-t7fb1onic9tfjs8n
Merge from RF. Resolved conflicts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python2.4
 
2
# Copyright 2007 Canonical Ltd.  All rights reserved.
 
3
 
 
4
__metaclass__ = type
 
5
 
 
6
import logging
 
7
import sys
 
8
 
 
9
import _pythonpath
 
10
 
 
11
from canonical.launchpad.scripts.base import LaunchpadScript
 
12
from canonical.launchpad.scripts.entitlement import (
 
13
    EntitlementExchange,
 
14
    EntitlementImporter,
 
15
    )
 
16
 
 
17
 
 
18
class ImportEntitlementsScript(LaunchpadScript):
 
19
    """Script for to import entitlement data into Launchpad."""
 
20
 
 
21
    description = "Create or update entitlements."
 
22
    usage = ("usage: %s [-c|--create | -u|--update] file_name" %
 
23
             sys.argv[0])
 
24
 
 
25
    loglevel = logging.INFO
 
26
 
 
27
    def add_my_options(self):
 
28
        """See `LaunchpadScript`."""
 
29
        self.parser.add_option(
 
30
            '-c', '--create', action='store_const', const='create',
 
31
            help='Create new entitlements', dest='action')
 
32
        self.parser.add_option(
 
33
            '-u', '--update', action='store_const', const='update',
 
34
            help='Update existing entitlements', dest='action')
 
35
        self.parser.add_option(
 
36
            '-f', '--infile', action='store', default='-',
 
37
            help='Input file name ("-" for stdin)', dest='in_file_name')
 
38
        self.parser.add_option(
 
39
            '-o', '--outfile', action='store', default='-',
 
40
            help='Output file name ("-" for stdout)', dest='out_file_name')
 
41
 
 
42
    def main(self):
 
43
        """See `LaunchpadScript`."""
 
44
 
 
45
        action = self.options.action
 
46
 
 
47
        if self.options.in_file_name == '-':
 
48
            in_file = sys.stdin
 
49
        else:
 
50
            in_file = open(self.options.in_file_name, "rb")
 
51
 
 
52
        if self.options.out_file_name == '-':
 
53
            out_file = sys.stdout
 
54
        else:
 
55
            out_file = open(self.options.out_file_name, "wb")
 
56
 
 
57
        # get a reader and writer
 
58
        reader = EntitlementExchange.readerFactory(in_file)
 
59
        entitlement_writer = EntitlementImporter(self.logger)
 
60
        importer = EntitlementImporter(self.logger)
 
61
        if action == 'create':
 
62
            out_data = importer.createEntitlements(reader)
 
63
        elif action == 'update':
 
64
            out_data = importer.updateEntitlements(reader)
 
65
        elif action is None:
 
66
            self.logger.error("No action specified.  Use either -c or -u.")
 
67
            return 1
 
68
        else:
 
69
            self.logger.error("Invalid action: %s\n" % action)
 
70
            return 1
 
71
 
 
72
        self.txn.commit()
 
73
 
 
74
        if out_data:
 
75
            writer = EntitlementExchange.writerFactory(out_file)
 
76
            writer.writerows(out_data)
 
77
        return 0
 
78
 
 
79
if __name__ == '__main__':
 
80
    script = ImportEntitlementsScript('canonical.launchpad.scripts.entitlements')
 
81
    script.run()