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.
7 from __future__ import print_function, unicode_literals
9 from collections import OrderedDict
15 from librarian import packagers
18 if __name__ == '__main__':
19 # Parse commandline arguments
20 usage = """Usage: %prog [options] SOURCE [SOURCE...]
21 Prepare SOURCE files for a partner."""
23 parser = optparse.OptionParser(usage=usage)
25 parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False,
26 help='print status messages to stdout')
27 parser.add_option('-O', '--output-dir', dest='output_dir', metavar='DIR', default='',
28 help='specifies the directory for output')
29 parser.add_option('-f', '--force-overwrite',
33 help='rebuild the file even if it already exists')
34 parser.add_option('-p', '--packages',
36 metavar='package,...',
38 help='lists packages to build')
39 parser.add_option('-m', '--packages-modules',
40 dest='packages_modules', metavar='path.to.module,...',
41 default='librarian.partners',
42 help='modules with packages definitions to load')
43 parser.add_option('-l', '--list-packages',
47 help='lists available packages')
49 options, input_filenames = parser.parse_args()
50 packages = OrderedDict()
52 sys.path.insert(0, os.getcwd())
53 for module_name in options.packages_modules.split(','):
56 module = __import__(module_name, globals(), locals(), ['*'])
57 for package_name in dir(module):
58 package = getattr(module, package_name)
59 if inspect.isclass(package) and issubclass(package, packagers.Packager):
60 packages[package_name] = package
62 print('No packages found!')
64 if options.list_packages:
65 print('Available packages:')
66 for package_name, package in packages.items():
67 print(' ', package_name)
70 if len(input_filenames) < 1 or not options.packages:
74 used_packages = [packages[p] for p in options.packages.split(',')]
75 for package in used_packages:
77 print('Package:', package.__name__)
78 package.prepare(input_filenames,
79 options.output_dir, options.verbose, options.overwrite)