[IPOL discuss] various questions about python demos

Nicolas Limare nicolas.limare at cmla.ens-cachan.fr
Mon Nov 28 15:52:32 CET 2011


> > I use
> >  build.run("cd %s && cmake . && make" % self.src_dir, stdout=log_file)
> > hope you find this useful.
> 
> Yeah, my bad, I should have tried that first, because this also tests
> the command's output value.

But if this fails you don't know it it failed at cd, cmake or make stage.

> Just out of curiosity, this works because the && doesn't need to be
> escaped and the semicolon does, right?

I don't think so. build.run() is the python subprocess.Popen() command
with the parameter shell=True, ie it takes and runs one shell
command. Looking at the Python source, subprocess.Popen(...shell=True)
calls _execute_child(), which calls a posix subprocess with the
arguments "/bin/sh", "-c" and the string you entered in
build.run(). And this continues into the posix subprocess code[3]. But
the string you entered in build.run() is never modified.

[1]http://hg.python.org/cpython/file/bf51e32b2a81/Lib/subprocess.py#l655
[2]http://hg.python.org/cpython/file/bf51e32b2a81/Lib/subprocess.py#l1234
[3]http://hg.python.org/cpython/file/53ea5663b290/Modules/_posixsubprocess.c

And a succession of commands is not forbidden for the sh -c option.
/bin/sh -c "cd tmp; touch foo" works on my machine.

So I checked in Python...

$ python
Python 2.6.6 (r266:84292, Dec 26 2010, 22:31:48) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> p = subprocess.Popen("cd /tmp; touch bar", shell=True)
>>> p.wait()
0

No problem, I have a /tmp/bar file. Hmm... what was your problem? What
did not work? What was the python answer?

PS: however, the cwd parameter is better than "cd foo; do_stuff"

-- 
Nicolas LIMARE - CMLA - ENS Cachan    http://www.cmla.ens-cachan.fr/~limare/
IPOL - image processing on line                          http://www.ipol.im/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://tools.ipol.im/mailman/archive/discuss/attachments/20111128/e6d1a484/attachment.pgp>


More information about the discuss mailing list