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.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
100
d6088dba8fea Add pgtool wrapper for all tools. Only this script will be installed into system bin.
Radek Brich <brich.radek@ifortuna.cz>
parents:
diff changeset
     1
#!/usr/bin/env python3
d6088dba8fea Add pgtool wrapper for all tools. Only this script will be installed into system bin.
Radek Brich <brich.radek@ifortuna.cz>
parents:
diff changeset
     2
d6088dba8fea Add pgtool wrapper for all tools. Only this script will be installed into system bin.
Radek Brich <brich.radek@ifortuna.cz>
parents:
diff changeset
     3
"""
104
d8ff52a0390f Rename to pydbkit.
Radek Brich <radek.brich@devl.cz>
parents: 102
diff changeset
     4
Wrapper script for pydbkit tools.
100
d6088dba8fea Add pgtool wrapper for all tools. Only this script will be installed into system bin.
Radek Brich <brich.radek@ifortuna.cz>
parents:
diff changeset
     5
102
fda45bdfd68d Update ToolBase: Load cascade of config files, add -c parameter.
Radek Brich <brich.radek@ifortuna.cz>
parents: 101
diff changeset
     6
Usage
fda45bdfd68d Update ToolBase: Load cascade of config files, add -c parameter.
Radek Brich <brich.radek@ifortuna.cz>
parents: 101
diff changeset
     7
-----
100
d6088dba8fea Add pgtool wrapper for all tools. Only this script will be installed into system bin.
Radek Brich <brich.radek@ifortuna.cz>
parents:
diff changeset
     8
d6088dba8fea Add pgtool wrapper for all tools. Only this script will be installed into system bin.
Radek Brich <brich.radek@ifortuna.cz>
parents:
diff changeset
     9
    pgtool --list
d6088dba8fea Add pgtool wrapper for all tools. Only this script will be installed into system bin.
Radek Brich <brich.radek@ifortuna.cz>
parents:
diff changeset
    10
        List all available tools.
d6088dba8fea Add pgtool wrapper for all tools. Only this script will be installed into system bin.
Radek Brich <brich.radek@ifortuna.cz>
parents:
diff changeset
    11
d6088dba8fea Add pgtool wrapper for all tools. Only this script will be installed into system bin.
Radek Brich <brich.radek@ifortuna.cz>
parents:
diff changeset
    12
    pgtool <tool_name> <arg_1> <arg_2> ...
d6088dba8fea Add pgtool wrapper for all tools. Only this script will be installed into system bin.
Radek Brich <brich.radek@ifortuna.cz>
parents:
diff changeset
    13
        Run tool. All args except first one are forwarded to the tool.
d6088dba8fea Add pgtool wrapper for all tools. Only this script will be installed into system bin.
Radek Brich <brich.radek@ifortuna.cz>
parents:
diff changeset
    14
d6088dba8fea Add pgtool wrapper for all tools. Only this script will be installed into system bin.
Radek Brich <brich.radek@ifortuna.cz>
parents:
diff changeset
    15
    pgtool schemadiff db1 db2
d6088dba8fea Add pgtool wrapper for all tools. Only this script will be installed into system bin.
Radek Brich <brich.radek@ifortuna.cz>
parents:
diff changeset
    16
        E.g. run schemadiff between db1 and db2.
d6088dba8fea Add pgtool wrapper for all tools. Only this script will be installed into system bin.
Radek Brich <brich.radek@ifortuna.cz>
parents:
diff changeset
    17
d6088dba8fea Add pgtool wrapper for all tools. Only this script will be installed into system bin.
Radek Brich <brich.radek@ifortuna.cz>
parents:
diff changeset
    18
    pgtool schemadiff --help
d6088dba8fea Add pgtool wrapper for all tools. Only this script will be installed into system bin.
Radek Brich <brich.radek@ifortuna.cz>
parents:
diff changeset
    19
        Get help for tool parameters.
d6088dba8fea Add pgtool wrapper for all tools. Only this script will be installed into system bin.
Radek Brich <brich.radek@ifortuna.cz>
parents:
diff changeset
    20
102
fda45bdfd68d Update ToolBase: Load cascade of config files, add -c parameter.
Radek Brich <brich.radek@ifortuna.cz>
parents: 101
diff changeset
    21
Configuration
fda45bdfd68d Update ToolBase: Load cascade of config files, add -c parameter.
Radek Brich <brich.radek@ifortuna.cz>
parents: 101
diff changeset
    22
-------------
fda45bdfd68d Update ToolBase: Load cascade of config files, add -c parameter.
Radek Brich <brich.radek@ifortuna.cz>
parents: 101
diff changeset
    23
104
d8ff52a0390f Rename to pydbkit.
Radek Brich <radek.brich@devl.cz>
parents: 102
diff changeset
    24
Global pydbkit configuration:
d8ff52a0390f Rename to pydbkit.
Radek Brich <radek.brich@devl.cz>
parents: 102
diff changeset
    25
    /etc/pydbkit.conf
102
fda45bdfd68d Update ToolBase: Load cascade of config files, add -c parameter.
Radek Brich <brich.radek@ifortuna.cz>
parents: 101
diff changeset
    26
fda45bdfd68d Update ToolBase: Load cascade of config files, add -c parameter.
Radek Brich <brich.radek@ifortuna.cz>
parents: 101
diff changeset
    27
Local configuration:
104
d8ff52a0390f Rename to pydbkit.
Radek Brich <radek.brich@devl.cz>
parents: 102
diff changeset
    28
    ./pydbkit.conf
102
fda45bdfd68d Update ToolBase: Load cascade of config files, add -c parameter.
Radek Brich <brich.radek@ifortuna.cz>
parents: 101
diff changeset
    29
fda45bdfd68d Update ToolBase: Load cascade of config files, add -c parameter.
Radek Brich <brich.radek@ifortuna.cz>
parents: 101
diff changeset
    30
Additional config file can be loaded using -c parameter (after tool name).
fda45bdfd68d Update ToolBase: Load cascade of config files, add -c parameter.
Radek Brich <brich.radek@ifortuna.cz>
parents: 101
diff changeset
    31
fda45bdfd68d Update ToolBase: Load cascade of config files, add -c parameter.
Radek Brich <brich.radek@ifortuna.cz>
parents: 101
diff changeset
    32
Configuration from all present files is loaded in above order,
fda45bdfd68d Update ToolBase: Load cascade of config files, add -c parameter.
Radek Brich <brich.radek@ifortuna.cz>
parents: 101
diff changeset
    33
later value overrides previous value. Resulting config file priority is:
fda45bdfd68d Update ToolBase: Load cascade of config files, add -c parameter.
Radek Brich <brich.radek@ifortuna.cz>
parents: 101
diff changeset
    34
fda45bdfd68d Update ToolBase: Load cascade of config files, add -c parameter.
Radek Brich <brich.radek@ifortuna.cz>
parents: 101
diff changeset
    35
   1) parameter
fda45bdfd68d Update ToolBase: Load cascade of config files, add -c parameter.
Radek Brich <brich.radek@ifortuna.cz>
parents: 101
diff changeset
    36
   2) local
fda45bdfd68d Update ToolBase: Load cascade of config files, add -c parameter.
Radek Brich <brich.radek@ifortuna.cz>
parents: 101
diff changeset
    37
   3) global
fda45bdfd68d Update ToolBase: Load cascade of config files, add -c parameter.
Radek Brich <brich.radek@ifortuna.cz>
parents: 101
diff changeset
    38
100
d6088dba8fea Add pgtool wrapper for all tools. Only this script will be installed into system bin.
Radek Brich <brich.radek@ifortuna.cz>
parents:
diff changeset
    39
"""
d6088dba8fea Add pgtool wrapper for all tools. Only this script will be installed into system bin.
Radek Brich <brich.radek@ifortuna.cz>
parents:
diff changeset
    40
104
d8ff52a0390f Rename to pydbkit.
Radek Brich <radek.brich@devl.cz>
parents: 102
diff changeset
    41
import pydbkit.tools
100
d6088dba8fea Add pgtool wrapper for all tools. Only this script will be installed into system bin.
Radek Brich <brich.radek@ifortuna.cz>
parents:
diff changeset
    42
import sys
d6088dba8fea Add pgtool wrapper for all tools. Only this script will be installed into system bin.
Radek Brich <brich.radek@ifortuna.cz>
parents:
diff changeset
    43
from importlib import import_module
d6088dba8fea Add pgtool wrapper for all tools. Only this script will be installed into system bin.
Radek Brich <brich.radek@ifortuna.cz>
parents:
diff changeset
    44
d6088dba8fea Add pgtool wrapper for all tools. Only this script will be installed into system bin.
Radek Brich <brich.radek@ifortuna.cz>
parents:
diff changeset
    45
101
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 100
diff changeset
    46
def print_tool_with_short_desc(name):
104
d8ff52a0390f Rename to pydbkit.
Radek Brich <radek.brich@devl.cz>
parents: 102
diff changeset
    47
    module = import_module('pydbkit.tools.' + tool)
101
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 100
diff changeset
    48
    short_desc = module.cls.__doc__.lstrip().splitlines()[0]
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 100
diff changeset
    49
    print(name.ljust(15), '-', short_desc)
100
d6088dba8fea Add pgtool wrapper for all tools. Only this script will be installed into system bin.
Radek Brich <brich.radek@ifortuna.cz>
parents:
diff changeset
    50
101
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 100
diff changeset
    51
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 100
diff changeset
    52
if __name__ == '__main__':
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 100
diff changeset
    53
    if len(sys.argv) < 2:
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 100
diff changeset
    54
        print(__doc__, end='')
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 100
diff changeset
    55
        sys.exit()
100
d6088dba8fea Add pgtool wrapper for all tools. Only this script will be installed into system bin.
Radek Brich <brich.radek@ifortuna.cz>
parents:
diff changeset
    56
101
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 100
diff changeset
    57
    if sys.argv[1].startswith('--'):
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 100
diff changeset
    58
        if sys.argv[1] == '--list':
104
d8ff52a0390f Rename to pydbkit.
Radek Brich <radek.brich@devl.cz>
parents: 102
diff changeset
    59
            for tool in pydbkit.tools.__all__:
101
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 100
diff changeset
    60
                print_tool_with_short_desc(tool)
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 100
diff changeset
    61
        else:
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 100
diff changeset
    62
            print(__doc__, end='')
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 100
diff changeset
    63
        sys.exit()
100
d6088dba8fea Add pgtool wrapper for all tools. Only this script will be installed into system bin.
Radek Brich <brich.radek@ifortuna.cz>
parents:
diff changeset
    64
101
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 100
diff changeset
    65
    tool = sys.argv[1]
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 100
diff changeset
    66
    tool_args = sys.argv[2:]
100
d6088dba8fea Add pgtool wrapper for all tools. Only this script will be installed into system bin.
Radek Brich <brich.radek@ifortuna.cz>
parents:
diff changeset
    67
104
d8ff52a0390f Rename to pydbkit.
Radek Brich <radek.brich@devl.cz>
parents: 102
diff changeset
    68
    if tool not in pydbkit.tools.__all__:
101
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 100
diff changeset
    69
        print('Unknown tool "%s".\n\nCall "pgtool --list" to get list of all available tools.' % tool)
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 100
diff changeset
    70
        sys.exit()
100
d6088dba8fea Add pgtool wrapper for all tools. Only this script will be installed into system bin.
Radek Brich <brich.radek@ifortuna.cz>
parents:
diff changeset
    71
104
d8ff52a0390f Rename to pydbkit.
Radek Brich <radek.brich@devl.cz>
parents: 102
diff changeset
    72
    module = import_module('pydbkit.tools.' + tool)
101
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 100
diff changeset
    73
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 100
diff changeset
    74
    tool = module.cls()
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 100
diff changeset
    75
    tool.setup(tool_args)
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 100
diff changeset
    76
    tool.main()
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 100
diff changeset
    77