我有一個試圖執行外部命令的小腳本。但由於某種原因,我執行該命令的功能正在被完全跳過!似乎沒有錯誤提出,它只是不執行。我在裏面有一些調試打印語句來驗證函數是否進入,但他們從不打印。我在它外面有一個打印語句來驗證腳本沒有死掉。那麼是什麼給了?爲什麼我的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