Python 在使用setuptools构建sphinx文档时,如何将警告转化为错误?

Python 在使用setuptools构建sphinx文档时,如何将警告转化为错误?,python,python-sphinx,setuptools,Python,Python Sphinx,Setuptools,我正在使用setuptools构建python项目的sphinx文档(python setup.py build\u sphinx) 例如,如上所示,我已使用setup.cfg配置了构建过程: [build_sphinx] source-dir = docs/source build-dir = docs/build all_files = 1 不过,我想补充一些其他选择。具体来说,我想将所有警告转换为错误,这将与带有选项-W的sphinx build命令一起使用: sphinx-buil

我正在使用setuptools构建python项目的sphinx文档(
python setup.py build\u sphinx

例如,如上所示,我已使用setup.cfg配置了构建过程:

[build_sphinx]
source-dir = docs/source
build-dir  = docs/build
all_files  = 1
不过,我想补充一些其他选择。具体来说,我想将所有警告转换为错误,这将与带有选项
-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.
有人知道,在使用setuptools构建sphinx docu时,是否可以将所有警告转化为错误

编辑:

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

我能管理的唯一解决方案既简单又次优

更改为:

python setup.py build_sphinx
致:

这会将所有的警告变成错误,包括setuptools中的错误,等等,这并不是您想要的,但是会在sphinx错误时停止

如果您这样做是为了尝试建立持续集成或其他什么,也许这就足够了

更新:查看是否在中使用Sphinx 1.5+

可通过在
setup.cfg
中的部分添加附加选项来完成此操作:

[build_sphinx]
all-files = 1
source-dir = docs/source
build-dir = docs/build
warning-is-error = 1

添加了对此的支持,因此,这将不适用于旧版本。

如果您像我一样使用
make
使用Sphinx构建html文档,那么您可以将警告变成错误并导致
make
失败:

make html SPHINXOPTS="-W"
这将导致生成在遇到警告时立即失败。如果添加
--继续
,则文档生成仍将失败,但将运行到完成,以便您可以看到所有警告。而
-n
将调用“挑剔”选项来检查断开的链接。因此,在我的CI框架中构建文档时,我发现这很有用:

make html SPHINXOPTS="-W --keep-going -n"

有关选项列表,请参阅。

python setup.py build\u sphinx-W会发生什么情况?该参数是,但显然无法设置,并且不是源代码列表中的
[build\u sphinx]
选项之一。如果显式地传递命令行参数不起作用,我想这是不可能的。另一种方法可能是研究是否可以在
setup.py
中使用
cmdclass
,并以这种方式指定参数。@jornsharpe:
python setup.py build\u sphinx-W
给出一个错误“option-W not recognized”。它是否告诉您哪些地方无法识别?它是通过Sphinx还是在到达Sphinx之前失败了?看起来setuptools无法识别该选项,请参见编辑。
make html SPHINXOPTS="-W"
make html SPHINXOPTS="-W --keep-going -n"