From 905ed466964509d01bf515ff3e42c96f2da04a63 Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Tue, 17 Feb 2009 19:19:09 +0100 Subject: [PATCH] There is now a ``-W`` option for sphinx-build that turns warnings into errors. ^ --- CHANGES | 3 +++ doc/intro.rst | 4 ++++ sphinx/application.py | 11 ++++++++++- sphinx/cmdline.py | 9 ++++++--- 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/CHANGES b/CHANGES index ddecfdb06..1c0b11c56 100644 --- a/CHANGES +++ b/CHANGES @@ -131,6 +131,9 @@ New features added - Quickstart can now generate a Windows ``make.bat`` file. + - There is now a ``-W`` option for sphinx-build that turns warnings + into errors. + Release 0.5.2 (in development) ============================== diff --git a/doc/intro.rst b/doc/intro.rst index f9e23e180..782328be4 100644 --- a/doc/intro.rst +++ b/doc/intro.rst @@ -133,6 +133,10 @@ The :program:`sphinx-build` script has several more options: Do not output anything on standard output, also suppress warnings. Only errors are written to standard error. +**-W** + Turn warnings into errors. This means that the build stops at the first + warning and ``sphinx-build`` exits with exit status 1. + **-P** (Useful for debugging only.) Run the Python debugger, :mod:`pdb`, if an unhandled exception occurs while building. diff --git a/sphinx/application.py b/sphinx/application.py index 9bb276f12..70a0a8232 100644 --- a/sphinx/application.py +++ b/sphinx/application.py @@ -28,6 +28,10 @@ class SphinxError(Exception): """ category = 'Sphinx error' +class SphinxWarning(SphinxError): + """Raised for warnings if warnings are treated as errors.""" + category = 'Warning, treated as error' + class ExtensionError(SphinxError): """Raised if something's wrong with the configuration.""" category = 'Extension error' @@ -78,7 +82,8 @@ CONFIG_FILENAME = 'conf.py' class Sphinx(object): def __init__(self, srcdir, confdir, outdir, doctreedir, buildername, - confoverrides, status, warning=sys.stderr, freshenv=False): + confoverrides, status, warning=sys.stderr, freshenv=False, + warningiserror=False): self.next_listener_id = 0 self._listeners = {} self.builderclasses = BUILTIN_BUILDERS.copy() @@ -95,11 +100,13 @@ class Sphinx(object): else: self._status = status self.quiet = False + if warning is None: self._warning = StringIO() else: self._warning = warning self._warncount = 0 + self.warningiserror = warningiserror self._events = events.copy() @@ -153,6 +160,8 @@ class Sphinx(object): self.builder.cleanup() def warn(self, message): + if self.warningiserror: + raise SphinxWarning('WARNING: %s\n' % message) self._warncount += 1 try: self._warning.write('WARNING: %s\n' % message) diff --git a/sphinx/cmdline.py b/sphinx/cmdline.py index 4642b7953..b4c3cc7b0 100644 --- a/sphinx/cmdline.py +++ b/sphinx/cmdline.py @@ -44,6 +44,7 @@ new and changed files -N -- do not do colored output -q -- no output on stdout, just warnings on stderr -Q -- no output at all, not even warnings + -W -- turn warnings into errors -P -- run Pdb on exception Modi: * without -a and without filenames, write new and changed files. @@ -57,7 +58,7 @@ def main(argv): nocolor() try: - opts, args = getopt.getopt(argv[1:], 'ab:d:c:CD:A:NEqQP') + opts, args = getopt.getopt(argv[1:], 'ab:d:c:CD:A:NEqQWP') allopts = set(opt[0] for opt in opts) srcdir = confdir = path.abspath(args[0]) if not path.isdir(srcdir): @@ -86,7 +87,7 @@ def main(argv): return 1 buildername = all_files = None - freshenv = use_pdb = False + freshenv = warningiserror = use_pdb = False status = sys.stdout warning = sys.stderr confoverrides = {} @@ -143,13 +144,15 @@ def main(argv): elif opt == '-Q': status = None warning = None + elif opt == '-W': + warningiserror = True elif opt == '-P': use_pdb = True confoverrides['html_context'] = htmlcontext try: app = Sphinx(srcdir, confdir, outdir, doctreedir, buildername, - confoverrides, status, warning, freshenv) + confoverrides, status, warning, freshenv, warningiserror) app.build(all_files, filenames) return app.statuscode except KeyboardInterrupt: