Can I catch error codes when using Fabric to run() calls in a remote shell?

前端 未结 4 638
花落未央
花落未央 2020-12-23 13:17

Normally Fabric quits as soon as a run() call returns a non-zero exit code. For some calls, however, this is expected. For example, PNGOut returns an error code of 2 when it

4条回答
  •  [愿得一人]
    2020-12-23 13:54

    You can prevent aborting on non-zero exit codes by using the settings context manager and the warn_only setting:

    from fabric.api import settings
    
    with settings(warn_only=True):
        result = run('pngout old.png new.png')
        if result.return_code == 0: 
            do something
        elif result.return_code == 2: 
            do something else 
        else: #print error to user
            print result
            raise SystemExit()
    

    Update: My answer is outdated. See comments below.

提交回复
热议问题