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

How can I run Sphinx doctests as part of setup.py?

我想在 运行 Sphinx doctest 中包含一个 setuptools 命令作为我的包 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

继承 setuptools.Command 并使用 sphinx.application.Sphinx to launch the 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,
    },
)