Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fabric equivalent of try finally

Tags:

python

fabric

In the eventuality that Fabric exits cleanly or not, I need to execute a bunch of clean-up tasks (mostly delete temp files and folders).

How can I achieve this with Fabric?

like image 522
Quintin Par Avatar asked Jun 22 '12 18:06

Quintin Par


1 Answers

Put something like this in your fabfile:

from fabric.context_managers import settings

def task_name():
    # commands that are not expected to fail
    ...
    with settings(warn_only=True):
        # commands that might fail
        ...
    clean_up()

You may even want to give the entire task the warn_only=True treatment, if you don't care:

@with_settings(warn_only=True)
def task_name():
    ...

(more info)

like image 100
tshepang Avatar answered Oct 13 '22 09:10

tshepang