pgtool
author Radek Brich <brich.radek@ifortuna.cz>
Tue, 06 May 2014 18:37:43 +0200
changeset 101 2a2d0d5df03b
parent 100 d6088dba8fea
child 102 fda45bdfd68d
permissions -rwxr-xr-x
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.

#!/usr/bin/env python3

"""
Wrapper script for pgtoolkit tools.

Usage:

    pgtool --list
        List all available tools.

    pgtool <tool_name> <arg_1> <arg_2> ...
        Run tool. All args except first one are forwarded to the tool.

    pgtool schemadiff db1 db2
        E.g. run schemadiff between db1 and db2.

    pgtool schemadiff --help
        Get help for tool parameters.

"""

import pgtoolkit.tools
import sys
from importlib import import_module


def print_tool_with_short_desc(name):
    module = import_module('pgtoolkit.tools.' + tool)
    short_desc = module.cls.__doc__.lstrip().splitlines()[0]
    print(name.ljust(15), '-', short_desc)


if __name__ == '__main__':
    if len(sys.argv) < 2:
        print(__doc__, end='')
        sys.exit()

    if sys.argv[1].startswith('--'):
        if sys.argv[1] == '--list':
            for tool in pgtoolkit.tools.__all__:
                print_tool_with_short_desc(tool)
        else:
            print(__doc__, end='')
        sys.exit()

    tool = sys.argv[1]
    tool_args = sys.argv[2:]

    if tool not in pgtoolkit.tools.__all__:
        print('Unknown tool "%s".\n\nCall "pgtool --list" to get list of all available tools.' % tool)
        sys.exit()

    module = import_module('pgtoolkit.tools.' + tool)

    tool = module.cls()
    tool.setup(tool_args)
    tool.main()