2 # -*- coding: utf-8 -*-
4 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
5 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
10 from librarian import packagers
12 from collections import OrderedDict
15 from django.utils.datastructures import SortedDict
16 OrderedDict = SortedDict
21 if __name__ == '__main__':
22 # Parse commandline arguments
23 usage = """Usage: %prog [options] SOURCE [SOURCE...]
24 Prepare SOURCE files for a partner."""
26 parser = optparse.OptionParser(usage=usage)
28 parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False,
29 help='print status messages to stdout')
30 parser.add_option('-O', '--output-dir', dest='output_dir', metavar='DIR', default='',
31 help='specifies the directory for output')
32 parser.add_option('-f', '--force-overwrite',
36 help='rebuild the file even if it already exists')
37 parser.add_option('-p', '--packages',
39 metavar='package,...',
41 help='lists packages to build')
42 parser.add_option('-m', '--packages-modules',
43 dest='packages_modules', metavar='path.to.module,...',
44 default='librarian.partners',
45 help='modules with packages definitions to load')
46 parser.add_option('-l', '--list-packages',
50 help='lists available packages')
52 options, input_filenames = parser.parse_args()
53 packages = OrderedDict()
55 for module_name in options.packages_modules.split(','):
58 module = __import__(module_name, globals(), locals(), ['*'])
59 for package_name in dir(module):
60 package = getattr(module, package_name)
61 if inspect.isclass(package) and issubclass(package, packagers.Packager):
62 packages[package_name] = package
64 print 'No packages found!'
66 if options.list_packages:
67 print 'Available packages:'
68 for package_name, package in packages.items():
69 print ' ', package_name
72 if len(input_filenames) < 1 or not options.packages:
76 used_packages = [packages[p] for p in options.packages.split(',')]
77 for package in used_packages:
79 print 'Package:', package.__name__
80 package.prepare(input_filenames,
81 options.output_dir, options.verbose, options.overwrite)