我正在尝试设置一个脚本来读取Ant生成的输出。我的背景是我正在处理Salesforce代码部署和检索。这些都是用Ant命令完成的,结果通常显示在屏幕上(我也可以得到一个返回代码)。
当构建成功时获取代码很容易。然而,当构建失败时,ant会返回两个独立的输出数据。不过,Python的子进程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
因此,现在我可以获得构建成功的信息(基本上,我只需要在消息中获得"成功"),当构建失败时,我会得到它失败的信息,但不知道它失败的真正原因。
有什么建议吗?
stderr
,而不是stdout
。
默认情况下,Python的subprocess.check_output
不会捕获stderr
。
要捕获stderr
,请使用以下方法:
subprocess.check_output(
['ant', 'validatedeployment'],
universal_newlines=True,
shell=True,
stderr=subprocess.STDOUT # add this line
)