2011-01-14 57 views
6

我已經編寫了一個Python模塊,其中包含一個用C語言編寫的子模塊:模塊本身名爲foo,C部分爲foo._bar。該結構是這樣的:將Sphinx與distutils構建的C擴展結合使用

src/ 
    foo/__init__.py <- contains the public stuff 
    foo/_bar/bar.c <- the C extension 
doc/    <- Sphinx configuration 
    conf.py 
    ... 

foo/__init__.py進口_bar以加強它,並且有用的東西是foo模塊中暴露出來。這在構建時工作正常,但顯然不能以未編譯的形式工作,因爲_bar在構建之前不存在。

我想使用Sphinx來記錄項目,並在foo模塊上使用autodoc擴展名。這意味着我需要在構建文檔之前構建項目。

由於我使用distutils構建,所構建的模塊最終以一些不同名稱的dir build/lib.linux-ARCH-PYVERSION - 這意味着我不能將目錄硬編碼到一個Sphinx'conf.py

那麼,如何配置我的distutils setup.py腳本以在構建的模塊上運行Sphinx生成器?

爲了完整起見,這裏的調用setup(即「假」的東西是繼承buildbuild_ext自定義生成器):

setup(cmdclass = { 
     'fake': fake, 
     'build_ext_fake' : build_ext_fake 
     }, 
     package_dir = {'': 'src'}, 
     packages = ['foo'], 
     name = 'foo', 
     version = '0.1', 
     description = desc, 
     ext_modules = [module_real]) 

回答

4

由於distutils的具有搞清楚變量構建路徑,何不的一種方式只是用它?

import distutils.command.build 
from distutils.dist import Distribution 

b = distutils.command.build.build(Distribution()) 
b.initialize_options() 
b.finalize_options() 

print b.build_temp 

# If you're building a library, you might need: 
print b.build_lib 

# Other values of interest are: 
b.build_purelib 
b.build_platlib 
b.build_scripts 
b.build_base 

甚至認爲Distutils的文檔是稀疏,here you'll find one-liners關於什麼樣的身材都在那裏。

+0

這是一個很好的方法 - 我可以把它放在`conf.py`(我認爲)。我明天會嘗試。 – detly 2011-01-16 06:56:01

2

有更簡單的方法來獲得build目錄名稱:

>>> from distutils.util import get_platform 
>>> get_platform() 
'linux-x86_64' 

我會讓你完成字符串連接:)

另一種方法來解決你的問題是創建一個setup.cfg文件旁邊的setup.py與此內容:

[build_ext] 
inplace = 1 

這將建立在其父包目錄的擴展模塊。獅身人面像應該看到它。