我有一个试图执行外部命令的小脚本。但由于某种原因,我执行该命令的功能正在被完全跳过!似乎没有错误提出,它只是不执行。我在里面有一些调试打印语句来验证函数是否进入,但他们从不打印。我在它外面有一个打印语句来验证脚本没有死掉。那么是什么给了?为什么我的Python函数被跳过?
from xml.etree import ElementTree as et
import subprocess
pomFileLocation = "pom.xml"
uiAutomationCommand = "mvn clean install"
revertPomFileCommand = "git checkout pom.xml"
profileToSetToDefault = "smoketest"
def modifyxml(datafile, value):
print("modifying " + datafile)
tree = et.parse(datafile)
rootNodes = tree.getroot()
for node in rootNodes:
if "profiles" in node.tag:
for profile in node.iter():
foundIt = False
for param in profile.iter():
if "id" in param.tag and profileToSetToDefault in param.text:
foundIt = True
break
if foundIt == True:
for param in profile.iter():
if "activation" in param.tag:
for child in param.iter():
if "activeByDefault" in child.tag:
child.text = value
tree.write(datafile)
return
def runExternalCommand(comm):
print("running command " + comm)
p = subprocess.Popen(comm, bufsize=-1, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0]
print(str(p))
while(True):
print("still running")
retcode = p.poll()
line = p.stdout.readline()
yield line
if(retcode is not None):
print("Exiting")
break
return
if __name__ == '__main__':
modifyxml(pomFileLocation, "true")
#runExternalCommand(uiAutomationCommand)
runExternalCommand(revertPomFileCommand)
print("finished")
你在哪里得到'runExternalCommand()'定义?它被打破。请留下您的评论,以便其他人不会使用破损的代码。 [我的答案提供了一些替代方法](http://stackoverflow.com/a/28352727/4279) – jfs 2015-02-05 19:49:47