1

I have the following code:

pwd = '/home/user/svnexport/Repo/'

updateSVN = "svn up " + pwd
cmd = os.popen(updateSVN)

getAllInfo = "svn info " + pwd + "branches/* " + pwd + "tags/* " + pwd + "trunk/*"
cmd = os.popen(getAllInfo)

How can I be sure cmd = os.popen(updateSVN) has completed execution before cmd = os.popen(getAllInfo) begins execution?

2
  • 3
    You should use subprocess.call() instead.
    – chepner
    Commented Jan 28, 2014 at 19:21
  • Seconding chepner's comment - subprocess.call (or subprocess.check_call) is the right way to do this. Commented Jan 28, 2014 at 22:17

4 Answers 4

2

You should use subprocess:

import subprocess
import glob
pwd = '/home/user/svnexport/Repo/'

updateSVN = ["svn", "up", pwd]
cmd = subprocess.Popen(updateSVN)
status = cmd.wait()

# the same can be achieved in a shorter way:
filelists = [glob.glob(pwd + i + "/*") for i in ('branches', 'tags', 'trunk')]
filelist = sum(filelists, []) # add them together

getAllInfo = ["svn", "info"] + filelist
status = subprocess.call(getAllInfo)

If you need to capture the subprocesses's output, instead do

process = subprocess.Popen(..., stdout=subprocess.PIPE)
data = process.stdout.read()
status = subprocess.wait()
1

If you need to want for the first command to terminate, you don't really need multithreading. You can just do

os.system(updateSVN)
os.system(getAllInfo)

If you really want to use updateSVN you can wait for it by

for _ in cmd:
    pass
1
  • Please don't suggest os.system() as there are better ways.
    – glglgl
    Commented Jan 28, 2014 at 22:09
1

Try the wait() method:

pwd = '/home/user/svnexport/Repo/'

updateSVN = "svn up " + pwd
cmd = os.popen(updateSVN)
cmd.wait()

getAllInfo = "svn info " + pwd + "branches/* " + pwd + "tags/* " + pwd + "trunk/*"
cmd = os.popen(getAllInfo)
1
  • os.wait() only available on Unix.
    – dns
    Commented Sep 3, 2020 at 20:58
1

If you want to wait, the simplest way is to use one of the following subprocess functions

  • call
  • check_call
  • check_output

Each one of those returns only after the command execution in the shell completes, see docs for details

Not the answer you're looking for? Browse other questions tagged or ask your own question.