From: | Dimitri Fontaine <dfontaine(at)hi-media(dot)com> |
---|---|
To: | Erik Jones <ejones(at)engineyard(dot)com> |
Cc: | Jasen Betts <jasen(at)xnet(dot)co(dot)nz>, pgsql-general(at)postgresql(dot)org |
Subject: | Re: running pg_dump from python |
Date: | 2009-06-22 12:16:51 |
Message-ID: | 874ou8wjik.fsf@hi-media-techno.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
Hi,
Erik Jones <ejones(at)engineyard(dot)com> writes:
> On Jun 15, 2009, at 5:17 AM, Jasen Betts wrote:
>
>> On 2009-06-14, Garry Saddington <garry(at)schoolteachers(dot)co(dot)uk> wrote:
>>> def backup():
>>> import os
>>> os.popen("c:/scholarpack/postgres/bin/pg_dump scholarpack >
>>> c:/scholarpack/ancillary/scholarpack.sql")
>>
>> are you sure you're using os.popen correctly?
>> you don't appear to be waiting for the pg_dump process to finish.
>
> Right, the popen stuff should be something like:
>
> p = os.popen("c:/scholarpack/postgres/bin/pg_dump scholarpack > c:/
> scholarpack/ancillary/scholarpack.sql 2> c:/scholarpack/ancillary/
> dump.err")
> status = p.close()
>
> Then check status to see if the command was successful or not.
Well, use subprocess:
def run_command(command, expected_retcodes = 0, stdin = None):
"""run a command and raise an exception if retcode not in expected_retcode"""
# we want expected_retcode to be a tuple but will manage integers
if type(expected_retcodes) == type(0):
expected_retcodes = (expected_retcodes,)
# we want the command to be a list, but accomodate when given a string
cmd = command
if type(cmd) == type('string'):
cmd = shlex.split(command)
proc = subprocess.Popen(cmd,
stdin = stdin,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE)
out, err = proc.communicate()
if proc.returncode not in expected_retcodes:
# when nothing gets to stderr, add stdout to Detail
if err.strip() == '':
err = out
mesg = 'Error [%d]: %s' % (proc.returncode, command)
mesg += '\nDetail: %s' % err
raise Exception, mesg
return proc.returncode, out, err
Regards,
--
dim
From | Date | Subject | |
---|---|---|---|
Next Message | Dario Teixeira | 2009-06-22 12:26:45 | Information about columns |
Previous Message | Grzegorz Jaśkiewicz | 2009-06-22 12:04:45 | Re: Graphical representation of query plans |