2013-07-14 38 views
2

在織物任務中如何清理(例如刪除臨時文件等)是否已經收到了智慧?如果我使用atexit模塊,正如我通常那樣,那麼我有困難,因爲我不能使用裝飾器來裝飾傳遞給atexit.register()的函數。或者我可以嗎?其他面料用戶如何處理這個問題?atexit織物清理操作

回答

5

我也有同樣的問題。 Next Code並不理想,但我目前有這樣的實現。

fabfile.py

from functools import wraps 
from fabric.network import needs_host 
from fabric.api import run, env 

def runs_final(func): 
    @wraps(func) 
    def decorated(*args, **kwargs): 
     if env.host_string == env.all_hosts[-1]: 
      return func(*args, **kwargs) 
     else: 
      return None 
    return decorated 

@needs_host 
def hello(): 
    run('hostname') 
    atexit() 

@runs_final 
def atexit(): 
    print ('this is at exit command.') 

結果:

fabric$ fab hello -H web01,web02 
>[web01] Executing task 'hello' 
>[web01] run: hostname 
>[web01] out: web01 
>[web01] out: 
>[web02] Executing task 'hello' 
>[web02] run: hostname 
>[web02] out: web02 
>[web02] out: 
> 
>this is at exit command. 
> 
>Done. 
+0

感謝,這是有趣的,我將開始用它進行試驗。我打算讓這個問題開放一段時間,看看是否有其他事情出現。 – simon

+0

好吧,似乎沒有比這更好的答案了(這並不是我真正想要的)。畢竟,我想我會堅持使用我的BASH腳本。謝謝! – simon