pgtool
author Radek Brich <radek.brich@devl.cz>
Wed, 09 Jul 2014 18:04:11 +0200
changeset 105 10551741f61f
parent 104 d8ff52a0390f
permissions -rwxr-xr-x
Move batchquery, listtables tools under pgtool.

#!/usr/bin/env python3

"""
Wrapper script for pydbkit 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.

Configuration
-------------

Global pydbkit configuration:
    /etc/pydbkit.conf

Local configuration:
    ./pydbkit.conf

Additional config file can be loaded using -c parameter (after tool name).

Configuration from all present files is loaded in above order,
later value overrides previous value. Resulting config file priority is:

   1) parameter
   2) local
   3) global

"""

import pydbkit.tools
import sys
from importlib import import_module


def print_tool_with_short_desc(name):
    module = import_module('pydbkit.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 pydbkit.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 pydbkit.tools.__all__:
        print('Unknown tool "%s".\n\nCall "pgtool --list" to get list of all available tools.' % tool)
        sys.exit()

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

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