Python calls ant build to determine command line exit status based on build state

Source: Internet
Author: User

Problems encountered when using Python to perform ant build:

When using Os.system () to invoke ant build, the command line always exits normally, regardless of build success or failure (build Successful/build FAILED)
To resolve the issue:
The first thought is to get the return value of the ant command, depending on the return value to determine the command line exit status (0 or not 0, 0 for normal exit)
Refer to the relevant data, know Python call system command functions are: Os.system, Os.popen, Commands.getstatusoutput/getstatus/getoutput, subprocess. Popen and so on.

    • Os.system () cannot get return value and output
    • Os.popen () Returns the object of file read, which reads the read () operation to see the output of the execution.
    • Commands.getstatusoutput () returns the exit status and output of the system command
    • Commands.getstatus () returns the exit status of the system command
    • Commands.getoutput () returns the output of the system command
When you execute an ant command line with a related function that uses commands:
No build direct exit is performed (Exit Status:1, the output is:is not an internal or external command, or a program or batch file that can be run)
Conclusion: It may be because the ant command is not a system command.
Then find the data and know the subprocess related functions, such as Subprocess.call, Subprocess.check_call, Subprocess.check_output
    • Subprocess.call (*popenargs, **kwargs) executes the command and waits for the command to end, returning the return value of the child process
    • Subprocess.check_call (*popenargs, **kwargs) executes the call command above and checks the return value, which throws a Calledprocesserror exception if the child process returns a non-0. This exception will have a ReturnCode property that records the return value of the child process.
    • Subprocess.check_output () executes the program and returns its standard output
When you use the Subprocess.call command to execute an ant command line:
The command line always exits normally (return value is 0) regardless of build success or failure (build Successful/build FAILED)
Conclusion: the command-line exit state (that is, the return value) is independent of the ant build state, only indicates whether the ant build has completed its normal execution state
Since the command line exit state (that is, the return value) is independent of the ant build State,
Then onlyParse command line output to determine command line exit status based on build success or failure
Then, use the Os.popen () command to get the output and parse the return status value

The specific Python script demo is as follows:

#!  Python.exe # python version 2.7.8#-*-coding:utf-8-*-"call Ant to perform build and return build result" __author__ = "Donhui" Import osbuild_successful = "Build Successful" build_failed = "Build FAILED" # call Ant to execute build and return build result #def build (Ant_target, Build_file):    ant_cmd = "A nt-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 on line:            status = 0    if 1 = = Status:        print build_failed,    return Statusif __name__ = = "__main__":    # call ant execution build    build_file = OS.GETCWD () + "/build.xml"    ant_targets = "Init"    if 0! = Build (Ant_targets, Build_file):        exit (1)

Reference:

    1. "Python executes system commands in Os.system (), Os.popen (), Commands" http://my.oschina.net/renwofei423/blog/17403
    2. "Python's subprocess module usage" http://blog.csdn.net/g457499940/article/details/17068277

Python calls ant build to determine command line exit status based on build state

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.