3

I need to run those three commands for profiling/code coverage reporting on Win32.

vsperfcmd /start:coverage /output:run.coverage
helloclass
vsperfcmd /shutdown

I can't run one command by one because the helloclass executable should be profiled in the same process of vsperfcmd.

What I think of is to make a batch file to run those three commands, and run the batch file in Python. However, I think python should have a way to do the equivalent action of launching a shell and run commands.

  • Q : How can I run the commands in the same process in Python?
  • Q : Or, how can I launch command shell and run commands in Python?

SOLVED

import subprocess
cmdline = ["cmd", "/q", "/k", "echo off"]
cmd = subprocess.Popen(cmdline, stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True)
batch = b"""\
rem vsinstr -coverage helloclass.exe /exclude:std::*
vsperfcmd /start:coverage /output:run.coverage
helloclass
vsperfcmd /shutdown
exit
"""
cmd.stdin.write(batch)
cmd.stdin.flush() # Must include this to ensure data is passed to child process
result = cmd.stdout.read()
print(result)
prosseek
  • 155,475
  • 189
  • 518
  • 818
  • I have a similar problem as yours could you please help me http://stackoverflow.com/questions/38826029/running-a-cmd-in-the-vs2015-x64-arm-cross-tools-command-prompt . Thanks – lads Aug 08 '16 at 11:59

2 Answers2

7

Interesting question.

One approach that works is to run a command shell and then pipe commands to it via stdin (example uses Python 3, for Python 2 you can skip the decode() call). Note that the command shell invocation is set up to suppress everything except explicit output written to stdout.

>>> import subprocess
>>> cmdline = ["cmd", "/q", "/k", "echo off"]
>>> cmd = subprocess.Popen(cmdline, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
>>> batch = b"""\
... set TEST_VAR=Hello World
... set TEST_VAR
... echo %TEST_VAR%
... exit
... """
>>> cmd.stdin.write(batch)
59
>>> cmd.stdin.flush() # Must include this to ensure data is passed to child process
>>> result = cmd.stdout.read()
>>> print(result.decode())
TEST_VAR=Hello World
Hello World

Compare that to the result of separate invocations of subprocess.call:

>>> subprocess.call(["set", "TEST_VAR=Hello World"], shell=True)
0
>>> subprocess.call(["set", "TEST_VAR"], shell=True)
Environment variable TEST_VAR not defined
1
>>> subprocess.call(["echo", "%TEST_VAR%"], shell=True)
%TEST_VAR%
0

The latter two invocations can't see the environment set up by the first one, as all 3 are distinct child processes.

ncoghlan
  • 35,440
  • 8
  • 67
  • 77
2

Calling an external command in Python

How can I run an external command asynchronously from Python?

http://docs.python.org/library/subprocess.html

In particular checkout the 'subprocess' module and lookup the 'shell' parameter.

Community
  • 1
  • 1
Andreas Jung
  • 1
  • 18
  • 70
  • 118