github.com/imran-kn/cilium-fork@v1.6.9/Documentation/_themes/sphinx_rtd_theme/tests/util.py (about)

     1  from __future__ import print_function
     2  
     3  import os
     4  import tempfile
     5  import shutil
     6  from contextlib import contextmanager
     7  
     8  import pytest
     9  from sphinx.application import Sphinx
    10  
    11  try:
    12      from StringIO import StringIO
    13  except ImportError:
    14      from io import StringIO
    15  
    16  
    17  @contextmanager
    18  def build(root, builder='html', **kwargs):
    19      tmpdir = tempfile.mkdtemp()
    20  
    21      srcdir = os.path.join(os.path.dirname(__file__), 'roots', root)
    22      destdir = os.path.join(tmpdir, builder)
    23      doctreedir = os.path.join(tmpdir, 'doctree/')
    24  
    25      status = StringIO()
    26      warning = StringIO()
    27  
    28      kwargs.update({
    29          'status': status,
    30          'warning': warning,
    31      })
    32  
    33      confoverrides = kwargs.pop('confoverrides', {})
    34      confoverrides['html_theme'] = 'sphinx_rtd_theme'
    35      extensions = confoverrides.get('extensions', [])
    36      extensions.append('readthedocs_ext.readthedocs')
    37      confoverrides['extensions'] = extensions
    38      kwargs['confoverrides'] = confoverrides
    39  
    40      try:
    41          app = Sphinx(srcdir, srcdir, destdir, doctreedir, builder, **kwargs)
    42          app.builder.build_all()
    43          yield (app, status.getvalue(), warning.getvalue())
    44      except Exception as e:
    45          print('# root:', root)
    46          print('# builder:', builder)
    47          print('# source:', srcdir)
    48          print('# destination:', destdir)
    49          print('# status:', '\n' + status.getvalue())
    50          print('# warning:', '\n' + warning.getvalue())
    51          raise
    52      finally:
    53          shutil.rmtree(tmpdir)
    54  
    55  
    56  def build_all(root, **kwargs):
    57      for builder in ['html', 'singlehtml', 'readthedocs', 'readthedocsdirhtml',
    58                      'readthedocssinglehtml', 'readthedocssinglehtmllocalmedia']:
    59          with build(root, builder, **kwargs) as ret:
    60              yield ret