如何作为setup.py的一部分运行Sphinx doctests?

时间:2015-07-24 14:22:04

标签: python python-sphinx setuptools setup.py doctest

我想包含一个setuptools命令来运行Sphinx doctest作为我的软件包setup.py的一部分,如下所示:

$ python setup.py sphinx_doctest

我的包结构如下:

my_pkg
|---__init__.py
|---module1.py    # Containing reStructuredText docstrings with examples
docs
|---build
|---|---doctrees
|---source
|---|---conf.py   # Sphinx config
|---|---index.rst # Sphinx index file
setup.py

如何实现与以下内容相同的setuptools命令:

$ sphinx-build -b doctest -d docs/build/doctrees docs/source docs/build

1 个答案:

答案 0 :(得分:2)

子类setuptools.Command并使用sphinx.application.Sphinx启动sphinx.ext.doctest构建器。

在setup.py中:

from setuptools import setup, Command

class Doctest(Command):
    description = 'Run doctests with Sphinx'
    user_options = []

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def run(self):
        from sphinx.application import Sphinx
        sph = Sphinx('./docs/source', # source directory
                     './docs/source, # directory containing conf.py
                     './docs/build', # output directory
                     './docs/build/doctrees', # doctree directory
                     'doctest') # finally, specify the doctest builder
        sph.build()

sphinx_requires = ['sphinx>=1.3.1']

setup(
    name='mypkg',
    version='0.0.1',
    description='My Package',
    packages=['mypkg'],
    cmdclass={
        'doctests': Doctest
    },
    extras_require={
        'build_sphinx': sphinx_requires,
    },
)