#!/usr/bin/env python

import sys
import subprocess

from . import mpsconfigquery

def export(dbfile, path, verbose):
    """Write dump file to path"""

    if verbose:
        print "Exporting dump file to", path
    
    try:
        outfile = open(path, 'w')
    except IOError, e:
        sys.stderr.write('Could not open "%s": %s\n' % (path, e.strerror))
        return 1

    p = subprocess.Popen(('sqlite3', dbfile), stdin=subprocess.PIPE, stdout=outfile)
    p.stdin.write('.dump')
    p.stdin.close()
    p.wait()
    outfile.close()
