2011-01-14 60 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 

这将建立在其父包目录的扩展模块。狮身人面像应该看到它。