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

时间:2021-08-15 07:30:45

I want to include a setuptools command to run Sphinx doctest as part of my package setup.py, like this:

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

$ python setup.py sphinx_doctest

I have a package structure like:

我有一个包结构,如:

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

How can I implement a setuptools command that does the equivalent of:

如何实现setuptools命令,该命令具有以下功能:

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

1 个解决方案

#1


2  

Subclass setuptools.Command and use sphinx.application.Sphinx to launch the sphinx.ext.doctest builder.

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

In 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,
    },
)

#1


2  

Subclass setuptools.Command and use sphinx.application.Sphinx to launch the sphinx.ext.doctest builder.

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

In 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,
    },
)