2010-02-09 50 views
5

如果我有建立雞蛋,基本上通過運行如何在使用setuptools構建雞蛋時以編程方式檢測錯誤?

python setup.py bdist_egg --exclude-source-files 

爲一些使用setuptools來定義雞蛋如何構建setup.py文件腳本中,有一個簡單的方法來確定是否有任何製造雞蛋的錯誤?

我最近遇到的情況是模塊中有語法錯誤。 Setuptools向標準錯誤吐出一條消息,但繼續創建egg,省略掉已損壞的模塊。因爲這是批量生成一些雞蛋的一部分,所以錯過了錯誤,結果沒用。

有沒有一種方法來檢測錯誤編程時編寫一個雞蛋,除了捕獲標準錯誤和解析?

回答

5

distutils使用py_compile.compile()函數來編譯源文件。此函數採用doraise參數,當設置爲True時,會在編譯錯誤時引發異常(默認情況下,將錯誤打印到stderr)。 distutils不要撥py_compile.compile()doraise=True,所以編譯時不會因編譯錯誤而中止。

要停止發生錯誤並能夠檢查setup.py返回碼(錯誤將不爲零),可以修補py_compile.compile()函數。例如,在您的setup.py

from setuptools import setup 
import py_compile 

# Replace py_compile.compile with a function that calls it with doraise=True 
orig_py_compile = py_compile.compile 

def doraise_py_compile(file, cfile=None, dfile=None, doraise=False): 
    orig_py_compile(file, cfile=cfile, dfile=dfile, doraise=True) 

py_compile.compile = doraise_py_compile 

# Usual setup... 
+0

那裏的優秀工作!這看起來就像你期望setuptools/distutils直接支持的那種。 – SpoonMeiser 2010-02-11 10:36:47

相關問題