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.
12 from librarian import packagers
14 from collections import OrderedDict
17 from django.utils.datastructures import SortedDict
18 OrderedDict = SortedDict
23 if __name__ == '__main__':
24 # Parse commandline arguments
25 usage = """Usage: %prog [options] SOURCE [SOURCE...]
26 Prepare SOURCE files for a partner."""
28 parser = optparse.OptionParser(usage=usage)
30 parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False,
31 help='print status messages to stdout')
32 parser.add_option('-O', '--output-dir', dest='output_dir', metavar='DIR', default='',
33 help='specifies the directory for output')
34 parser.add_option('-f', '--force-overwrite',
38 help='rebuild the file even if it already exists')
39 parser.add_option('-p', '--packages',
41 metavar='package,...',
43 help='lists packages to build')
44 parser.add_option('-m', '--packages-modules',
45 dest='packages_modules', metavar='path.to.module,...',
46 default='librarian.partners',
47 help='modules with packages definitions to load')
48 parser.add_option('-l', '--list-packages',
52 help='lists available packages')
54 options, input_filenames = parser.parse_args()
55 packages = OrderedDict()
57 sys.path.insert(0, os.getcwd())
58 for module_name in options.packages_modules.split(','):
61 module = __import__(module_name, globals(), locals(), ['*'])
62 for package_name in dir(module):
63 package = getattr(module, package_name)
64 if inspect.isclass(package) and issubclass(package, packagers.Packager):
65 packages[package_name] = package
67 print 'No packages found!'
69 if options.list_packages:
70 print 'Available packages:'
71 for package_name, package in packages.items():
72 print ' ', package_name
75 if len(input_filenames) < 1 or not options.packages:
79 used_packages = [packages[p] for p in options.packages.split(',')]
80 for package in used_packages:
82 print 'Package:', package.__name__
83 package.prepare(input_filenames,
84 options.output_dir, options.verbose, options.overwrite)