[英]How to turn warnings into errors when building sphinx documentation with setuptools?
I am using setuptools to build my sphinx documentation of a python project ( python setup.py build_sphinx
).我正在使用 setuptools 来构建 python 项目的 sphinx 文档(
python setup.py build_sphinx
)。
As found on, eg, this site , I have configured the build process using the setup.cfg:例如,在这个站点上发现,我已经使用 setup.cfg 配置了构建过程:
[build_sphinx]
source-dir = docs/source
build-dir = docs/build
all_files = 1
However, I would like to add some more options.但是,我想添加更多选项。 Specifically, I would like to turn all warnings into errors, which would work with the
sphinx-build
command with the option -W
:具体来说,我想将所有警告转换为错误,这将与带有选项
-W
的sphinx-build
命令一起使用:
sphinx-build --help
Sphinx v1.1.3
Usage: /usr/bin/sphinx-build [options] sourcedir outdir [filenames...]
Options: -b <builder> -- builder to use; default is html
-a -- write all files; default is to only write new and changed files
-E -- don't use a saved environment, always read all files
-t <tag> -- include "only" blocks with <tag>
-d <path> -- path for the cached environment and doctree files
(default: outdir/.doctrees)
-c <path> -- path where configuration file (conf.py) is located
(default: same as sourcedir)
-C -- use no config file at all, only -D options
-D <setting=value> -- override a setting in configuration
-A <name=value> -- pass a value into the templates, for HTML builder
-n -- nit-picky mode, warn about all missing references
-N -- do not do colored output
-q -- no output on stdout, just warnings on stderr
-Q -- no output at all, not even warnings
-w <file> -- write warnings (and errors) to given file
-W -- turn warnings into errors
-P -- run Pdb on exception
Modi:
* without -a and without filenames, write new and changed files.
* with -a, write all files.
* with filenames, write these.
I do not see a similar option for python setup.py build_sphinx
:我没有看到
python setup.py build_sphinx
的类似选项:
python setup.py build_sphinx --help
Common commands: (see '--help-commands' for more)
setup.py build will build the package underneath 'build/'
setup.py install will install the package
Global options:
--verbose (-v) run verbosely (default)
--quiet (-q) run quietly (turns verbosity off)
--dry-run (-n) don't actually do anything
--help (-h) show detailed help message
--no-user-cfg ignore pydistutils.cfg in your home directory
Options for 'BuildDoc' command:
--fresh-env (-E) discard saved environment
--all-files (-a) build all files
--source-dir (-s) Source directory
--build-dir Build directory
--config-dir (-c) Location of the configuration directory
--builder (-b) The builder to use. Defaults to "html"
--project The documented project's name
--version The short X.Y version
--release The full version, including alpha/beta/rc tags
--today How to format the current date, used as the replacement
for |today|
--link-index (-i) Link index.html to the master doc
usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
or: setup.py --help [cmd1 cmd2 ...]
or: setup.py --help-commands
or: setup.py cmd --help
Does anyone know, if turning all warnings into errors can be achieved when building the sphinx docu with setuptools?有谁知道,在使用 setuptools 构建 sphinx 文档时是否可以实现将所有警告转化为错误?
Edit:编辑:
The option -W
is not recognized by setuptools: setuptools 无法识别选项
-W
:
python setup.py build_sphinx -W
usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
or: setup.py --help [cmd1 cmd2 ...]
or: setup.py --help-commands
or: setup.py cmd --help
error: option -W not recognized
If instead, like me, you're using make
to build your html docs with Sphinx, then you can do this to turn warnings into errors and cause make
to fail:相反,如果您像我一样使用
make
来构建带有 Sphinx 的 html 文档,那么您可以这样做以将警告转换为错误并导致make
失败:
make html SPHINXOPTS="-W"
This will cause the build to fail immediately when a warning is encountered.当遇到警告时,这将导致构建立即失败。 If you add
--keep-going
then the docs build will still fail but it will run to completion so you can see all the warnings.如果您添加
--keep-going
则文档构建仍将失败,但它将运行完成,因此您可以看到所有警告。 And -n
will invoke the 'nit-picky' option to check for broken links. -n
将调用 'nit-picky' 选项来检查断开的链接。 So I find this useful when building the docs in my CI framework:所以我发现这在我的 CI 框架中构建文档时很有用:
make html SPHINXOPTS="-W --keep-going -n"
In recent versions of Sphinx , you do this by adding an additional option to the section in setup.cfg
:在Sphinx 的最新版本中,您可以通过向
setup.cfg
的部分添加附加选项来实现此setup.cfg
:
[build_sphinx]
all-files = 1
source-dir = docs/source
build-dir = docs/build
warning-is-error = 1
Support for this was added in Sphinx 1.5 , thus, this will not work with older versions. Sphinx 1.5中添加了对此的支持,因此,这不适用于旧版本。
The only solution I can manage is both simple and sub-optimal.我可以管理的唯一解决方案既简单又次优。
Change from:更改自:
python setup.py build_sphinx
to:到:
python -W error setup.py build_sphinx
That will turn all warnings into errors, including errors from setuptools, etc., which isn't exactly what you want, but it will stop on sphinx errors.这会将所有警告变成错误,包括来自 setuptools 等的错误,这不是您想要的,但它会在 sphinx 错误时停止。
If you're doing this to try and set up Continuous Integration or something, maybe this is good enough?如果您这样做是为了尝试设置持续集成或其他什么,也许这已经足够了?
UPDATE: See stephenfin's answer if using Sphinx 1.5+更新:如果使用 Sphinx 1.5+,请参阅stephenfin 的回答
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.