Search code examples
python-2.7scons

How to get the exit status in python


i am using scons build tool with renesas compiler.

I want to know the exit status of SCONS when the build terminated with an error.

When scons executed the project successfully it will return 0. How to get the exit status or number to check the erros.

Is there any function in python to get them.

Thanks


Solution

  • There is SCons.Script.Main.exit_status and SCons.script.Main.exit_status.code, however I've found them to be unreliable in practice (i.e. they often differ from the actual return code).

    The actual exit code is set by calling sys.exit with the appropriate argument. It seems that the only way to access it is by replacing the original sys.exit with a wrapper:

    import atexit
    import sys
    
    exit_code = None
    original_exit = None
    
    def my_exit(code=0):
        global exit_code
        exit_code = code
        original_exit(code)
    
    original_exit = sys.exit
    sys.exit = my_exit
    
    @atexit.register
    def my_exit_handler():
        print "Exit code is", exit_code
    
    if __name__ == '__main__':
        sys.exit(1)
    

    If you need more information in case of an error you will probably also want to use a custom wrapper for sys.excepthook. There's also SCons.Script.GetBuildFailures.