2
# Copyright 2007 Canonical Ltd. All rights reserved.
11
from canonical.launchpad.scripts.base import LaunchpadScript
12
from canonical.launchpad.scripts.entitlement import (
18
class ImportEntitlementsScript(LaunchpadScript):
19
"""Script for to import entitlement data into Launchpad."""
21
description = "Create or update entitlements."
22
usage = ("usage: %s [-c|--create | -u|--update] file_name" %
25
loglevel = logging.INFO
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')
43
"""See `LaunchpadScript`."""
45
action = self.options.action
47
if self.options.in_file_name == '-':
50
in_file = open(self.options.in_file_name, "rb")
52
if self.options.out_file_name == '-':
55
out_file = open(self.options.out_file_name, "wb")
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)
66
self.logger.error("No action specified. Use either -c or -u.")
69
self.logger.error("Invalid action: %s\n" % action)
75
writer = EntitlementExchange.writerFactory(out_file)
76
writer.writerows(out_data)
79
if __name__ == '__main__':
80
script = ImportEntitlementsScript('canonical.launchpad.scripts.entitlements')