简体   繁体   中英

how to find if svn command executed successfully or not in python

I am executing svn commands (assume I do not want to use pysvn) from my python script in the following manner (for example):

cmd = 'svn update http://myserver/myrepo'
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    output, stderr = p.communicate()      
    status = p.returncode
    if status != 0: 
        logging.error('A fatal has error occurred while executing command: ' + cmd)
        exit(-1)

Let's say myrepo does NOT exists on the server. It this case SVN will silently produces output like:

Skipped http://myserver/myrepo

and the status variable has the value '0'. Is there a way I can detect via the return code to check if SVN update skipped or in fact did update the repo successfully?

For now, I am using the following solution but not sure if it is an elegant one:

if 'Skipped' in output:
    logging.error('SVN update failed!')
    exit(-1)

Elegant or not, if it works it works! However I think you should look into pysvn, it might do the things you need. However it seems to be using svn 1.6 and not 1.7: http://pysvn.tigris.org/docs/pysvn_prog_guide.html

So looks like this will be a good solution taking into consideation David's suggestion:

cmd = 'svn update http://myserver/myrepo'
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output, stderr = p.communicate()      
status = p.returncode
if output.strip().startswith('Skipped'):
    logging.error('A fatal has error occurred while executing command: ' + cmd)
    exit(-1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM