我试图设置一个脚本来读取Ant生成的输出。我的背景是我正在使用Salesforce代码部署和检索。这些都是使用Ant命令完成的,结果通常显示在屏幕上(我也可以获得返回代码)。
在构建成功时获取代码很容易。但是,当构建失败时,看起来像蚂蚁会返回两个单独的“片段”。输出数据。不过,Python的subprocess.check_output只获得了第一部分。我也需要第二部分,因为它是注册所有部署错误的地方。
示例:
< 18digitcodehere>出于安全原因,代替原始代码。
成功
[sf:retrieve] Request for a retrieve submitted successfully.
[sf:retrieve] Request ID for the current retrieve task: <18digitcodehere>
[sf:retrieve] Waiting for server to finish processing the request...
[sf:retrieve] Request Status: Pending
[sf:retrieve] Request Status: Succeeded
[sf:retrieve] Finished request <18digitcodehere> successfully.
失败
第一部分:
[sf:deploy] Request for a deploy submitted successfully.
[sf:deploy] Request ID for the current deploy task: 0Af1a0000081rk1CAA
[sf:deploy] Waiting for server to finish processing the request...
[sf:deploy] Request Status: Pending
[sf:deploy] Request Status: Pending
[sf:deploy] Request Status: Failed
第二部分:
这个包含我想要阅读的错误。它没有显示......
BUILD FAILED
<mySystempath>\build.xml:125:
*********** DEPLOYMENT FAILED ***********
Request ID: <18digitcodehere>
All Component Failures:
1. labels/CustomLabels.labels (Something) -- Error: Not in package.xml
2. labels/CustomLabels.labels (AnotherThing) -- Error: Not in package.xml
3. labels/CustomLabels.labels (Thinggy) -- Error: Not in package.xml
4. labels/CustomLabels.labels (YetAnotherThing) -- Error: Not in package.xml
...
我目前的脚本是这样的:
导入子流程,重新
try:
result = subprocess.check_output(['ant', 'validatedeployment'], universal_newlines=True, shell=True)
# I can get the code using some regex magic here
except subprocess.CalledProcessError as e:
# the exception is thrown when the result is different from 0, I believe
# but here I get the first part of the output, but not the second part
所以现在我可以获得信息,如果构建成功(我需要得到的只是消息中的成功&#39;基本上),并且当构建失败时我得到它失败,但是没有找到失败的真正原因。
任何提示?
答案 0 :(得分:1)
Ant将错误消息写入stderr
,而不是stdout
。
默认情况下,Python的subprocess.check_output
不会捕获stderr
。
要捕获stderr
,请使用以下命令:
subprocess.check_output(
['ant', 'validatedeployment'],
universal_newlines=True,
shell=True,
stderr=subprocess.STDOUT # add this line
)