Calling multiple programs with subprocess

A

amit

How does one go about calling multiple programs using subprocess?

This is the program flow:

C:\> wrenv.exe
C:\> make clean
...
...

The 'wrenv.exe' is necessary since it sets up the proper environment
for building. How do I use subprocess to execute 'wrenv.exe' and then
the 'make clean' command.

Any help is appreciated.

Thanks,
Amit
 
N

News123

Hi Amit,


As far as I know you can't really do this with subprocess,
because wrenv.exe and make would be called in different contexts.
THus wrenve.exe couldn't change the environment.

just try following:
- create a .bat file with both commands in it
- verify, that the bat file works
- call the bat file with Popen


However I am surprised, that a .exe file can modify the current environment.
At least under linux this wouldn't be possible (except with sourcing a
shell script)

Or do you mean with "setting up the proper environment" creating some
intermediate files?


bye


N
 
D

Dave Angel

amit said:
How does one go about calling multiple programs using subprocess?

This is the program flow:

C:\> wrenv.exe
C:\> make clean
..
..

The 'wrenv.exe' is necessary since it sets up the proper environment
for building. How do I use subprocess to execute 'wrenv.exe' and then
the 'make clean' command.

Any help is appreciated.

Thanks,
Amit
One way is to write a batch file (since you're on Windows), and execute
that with shell=True.

It's not the only way, or the most efficient. But it's most likely to
work, without knowing anything more about those two programs.

DaveA
 
B

Bryan

amit asked:
How does one go about calling multiple programs using subprocess?

This is the program flow:

C:\> wrenv.exe
C:\> make clean
..

The 'wrenv.exe' is necessary since it sets up the proper environment
for building. How do I use subprocess to execute 'wrenv.exe' and then
the 'make clean' command.

Any help is appreciated.

In this case I don't see that you need to call multiple programs
simultaneously. You call 'wrenv.exe', then when and if it completes
successfully you call 'make clean'. Calling multiple programs
sequentially should be straightforward. What, specifically, is going
wrong?

Python's subprocess module rocks on Unix, and provides some useful
corresponding features that are easily available on MS Windows. If
your job is to rock on Windows, don't commit to modules devoted to
rockin' on Unix. Python has multiple ways to run "wrenv.exe", then
"make clean". In particular, check out os.system.
 
A

Amit Uttamchandani

One way is to write a batch file (since you're on Windows), and
execute that with shell=True.

It's not the only way, or the most efficient. But it's most likely
to work, without knowing anything more about those two programs.

DaveA

Thanks Dave.

That's actually a good idea and in this case probably the only way that
this would work. I can auto-generate the batch file from the python
script with the parameters that I need and just execute that with the
subprocess.

I wish there was a cleaner was as well.

But thanks for tip!

Amit
 
K

Kushal Kumaran

Thanks Dave.

That's actually a good idea and in this case probably the only way that
this would work. I can auto-generate the batch file from the python
script with the parameters that I need and just execute that with the
subprocess.

I wish there was a cleaner was as well.

Run a shell (cmd.exe, I think) using subprocess and send it the
commands you want to run using the communicate() method.
 
D

Dennis Lee Bieber

That's actually a good idea and in this case probably the only way that
this would work. I can auto-generate the batch file from the python
script with the parameters that I need and just execute that with the
subprocess.

I wish there was a cleaner was as well.
If using a command shell processor on Windows (shell = True), you
might try passing as the command line

wrenv.exe && make clean

{a single & might work too -- if I understand the documentation, the &&
says to only run the second command IF the first did not return a
failure code}

C:\Documents and Settings\Dennis Lee Bieber>date /t && time /t
Sat 04/24/2010
12:26 PM

C:\Documents and Settings\Dennis Lee Bieber>data /t && time /t
'data' is not recognized as an internal or external command,
operable program or batch file.

C:\Documents and Settings\Dennis Lee Bieber>data /t & time /t
'data' is not recognized as an internal or external command,
operable program or batch file.
12:32 PM


Valid command string('Sat 04/24/2010 \r\n12:34 PM\r\n', '')

Second command bad('Sat 04/24/2010 \r\n', "'tim' is not recognized as an internal or
external command,\r\noperable program or batch file.\r\n")

First command bad, using &&; second command did not run('', "'dat' is not recognized as an internal or external
command,\r\noperable program or batch file.\r\n")

First command bad, using &; second command DID run('12:35 PM\r\n', "'dat' is not recognized as an internal or external
command,\r\noperable program or batch file.\r\n")
Without the shellTraceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "e:\Python25\lib\subprocess.py", line 594, in __init__
errread, errwrite)
File "e:\Python25\lib\subprocess.py", line 816, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
 
A

Amit Uttamchandani

On Sat, Apr 24, 2010 at 08:22:14AM +0530, Kushal Kumaran wrote:

[snip]
Run a shell (cmd.exe, I think) using subprocess and send it the
commands you want to run using the communicate() method.

Actually, I ended up using stdin.write('...\n'), and it works as expected:

#
# Set WindRiver environment for VxWorks 6.4
#
wrenv_path = "C:\WindRiver\wrenv.EXE"
args = [wrenv_path, '-p', 'vxworks-6.4']
proc = subprocess.Popen(args,
shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE
)
proc.stdin.write(str.encode('wrws_update -data %s -l %s -b clean\n' %
(workspace, self.target)))
output, error = proc.communicate()
print(output)
print(error)

You can keep using stdin.write until you call communicate() this allows
you to write certain commands to the stdin for the process you launched.
Note: wrenv.exe is actually a shell like cmd.exe.

The above code is written for Python 3.1.

Thanks for the help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top