#!/usr/bin/env python
"""
Import a dump file from pg_dump into the database specified
in the <config file>.
"""

# boilerplate code. copy that
import os
import sys
sys.path.insert(0,  os.path.abspath(os.path.dirname(__file__)))
from common import create_parser, config_from_args, get_engine
# /end boilerplate code

import subprocess


def main():
    parser = create_parser(description=__doc__, use_instance=False)
    parser.add_argument('--script', default=None, required=True,
                        help="path to the input sql script")
    args = parser.parse_args()

    # check and cleanup script
    script_path = os.path.join(os.getcwd(), args.script)
    if not os.path.exists(script_path):
        parser.error('sql script "%s" does not exist.' % args.script)

    # get an engine to get the driver type and connection details.
    engine = get_engine(config_from_args(args))
    drivername = engine.url.drivername

    error = False
    if drivername == 'postgresql':
        # use the psql command line script for imports.
        # pg_dump by default emits COPY ... FROM STDIN statements
        # which the psycopg2 driver can't handle.
        # pg_dump can emit inserts (--inserts), but that's
        # dead slow to import.
        vars = engine.url.__dict__.copy()
        vars['script_path'] = script_path
        command = ('psql -U {username} -h {host} -p {port} -'
                   'd {database} -f {script_path}').format(**vars)
        print 'Executing command: %s' % command
        if engine.url.password is not None:
            print 'Prefixing it with PGPASSWORD="<password>"'
            command = 'PGPASSWORD="%s %s" ' % (engine.url.password, command)

        error = subprocess.call(command, shell=True)
    else:
        print ('Action for driver "%s" is not defined.\n'
               "Note: sqlite3 has a non-standard executescript() method.")
        exit(1)

    if error:
        print 'Process exited with Error: %s' % error
        exit(error)


if __name__ == '__main__':
    sys.exit(main())

@@ -436,22 +436,20 @@
 ment('--
-script
+dump
 ', defau
@@ -502,17 +502,17 @@
   help=%22
-p
+P
 ath to t
@@ -518,24 +518,22 @@
 the 
-input sql script
+SQL dump file.
 %22)%0A 
@@ -591,25 +591,26 @@
 nup 
-script%0A    script
+dump file%0A    dump
 _pat
@@ -644,22 +644,20 @@
 ), args.
-script
+dump
 )%0A    if
@@ -676,22 +676,20 @@
 .exists(
-script
+dump
 _path):%0A
@@ -714,18 +714,21 @@
 or('
-sql script
+SQL dump file
  %22%25s
@@ -753,22 +753,20 @@
  %25 args.
-script
+dump
 )%0A%0A    #
@@ -1286,22 +1286,20 @@
   vars%5B'
-script
+dump
 _path'%5D 
@@ -1300,22 +1300,20 @@
 ath'%5D = 
-script
+dump
 _path%0A  
@@ -1409,22 +1409,20 @@
 se%7D -f %7B
-script
+dump
 _path%7D')

rename --script to --dump