标签:
在使用python执行Ant构建时遇到的问题:
使用os.system()调用Ant构建时,不论构建成功还是失败(BUILD SUCCESSFUL/BUILD FAILED),命令行的总是正常退出
要解决问题:
首先想到的是获取ant命令的返回值,根据返回值来决定命令行的退出状态(0或非0,0代表正常退出)
查阅相关资料,得知python调用系统命令的函数有:os.system、os.popen、commands.getstatusoutput/getstatus/getoutput、subprocess.Popen等。
具体Python脚本DEMO如下:
#!Python.exe # python version 2.7.8 # -*- coding: utf-8 -*- "调用Ant执行构建,并返回构建结果" __author__ = "donhui" import os BUILD_SUCCESSFUL = "BUILD SUCCESSFUL" BUILD_FAILED = "BUILD FAILED" # 调用Ant执行构建,并返回构建结果 # def build(ant_target, build_file): ant_cmd = "ant -f {0} {1}".format(build_file, ant_target) logging.info(ant_cmd) status = 1 for line in os.popen(ant_cmd): print line, if BUILD_SUCCESSFUL in line: status = 0 if 1 == status: print BUILD_FAILED, return status if __name__ == "__main__": # 调用Ant执行构建 build_file = os.getcwd() + "/build.xml" ant_targets = "init" if 0 != build(ant_targets, build_file): exit(1)
参考:
python调用Ant构建时根据构建状态来决定命令行退出状态
标签:
原文地址:http://my.oschina.net/donhui/blog/402361