Hi all,

I'm trying to run 'svn update' on a given directory as a subprocess in python. I need to redirect the output of this process (svn update) to a file called updste.txt. And if 'svn update' fails, I'm trying to send out a mail to myself with the file content as message body. Please let me know how can i do that.

Following is the method which i call from my main() as
svn_update('<some absolute path>')

def svn_update(path):
    os.chdir(path)

    # execute the 'svn update' and pipe the result to a string
    test = "svn update --username mallik --password mallik"
    process = subprocess.Popen(test, shell=True, stdout=subprocess.PIPE)

    # give it time to respond
    process.wait()

    # check (0 --> success)
    if process.returncode == 0:
        print "successfully updated avm folder : ", path
    else:
        print "svn update failed for folder : ", path
        msg = open("update.txt")
        server.sendmail(FROM, TO, msg.read())
        server.quit()
        sys.exit("Exiting...")

Please reply me soon...

Regards
Mallikarjun

Recommended Answers

All 3 Replies

If you use popen2 you can get the subprocess output and inspect it yourself, then write it to a file.

Hi BearofNH,

To use popen(), i went to this site... http://docs.python.org/lib/module-popen2.html

But it says...

This module allows you to spawn processes and connect to their input/output/error pipes and obtain their return codes under Unix and Windows.

The subprocess module provides more powerful facilities for spawning new processes and retrieving their results. Using the subprocess module is preferable to using the popen2 module.


Hence i thought not to quit using the subprocess.Popen(...)

Please let me know if u know how to do it using subprocess module.
Thanks for your time.. :-)

Regards
Mallik

Doug Hellman's module of week shows how to read output, and use stdin, stdout, stderr. Don't know if it will solve your problem or not, but if you can read it you can write to a file, send e-mail, etc. Some systems buffer the output, so you may have to turn buffering off for the thread in order to read it as it is generated. There is pyprocessing also which has shared objects. I have not used it though.
http://blog.doughellmann.com/2007/07/pymotw-subprocess.html
http://pyprocessing.berlios.de/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.