def setUpContext(self, stack): lines = self.__test.docstring.splitlines() indent = min((len(line) - len(line.lstrip()) for line in lines[1:] if line.strip()), default=0) blank = True requires = list(self.__requires) for line in lines: if blank and line[indent:].startswith('.. requires:: '): requires.extend(name.strip() for name in line[indent+13:].split(',')) blank = not line.strip() missing = tuple(filter(nutils.testing._not_has_module, requires)) if missing: self.skipTest('missing module{}: {}'.format('s' if len(missing) > 1 else '', ','.join(missing))) if 'matplotlib' in requires: import matplotlib.testing matplotlib.testing.setup() super().setUpContext(stack) stack.enter_context(warnings.catch_warnings()) warnings.simplefilter('ignore') stack.enter_context(treelog.set(_doctestlog)) import numpy printoptions = numpy.get_printoptions() if 'legacy' in printoptions: stack.callback(numpy.set_printoptions, **printoptions) numpy.set_printoptions(legacy='1.13')
def create_log(app, env, node, contnode): logger = sphinx.util.logging.getLogger(__name__) if node['reftype'] == 'nutils-log': script = node.get('script') scriptname = str(script.relative_to(project_root)) cmdline_args = node['reftarget'] cmdline = ' '.join(map(shlex.quote, [scriptname, *cmdline_args])) target = '_logs/{}/index'.format( urllib.parse.quote(cmdline, safe='').replace('%', '+')) dst_log = (pathlib.Path(app.builder.outdir) / target).parent if dst_log.exists( ) and dst_log.stat().st_mtime > script.stat().st_mtime: logger.debug( 'Skip building log of {cmdline} because it already exists and ' 'is newer than {script}. Please touch {script} to force a rebuild.' .format(script=scriptname, cmdline=cmdline)) else: if dst_log.exists(): logger.debug('purging old log files... {}'.format(dst_log)) shutil.rmtree(str(dst_log)) else: dst_log.parent.mkdir(parents=True, exist_ok=True) logger.info('creating log... {}'.format(cmdline)) script_dict = runpy.run_path(str(script), run_name='__log_builder__') # Parse cmdline. func = script_dict['main'] params = inspect.signature(func).parameters doc = stringly.util.DocString(func) kwargs = doc.defaults.copy() kwargs.update(arg.split('=', 1) for arg in cmdline_args if arg) # Run script. import matplotlib.testing matplotlib.testing.setup() with nutils.cli._htmllog( outdir=str(dst_log), scriptname=scriptname, kwargs=[(name, kwargs[name], doc.argdocs[name]) for name in params ]) as log, treelog.set(log), nutils.matrix.backend( 'scipy'), nutils.warnings.via(treelog.warning): func( **{ name: stringly.loads(params[name].annotation, kwargs[name]) for name in params }) (dst_log / 'log.html').rename(dst_log / 'index.html') refnode = docutils.nodes.reference('', '', internal=False, refuri=app.builder.get_relative_uri( env.docname, target)) refnode.append(contnode) return refnode
def setUp(self): super().setUp() self.enter_context( treelog.set( treelog.TeeLog(treelog.StdoutLog(), treelog.LoggingLog()))) self.enter_context(_builtin_warnings.catch_warnings()) _builtin_warnings.simplefilter('error', warnings.NutilsWarning)
def setUpContext(self, stack): lines = self.__test.docstring.splitlines() indent = min((len(line) - len(line.lstrip()) for line in lines[1:] if line.strip()), default=0) blank = True requires = list(self.__requires) for line in lines: if blank and line[indent:].startswith('.. requires:: '): requires.extend(name.strip() for name in line[indent + 13:].split(',')) blank = not line.strip() missing = tuple(filter(nutils.testing._not_has_module, requires)) if missing: self.skipTest('missing module{}: {}'.format( 's' if len(missing) > 1 else '', ','.join(missing))) if 'matplotlib' in requires: import matplotlib.testing matplotlib.testing.setup() super().setUpContext(stack) stack.enter_context(warnings.catch_warnings()) warnings.simplefilter('ignore') stack.enter_context(treelog.set(_doctestlog)) import numpy printoptions = numpy.get_printoptions() if 'legacy' in printoptions: stack.callback(numpy.set_printoptions, **printoptions) numpy.set_printoptions(legacy='1.13')
def setup(scriptname: str, kwargs: typing.List[typing.Tuple[str, str, str]], outrootdir: str = '~/public_html', outdir: typing.Optional[str] = None, cachedir: str = 'cache', cache: bool = False, nprocs: int = 1, matrix: str = 'auto', richoutput: typing.Optional[bool] = None, outrooturi: typing.Optional[str] = None, outuri: typing.Optional[str] = None, verbose: typing.Optional[int] = 4, pdb: bool = False, gracefulexit: bool = True, **unused): '''Set up compute environment.''' from . import cache as _cache, parallel as _parallel, matrix as _matrix for name in unused: warnings.warn( 'ignoring unused configuration variable {!r}'.format(name)) if outdir is None: outdir = os.path.join(os.path.expanduser(outrootdir), scriptname) if outrooturi is None: outrooturi = pathlib.Path( outrootdir).expanduser().resolve().as_uri() outuri = outrooturi.rstrip('/') + '/' + scriptname elif outuri is None: outuri = pathlib.Path(outdir).resolve().as_uri() if richoutput is None: richoutput = sys.stdout.isatty() consolellog = treelog.RichOutputLog() if richoutput else treelog.StdoutLog( ) if verbose is not None: consolellog = treelog.FilterLog(consolellog, minlevel=tuple(Level)[5 - verbose]) htmllog = _htmllog(outdir, scriptname, kwargs) if nprocs == 1: os.environ['MKL_THREADING_LAYER'] = 'SEQUENTIAL' with htmllog, \ _status(outuri+'/'+htmllog.filename, richoutput), \ treelog.set(treelog.TeeLog(consolellog, htmllog)), \ _traceback(richoutput=richoutput, postmortem=pdb, exit=gracefulexit), \ warnings.via(treelog.warning), \ _cache.enable(os.path.join(outdir, cachedir)) if cache else _cache.disable(), \ _parallel.maxprocs(nprocs), \ _matrix.backend(matrix), \ _signal_handler(signal.SIGINT, functools.partial(_breakpoint, richoutput)): treelog.info('nutils v{}'.format(_version())) treelog.info('start', time.ctime()) yield treelog.info('finish', time.ctime())
def setUpContext(self, stack): super().setUpContext(stack) stack.enter_context(warnings.catch_warnings()) warnings.simplefilter('ignore') stack.enter_context(treelog.set(_doctestlog)) import numpy printoptions = numpy.get_printoptions() if 'legacy' in printoptions: stack.callback(numpy.set_printoptions, **printoptions) numpy.set_printoptions(legacy='1.13')
def test_data(self): with tempfile.TemporaryDirectory() as outdir, treelog.set(treelog.DataLog(outdir)): kwargs = {} if self.p is not None: kwargs['p'] = self.p if self.c is not None: kwargs['c'] = self.c nutils.export.vtk('test', self.tri, self.x, **kwargs) with open(os.path.join(outdir, 'test.vtk'), 'rb') as f: data = f.read() self.assertEqual(data, b''.join(self.data))
def test_data(self): with tempfile.TemporaryDirectory() as outdir, treelog.set(treelog.DataLog(outdir)): kwargs = {} if self.p is not None: kwargs['p'] = self.p if self.c is not None: kwargs['c'] = self.c nutils.export.vtk('test', self.tri, self.x, **kwargs) with open(os.path.join(outdir, 'test.vtk'), 'rb') as f: data = f.read() self.assertEqual(data, b''.join(self.data))
def run(self): document = self.state.document env = document.settings.env nodes = [] indent = min(len(line) - len(line.lstrip()) for line in self.content) code = ''.join(line[indent:] + '\n' for line in self.content) code_wo_spread = nutils.testing.FloatNeighborhoodOutputChecker.re_spread.sub( lambda m: m.group(0).split('±', 1)[0], code) literal = docutils.nodes.literal_block(code_wo_spread, code_wo_spread, classes=['console']) literal['language'] = 'python3' literal['linenos'] = False sphinx.util.nodes.set_source_info(self, literal) nodes.append(literal) import matplotlib.testing matplotlib.testing.setup() import matplotlib.pyplot parser = doctest.DocTestParser() runner = doctest.DocTestRunner( checker=nutils.testing.FloatNeighborhoodOutputChecker(), optionflags=doctest.ELLIPSIS) globs = getattr(document, '_console_globs', {}) test = parser.get_doctest(code, globs, 'test', env.docname, self.lineno) with treelog.set(self._console_log): failures, tries = runner.run(test, clear_globs=False) for fignum in matplotlib.pyplot.get_fignums(): fig = matplotlib.pyplot.figure(fignum) with io.BytesIO() as f: fig.savefig(f, format='svg') name = hashlib.sha1(f.getvalue()).hexdigest() + '.svg' uri = 'data:image/svg+xml;base64,{}'.format( base64.b64encode(f.getvalue()).decode()) nodes.append( docutils.nodes.image('', uri=uri, alt='image generated by matplotlib')) matplotlib.pyplot.close('all') if failures: document.reporter.warning('doctest failed', line=self.lineno) document._console_globs = test.globs return nodes
def call(func, kwargs, scriptname, funcname=None): '''set up compute environment and call function''' outdir = config.outdir or os.path.join(os.path.expanduser(config.outrootdir), scriptname) with contextlib.ExitStack() as stack: stack.enter_context(cache.enable(os.path.join(outdir, config.cachedir)) if config.cache else cache.disable()) stack.enter_context(matrix.backend(config.matrix)) stack.enter_context(log.set(log.FilterLog(log.RichOutputLog() if config.richoutput else log.StdoutLog(), minlevel=5-config.verbose))) if config.htmloutput: htmllog = stack.enter_context(log.HtmlLog(outdir, title=scriptname, htmltitle='<a href="http://www.nutils.org">{}</a> {}'.format(SVGLOGO, html.escape(scriptname)), favicon=FAVICON)) uri = (config.outrooturi.rstrip('/') + '/' + scriptname if config.outrooturi else pathlib.Path(outdir).resolve().as_uri()) + '/' + htmllog.filename if config.richoutput: t0 = time.perf_counter() bar = lambda running: '{0} [{1}] {2[0]}:{2[1]:02d}:{2[2]:02d}'.format(uri, 'RUNNING' if running else 'STOPPED', _hms(time.perf_counter()-t0)) stack.enter_context(stickybar.activate(bar, update=1)) else: log.info('opened log at', uri) htmllog.write('<ul style="list-style-position: inside; padding-left: 0px; margin-top: 0px;">{}</ul>'.format(''.join( '<li>{}={} <span style="color: gray;">{}</span></li>'.format(param.name, kwargs.get(param.name, param.default), param.annotation) for param in inspect.signature(func).parameters.values())), level=1, escape=False) stack.enter_context(log.add(htmllog)) stack.enter_context(warnings.via(lambda msg: log.warning(msg))) stack.callback(signal.signal, signal.SIGINT, signal.signal(signal.SIGINT, _sigint_handler)) log.info('nutils v{}'.format(_version())) log.info('start', time.ctime()) try: func(**kwargs) except (KeyboardInterrupt, SystemExit, pdb.bdb.BdbQuit): log.error('killed by user') return 1 except: log.error(traceback.format_exc()) if config.pdb: print(_mkbox( 'YOUR PROGRAM HAS DIED. The Python debugger', 'allows you to examine its post-mortem state', 'to figure out why this happened. Type "h"', 'for an overview of commands to get going.')) pdb.post_mortem() return 2 else: log.info('finish', time.ctime()) return 0
def run(self): document = self.state.document env = document.settings.env nodes = [] indent = min(len(line)-len(line.lstrip()) for line in self.content) code = ''.join(line[indent:]+'\n' for line in self.content) code_wo_spread = nutils.testing.FloatNeighborhoodOutputChecker.re_spread.sub(lambda m: m.group(0).split('±', 1)[0], code) literal = docutils.nodes.literal_block(code_wo_spread, code_wo_spread, classes=['console']) literal['language'] = 'python3' literal['linenos'] = False sphinx.util.nodes.set_source_info(self, literal) nodes.append(literal) import matplotlib.testing matplotlib.testing.setup() import matplotlib.pyplot parser = doctest.DocTestParser() runner = doctest.DocTestRunner(checker=nutils.testing.FloatNeighborhoodOutputChecker(), optionflags=doctest.ELLIPSIS) globs = getattr(document, '_console_globs', {}) test = parser.get_doctest(code, globs, 'test', env.docname, self.lineno) with treelog.set(self._console_log): failures, tries = runner.run(test, clear_globs=False) for fignum in matplotlib.pyplot.get_fignums(): fig = matplotlib.pyplot.figure(fignum) with io.BytesIO() as f: fig.savefig(f, format='svg') name = hashlib.sha1(f.getvalue()).hexdigest()+'.svg' uri = 'data:image/svg+xml;base64,{}'.format(base64.b64encode(f.getvalue()).decode()) nodes.append(docutils.nodes.image('', uri=uri, alt='image generated by matplotlib')) matplotlib.pyplot.close('all') if failures: document.reporter.warning('doctest failed', line=self.lineno) document._console_globs = test.globs return nodes
def setUpContext(self, stack): super().setUpContext(stack) self.outdir = pathlib.Path( stack.enter_context(tempfile.TemporaryDirectory())) stack.enter_context(treelog.set(treelog.DataLog(str(self.outdir))))
def call(func, kwargs, scriptname, funcname=None): '''set up compute environment and call function''' outdir = config.outdir or os.path.join( os.path.expanduser(config.outrootdir), scriptname) with contextlib.ExitStack() as stack: stack.enter_context( cache.enable(os.path.join(outdir, config.cachedir)) if config. cache else cache.disable()) stack.enter_context(matrix.backend(config.matrix)) stack.enter_context( log.set( log.FilterLog(log.RichOutputLog() if config.richoutput else log.StdoutLog(), minlevel=5 - config.verbose))) if config.htmloutput: html = stack.enter_context( log.HtmlLog( outdir, title=scriptname, htmltitle='<a href="http://www.nutils.org">{}</a> {}'. format(SVGLOGO, scriptname), favicon=FAVICON)) uri = (config.outrooturi.rstrip('/') + '/' + scriptname if config.outrooturi else pathlib.Path( outdir).resolve().as_uri()) + '/' + html.filename if config.richoutput: t0 = time.perf_counter() bar = lambda running: '{0} [{1}] {2[0]}:{2[1]:02d}:{2[2]:02d}'.format( uri, 'RUNNING' if running else 'STOPPED', _hms(time.perf_counter() - t0)) stack.enter_context(stickybar.activate(bar, update=1)) else: log.info('opened log at', uri) html.write( '<ul style="list-style-position: inside; padding-left: 0px; margin-top: 0px;">{}</ul>' .format(''.join( '<li>{}={} <span style="color: gray;">{}</span></li>'. format(param.name, kwargs.get(param.name, param.default), param.annotation) for param in inspect.signature(func).parameters.values())), level=1, escape=False) stack.enter_context(log.add(html)) stack.enter_context(warnings.via(log.warning)) stack.callback(signal.signal, signal.SIGINT, signal.signal(signal.SIGINT, _sigint_handler)) log.info('nutils v{}'.format(_version())) log.info('start', time.ctime()) try: func(**kwargs) except (KeyboardInterrupt, SystemExit, pdb.bdb.BdbQuit): log.error('killed by user') return 1 except: log.error(traceback.format_exc()) if config.pdb: print( _mkbox('YOUR PROGRAM HAS DIED. The Python debugger', 'allows you to examine its post-mortem state', 'to figure out why this happened. Type "h"', 'for an overview of commands to get going.')) pdb.post_mortem() return 2 else: log.info('finish', time.ctime()) return 0
def setUpContext(self, stack): stack.enter_context(treelog.set(treelog.LoggingLog()))
def setUp(self): super().setUp() self.enter_context( treelog.set( treelog.TeeLog(treelog.StdoutLog(), treelog.LoggingLog())))
def inner(*args, **kwargs): with log.set(logger): return func(*args, **kwargs)
def test_replay_in_current(self): recordlog = treelog.RecordLog() recordlog.write('test', level=treelog.proto.Level.info) with self.assertSilent(), treelog.set( treelog.LoggingLog()), self.assertLogs('nutils'): recordlog.replay()
def setUpContext(self, stack): stack.enter_context( treelog.set( treelog.TeeLog(treelog.StdoutLog(), treelog.LoggingLog())))
def test_output(self): with self.assertSilent(), self.output_tester() as log, treelog.set( log): self.generate()
def create_log(app, env, node, contnode): logger = sphinx.util.logging.getLogger(__name__) if node['reftype'] == 'nutils-log': script = node.get('script') scriptname = str(script.relative_to(project_root)) cmdline_args = node['reftarget'] cmdline = ' '.join(map(shlex.quote, [scriptname, *cmdline_args])) target = '_logs/{}/index'.format( urllib.parse.quote(cmdline, safe='').replace('%', '+')) dst_log = (pathlib.Path(app.builder.outdir) / target).parent if dst_log.exists( ) and dst_log.stat().st_mtime > script.stat().st_mtime: logger.debug( 'Skip building log of {cmdline} because it already exists and ' 'is newer than {script}. Please touch {script} to force a rebuild.' .format(script=scriptname, cmdline=cmdline)) else: if dst_log.exists(): logger.debug('purging old log files... {}'.format(dst_log)) shutil.rmtree(str(dst_log)) else: dst_log.parent.mkdir(parents=True, exist_ok=True) logger.info('creating log... {}'.format(cmdline)) script_dict = runpy.run_path(str(script), run_name='__log_builder__') # Parse cmdline. params = inspect.signature(script_dict['main']).parameters.values() kwargs = {param.name: param.default for param in params} for arg in cmdline_args: if not arg: continue name, sep, value = arg.lstrip('-').partition('=') if not sep: value = not name.startswith('no') if not value: name = name[2:] if name not in kwargs: logger.error('unkown argument {!r}'.format(name)) return default = kwargs[name] try: if isinstance(default, bool) and not isinstance(value, bool): raise Exception( 'boolean value should be specifiec as --{0}/--no{0}' .format(name)) kwargs[name] = default.__class__(value) except Exception as e: logger.error('invalid argument for {!r}: {}'.format( name, e)) return # Run script. import matplotlib.testing matplotlib.testing.setup() func = script_dict['main'] with treelog.HtmlLog( str(dst_log), title=scriptname, htmltitle='{} {}'.format(nutils.cli.SVGLOGO, html.escape(scriptname)), favicon=nutils.cli.FAVICON) as log, treelog.set( log), nutils.matrix.backend( 'scipy'), nutils.warnings.via(treelog.warning): log.write( '<ul style="list-style-position: inside; padding-left: 0px; margin-top: 0px;">{}</ul>' .format(''.join( '<li>{}={} <span style="color: gray;">{}</span></li>'. format(param.name, kwargs.get( param.name, param.default), param.annotation) for param in inspect.signature( func).parameters.values())), level=1, escape=False) func(**kwargs) (dst_log / 'log.html').rename(dst_log / 'index.html') refnode = docutils.nodes.reference('', '', internal=False, refuri=app.builder.get_relative_uri( env.docname, target)) refnode.append(contnode) return refnode
def create_log(app, env, node, contnode): logger = sphinx.util.logging.getLogger(__name__) if node['reftype'] == 'nutils-log': script = node.get('script') scriptname = str(script.relative_to(project_root)) cmdline_args = node['reftarget'] cmdline = ' '.join(map(shlex.quote, [scriptname, *cmdline_args])) target = '_logs/{}/index'.format(urllib.parse.quote(cmdline, safe='').replace('%', '+')) dst_log = (pathlib.Path(app.builder.outdir)/target).parent if dst_log.exists() and dst_log.stat().st_mtime > script.stat().st_mtime: logger.debug('Skip building log of {cmdline} because it already exists and ' 'is newer than {script}. Please touch {script} to force a rebuild.' .format(script=scriptname, cmdline=cmdline)) else: if dst_log.exists(): logger.debug('purging old log files... {}'.format(dst_log)) shutil.rmtree(str(dst_log)) else: dst_log.parent.mkdir(parents=True, exist_ok=True) logger.info('creating log... {}'.format(cmdline)) script_dict = runpy.run_path(str(script), run_name='__log_builder__') # Parse cmdline. params = inspect.signature(script_dict['main']).parameters.values() kwargs = {param.name: param.default for param in params} for arg in cmdline_args: if not arg: continue name, sep, value = arg.lstrip('-').partition('=') if not sep: value = not name.startswith('no') if not value: name = name[2:] if name not in kwargs: logger.error('unkown argument {!r}'.format(name)) return default = kwargs[name] try: if isinstance(default, bool) and not isinstance(value, bool): raise Exception('boolean value should be specifiec as --{0}/--no{0}'.format(name)) kwargs[name] = default.__class__(value) except Exception as e: logger.error('invalid argument for {!r}: {}'.format(name, e)) return # Run script. import matplotlib.testing matplotlib.testing.setup() func = script_dict['main'] with treelog.HtmlLog(str(dst_log), title=scriptname, htmltitle='{} {}'.format(nutils.cli.SVGLOGO, html.escape(scriptname)), favicon=nutils.cli.FAVICON) as log, treelog.set(log), nutils.matrix.backend('scipy'), nutils.warnings.via(treelog.warning): log.write('<ul style="list-style-position: inside; padding-left: 0px; margin-top: 0px;">{}</ul>'.format(''.join( '<li>{}={} <span style="color: gray;">{}</span></li>'.format(param.name, kwargs.get(param.name, param.default), param.annotation) for param in inspect.signature(func).parameters.values())), level=1, escape=False) func(**kwargs) (dst_log/'log.html').rename(dst_log/'index.html') refnode = docutils.nodes.reference('', '', internal=False, refuri=app.builder.get_relative_uri(env.docname, target)) refnode.append(contnode) return refnode
def setUpContext(self, stack): super().setUpContext(stack) stack.enter_context(warnings.catch_warnings()) warnings.simplefilter('ignore') stack.enter_context(treelog.set(_doctestlog))
def setUpContext(self, stack): stack.enter_context(treelog.set(treelog.LoggingLog()))
def setUpContext(self, stack): super().setUpContext(stack) self.outdir = pathlib.Path(stack.enter_context(tempfile.TemporaryDirectory())) stack.enter_context(treelog.set(treelog.DataLog(str(self.outdir))))