Ticket #6495: trac_6495_output_filter.patch

File trac_6495_output_filter.patch, 5.3 KB (added by vbraun, 7 years ago)

Initial patch

  • doc/common/builder.py

    # HG changeset patch
    # User Volker Braun <vbraun.name@gmail.com>
    # Date 1359636478 0
    # Node ID 39e2ad03ea0dc13b40c14cc9270f5a94b8f352f4
    # Parent  94f8c642b47ebf1d885cb25ea99fc9f461fc7b44
    Filter unnecessary messages, and line buffer the necessary ones
    
    diff --git a/doc/common/builder.py b/doc/common/builder.py
    a b  
    9494            # WEBSITESPHINXOPTS is either empty or " -A hide_pdf_links=1 "
    9595            options += WEBSITESPHINXOPTS
    9696
    97         build_command = 'sphinx-build'
     97        build_command = 'python '+os.path.join(SAGE_DOC, 'common', 'custom-sphinx-build.py')
    9898        build_command += ' -b %s -d %s %s %s %s'%(type, self._doctrees_dir(),
    9999                                                  options, self.dir,
    100100                                                  output_dir)
    101         logger.warning(build_command)
     101        logger.debug(build_command)
    102102        subprocess.call(build_command, shell=True)
    103103
    104         logger.warning("Build finished.  The built documents can be found in %s", output_dir)
     104        logger.info("Build finished and can be found in %s",
     105                    output_dir.replace(SAGE_DOC+'/', ''))
    105106       
    106107    f.is_output_format = True
    107108    return f
  • doc/common/conf.py

    diff --git a/doc/common/conf.py b/doc/common/conf.py
    a b  
    116116    refpath = 'output/html/en/reference/'
    117117    if not app.srcdir.endswith('reference'):
    118118        app.config.intersphinx_mapping[get_doc_abspath(refpath)] = get_doc_abspath(refpath+'objects.inv')
     119
    119120pythonversion = sys.version.split(' ')[0]
    120121# Python and Sage trac ticket shortcuts. For example, :trac:`7549` .
    121122
     
    609610    app.connect('autodoc-process-docstring', process_dollars)
    610611    app.connect('autodoc-process-docstring', process_inherited)
    611612    app.connect('autodoc-skip-member', skip_member)
    612 
     613   
    613614    # When building the standard docs, app.srcdir is set to SAGE_DOC +
    614615    # 'LANGUAGE/DOCNAME', but when doing introspection, app.srcdir is
    615616    # set to a temporary directory.  We don't want to use intersphinx,
     
    625626        app.connect('builder-inited', set_intersphinx_mappings)
    626627        app.connect('builder-inited', sphinx.ext.intersphinx.load_mappings)
    627628        app.connect('builder-inited', nitpick_patch_config)
    628         # Minimize GAP/libGAP RAM usage when we build the docs
    629         from sage.interfaces.gap import set_gap_memory_pool_size
    630         set_gap_memory_pool_size(0)  # will be rounded up to 1M
    631629
  • new file doc/common/custom-sphinx-build.py

    diff --git a/doc/common/custom-sphinx-build.py b/doc/common/custom-sphinx-build.py
    new file mode 100644
    - +  
     1"""
     2This is Sage's version of the sphinx-build script
     3
     4Enhancements are:
     5
     6* import the Sage library to access the docstrings, otherwise doc
     7  buliding doesn't work.
     8
     9* redirect stdout to our own logger, and remove some unwanted chatter.
     10"""
     11
     12import os
     13import sys
     14import re
     15
     16
     17
     18useless_chatter = (
     19    re.compile('^Running Sphinx v'),
     20    re.compile('^loading intersphinx inventory from '),
     21    re.compile('^Compiling a sub-document'),
     22    )
     23
     24
     25class SageSphinxLogger(object):
     26    """
     27    This implements the file object interface to serve as sys.stdout
     28    replacement.
     29    """
     30    ansi_color = re.compile(r'\x1b\[[0-9;]*m')
     31    prefix_len = 9
     32
     33    def __init__(self, stream, prefix):
     34        self._stream = stream
     35        prefix = prefix[0:self.prefix_len]
     36        prefix = ('[{0:'+str(self.prefix_len)+'}]').format(prefix)
     37        self._prefix = prefix
     38
     39    def _filter_out(self, line):
     40        line = re.sub(self.ansi_color, '', line)
     41        for regex in useless_chatter:
     42            if regex.match(line) is not None:
     43                return True
     44        return False
     45
     46    def _log_line(self, line):
     47        if self._filter_out(line):
     48            return
     49        self._stream.write(self._prefix + ' ' + line + '\n')
     50        self._stream.flush()
     51       
     52    _line_buffer = ''
     53   
     54    def _write(self, string):
     55        self._line_buffer += string
     56        lines = self._line_buffer.splitlines()
     57        for i, line in enumerate(lines):
     58            last = (i == len(lines)-1)
     59            if last and not self._line_buffer.endswith('\n'):
     60                self._line_buffer = line
     61                return
     62            self._log_line(line)
     63        self._line_buffer = ''
     64               
     65
     66    # file object interface follows
     67
     68    closed = False
     69    encoding = None
     70    mode = 'w'
     71    name = '<log>'
     72    newlines = None
     73    softspace = 0
     74
     75    def isatty(self):
     76        return True
     77
     78    def close(self):
     79        if self._line_buffer != '':
     80            self._log_line(self._line_buffer)
     81            self._line_buffer = ''
     82
     83    def flush(self):
     84        self._stream.flush()
     85       
     86    def write(self, str):
     87        self._write(str)
     88       
     89    def writelines(self, sequence):
     90        for line in sequence:
     91            self.write(line)
     92   
     93
     94output_dir = sys.argv[-1]
     95sys.stdout = SageSphinxLogger(sys.stdout, os.path.basename(output_dir))
     96
     97
     98
     99# pull in the Sage library
     100import sage.all
     101
     102# Minimize GAP/libGAP RAM usage when we build the docs
     103from sage.interfaces.gap import set_gap_memory_pool_size
     104set_gap_memory_pool_size(0)  # will be rounded up to 1M
     105
     106if __name__ == '__main__':
     107    from sphinx.cmdline import main
     108    sys.exit(main(sys.argv))
     109