我需要在Win32上运行这三个命令来分析/代码覆盖率报告。
vsperfcmd /start:coverage /output:run.coverage
helloclass
vsperfcmd /shutdown
我不能逐个运行一个命令,因为helloclass可执行文件应该在vsperfcmd的同一进程中进行分析。
我想的是创建一个批处理文件来运行这三个命令,并用Python运行批处理文件。但是,我认为python应该有一种方法来执行与启动shell和运行命令相同的操作。
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)
Calling an external command in Python
How can I run an external command asynchronously from Python?
http://docs.python.org/library/subprocess.html
尤其是签出“subprocess”模块并查找“shell”参数。
有趣的问题。
一种有效的方法是运行命令shell,然后通过
stdin
将命令传递给它(示例使用Python 3,对于Python 2,可以跳过decode()
调用)。注意,命令shell调用被设置为禁止除写入stdout的显式输出之外的所有内容。将其与单独调用
subprocess.call
的结果进行比较:后两个调用看不到第一个调用设置的环境,因为所有3个调用都是不同的子进程。
相关问题 更多 >
编程相关推荐