pgtool
author Radek Brich <brich.radek@ifortuna.cz>
Wed, 07 May 2014 18:33:50 +0200
changeset 102 fda45bdfd68d
parent 101 2a2d0d5df03b
child 104 d8ff52a0390f
permissions -rwxr-xr-x
Update ToolBase: Load cascade of config files, add -c parameter.
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
"""
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
     4
Wrapper script for pgtoolkit 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
     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
fda45bdfd68d Update ToolBase: Load cascade of config files, add -c parameter.
Radek Brich <brich.radek@ifortuna.cz>
parents: 101
diff changeset
    24
Global pgtoolkit configuration:
fda45bdfd68d Update ToolBase: Load cascade of config files, add -c parameter.
Radek Brich <brich.radek@ifortuna.cz>
parents: 101
diff changeset
    25
    /etc/pgtoolkit.conf
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:
fda45bdfd68d Update ToolBase: Load cascade of config files, add -c parameter.
Radek Brich <brich.radek@ifortuna.cz>
parents: 101
diff changeset
    28
    ./pgtoolkit.conf
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
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
    41
import pgtoolkit.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
    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):
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
    47
    module = import_module('pgtoolkit.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
    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':
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
    59
            for tool in pgtoolkit.tools.__all__:
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
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
    68
    if tool not in pgtoolkit.tools.__all__:
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
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
    72
    module = import_module('pgtoolkit.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
    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