def printResult(tester, result, timing, start, end, options, color=True): f_result = '' caveats = '' first_directory = tester.specs['first_directory'] test_name = tester.specs['test_name'] status = tester.getStatus() cnt = (TERM_COLS-2) - len(test_name + result) color_opts = {'code' : options.code, 'colored' : options.colored} if color: if options.color_first_directory: test_name = colorText(first_directory, 'CYAN', **color_opts) + test_name.replace(first_directory, '', 1) # Strip out first occurence only # Color the Caveats CYAN m = re.search(r'(\[.*?\])', result) if m: caveats = m.group(1) f_result = colorText(caveats, 'CYAN', **color_opts) # Color test results based on status. # Keep any caveats that may have been colored if status: f_result += colorText(result.replace(caveats, ''), tester.getColor(), **color_opts) f_result = test_name + '.'*cnt + ' ' + f_result else: f_result = test_name + '.'*cnt + ' ' + result # Tack on the timing if it exists if timing: f_result += ' [' + '%0.3f' % float(timing) + 's]' if options.debug_harness: f_result += ' Start: ' + '%0.3f' % start + ' End: ' + '%0.3f' % end return f_result
def report_error(message, filename, line=None, src='', traceback=None, prefix='ERROR'): """ Helper for reporting error to logging module. Inputs: message[str]: The message to report, ERROR is automatically appended page[pages.Page]: The page that created the error info[Information]: The lexer information object traceback: The traceback (should be a string from traceback.format_exc()) """ title = '{}: {}'.format(prefix, message) if src: src = mooseutils.colorText(box(src, line=line, width=100), 'LIGHT_CYAN') if line is not None: filename = mooseutils.colorText('{}:{}\n'.format(filename, line), 'RESET') else: filename = mooseutils.colorText('{}\n'.format(filename), 'RESET') trace = '\n' + mooseutils.colorText(traceback, 'GREY') if traceback else '' return '\n{}\n{}{}{}\n'.format(title, filename, src, trace)
def process(self, parent, token): """ Convert the AST defined in the token input into a output node of parent. Inputs: ast[tree.token]: The AST to convert. parent[tree.base.NodeBase]: A tree object that the AST shall be converted to. """ try: func = self.__getFunction(token) el = func(token, parent) if func else parent except Exception as e: #pylint: disable=broad-except el = None title = u'ERROR:{}'.format(e.message) if self.translator.current: filename = mooseutils.colorText('{}:{}\n'.format(self.translator.current.source, token.info.line), 'RESET') else: filename = '' box = mooseutils.colorText(MooseDocs.common.box(token.info[0], line=token.info.line, width=100), 'LIGHT_CYAN') if MooseDocs.LOG_LEVEL == logging.DEBUG: trace = mooseutils.colorText(traceback.format_exc(), 'GREY') else: trace = '' msg = u'\n{}\n{}{}\n{}\n'.format(title, filename, box, trace) LOG.error(msg) if el is not None: for child in token.children: self.process(el, child)
def __repr__(self): """ Print the node information. """ if self.is_root: return 'ROOT' msg = '' color = self.color if self.removed: color = 'GREY' elif self.test: color = 'BLUE' elif not self.hidden: color = 'LIGHT_' + self.color msg0 = '{}: {}'.format(self.name, self.fullpath()) msg1 = 'hidden={} removed={} group={} groups={} test={} alias={}'.format(self.hidden, self.removed, self.group, self.groups(), self.test, self.alias) msg0 = mooseutils.colorText(msg0, color) msg1 = mooseutils.colorText(msg1, 'GREY' if self.removed else 'LIGHT_GREY') return '{} {}'.format(msg0, msg1)
def sqa_check_requirement_duplicates( working_dir=os.getcwd(), specs=['tests'], skip=[]): """Check that no duplicate requirements exist.""" requirements = collections.defaultdict(list) for root, dirs, files in os.walk(working_dir): for fname in files: filename = os.path.join(root, fname) if fname in specs and not any(s in filename for s in skip): node = hit_load(filename) for child in node.children[0]: req = child.get('requirement', None) if req is not None: requirements[req.strip()].append( (filename, child.fullpath, child.line('requirement'))) count = 0 for key, value in requirements.iteritems(): if len(value) > 1: if count == 0: print colorText('Duplicate Requirements Found:\n', 'YELLOW') count += 1 if len(key) > 80: print colorText('{}...'.format(key[:80]), 'YELLOW') for filename, path, line in value: print ' {}:{}'.format(filename, line) return count
def processResults(self, moose_dir, options, output): FileTester.processResults(self, moose_dir, options, output) if self.isFail() or self.specs['skip_checks']: return output # Don't Run CSVDiff on Scaled Tests if options.scaling and self.specs['scale_refine']: return output output = "" # Make sure that all of the CSVDiff files are actually available for file in self.specs['csvdiff']: if not os.path.exists(os.path.join(self.getTestDir(), self.specs['gold_dir'], file)): output += "File Not Found: " + os.path.join(self.getTestDir(), self.specs['gold_dir'], file) self.setStatus(self.fail, 'MISSING GOLD FILE') break if not self.isFail(): output += "\n" fmt = "{{:{}s}} | {{:20s}} | {{:20s}}\n".format(self.file_name_len) output += fmt.format("file", "computed", "requested") output += "-" * (self.file_name_len + 46) + "\n" ok = True for file in self.specs['csvdiff']: gold_file = os.path.join(self.getTestDir(), self.specs['gold_dir'], file) out_file = os.path.join(self.getTestDir(), file) (mean, std) = diff_files(gold_file, out_file, self.specs['err_type']) computed = "" if mean > self.specs['mean_limit']: clr = 'RED' ok = False else: clr = 'GREEN' computed += colorText("{:.2f}".format(mean), clr, html=False, colored=options.colored, code=options.code) computed += " \u00B1 " if std > self.specs['std_limit']: clr = 'RED' ok = False else: clr = 'GREEN' computed += colorText("{:.2f}".format(std), clr, html=False, colored=options.colored, code=options.code) requested = "{:.2f} \u00B1 {:.2f}".format(self.specs['mean_limit'], self.specs['std_limit']) if options.colored: # need to account for the color characters in the second column fmt = "{{:{}s}} | {{:38s}} | {{:20s}}\n".format(self.file_name_len) else: fmt = "{{:{}s}} | {{:20s}} | {{:20s}}\n".format(self.file_name_len) output += fmt.format(file, computed, requested) if not ok: self.setStatus(self.diff, 'DIFF') return output
def _colorTextByMode(self, text, mode): if mode == logging.ERROR or mode == logging.CRITICAL: return mooseutils.colorText(text, 'LIGHT_RED', colored=self.color_text) elif mode == logging.WARNING: return mooseutils.colorText(text, 'LIGHT_YELLOW', colored=self.color_text) return text
def workshop(): # List of input files to compile into a single presentation files = [ 'input/cover.i', 'input/overview.i', 'input/example01.i', 'input/moose_object.i', 'input/input_parameters.i', 'input/kernels.i', 'input/example02.i', 'input/mesh.i', 'input/outputs.i', 'input/bcs.i', 'input/adapt.i', 'input/coupling.i', 'input/example03.i', 'input/materials.i', 'input/auxkernels.i', 'input/auxvariables.i', 'input/functions.i', 'input/postprocessors.i', 'input/executioners.i', 'input/time_steppers.i', 'input/ics.i', 'input/multiapps.i', 'input/transfers.i', 'input/mesh_modifiers.i', 'input/mooseapp.i', #'input/code_verification.i', #'input/preconditioning.i', #'input/debugging.i', #'input/vis_tools.i', #'input/testing.i', #'input/mesh_modifiers.i', 'input/oversampling.i', 'input/random.i', 'input/action.i', 'input/dirac.i', 'input/scalar_kernels.i', 'input/geom_search.i', 'input/dampers.i', 'input/dg.i', 'input/user_objects.i', 'input/restart.i' ] # Create the presentation containing the entire moose workshop print mooseutils.colorText('Building MOOSE Workshop', 'MAGENTA') merger = base.PresentationMerger( 'moose.i', files, style='inl', title="TMS2016 MOOSE/Phase-field Workshop") merger.write()
def _colorTestInfo(req, filename, name, line): """Helper for creating first line of message with file:test:line information""" filename = filename or req.filename name = mooseutils.colorText(name or req.name, 'MAGENTA', colored=RequirementLogHelper.COLOR_TEXT) line = mooseutils.colorText( str(line if (line is not None) else req.line), 'CYAN', colored=RequirementLogHelper.COLOR_TEXT) return '{}:{}:{}\n'.format(filename, name, line)
def report(self, current): title = 'ERROR: {}'.format(self.message) filename = '' if current: source = current.source filename = mooseutils.colorText('{}:{}\n'.format(source, self.info.line), 'RESET') box = mooseutils.colorText(common.box(self.info[0], line=self.info.line, width=100), 'LIGHT_CYAN') return u'\n{}\n{}{}\n'.format(title, filename, box)
def report(self, current): title = 'ERROR: {}'.format(self.message) filename = '' if current: source = current.source filename = mooseutils.colorText('{}:{}\n'.format(source, self.info.line), 'RESET') box = mooseutils.colorText(common.box(self.info[0], line=self.info.line, width=100), 'LIGHT_CYAN') return u'\n{}\n{}{}\n'.format(title, filename, box)
def _getStatusText(self, status): """Helper for returning a string version of the report status""" if status == SQAReport.Status.ERROR: text = mooseutils.colorText('FAIL', 'LIGHT_RED', colored=self.color_text) elif status == SQAReport.Status.WARNING: text = mooseutils.colorText('WARNING', 'LIGHT_YELLOW', colored=self.color_text) else: text = mooseutils.colorText('OK', 'LIGHT_GREEN', colored=self.color_text) return text
def box(content, title=None, line=None, width=None, color='RESET'): """Tool for building unicode box around text, this is used for error reporting.""" lines = content.split('\n') n_lines = len(max(lines, key=len)) out = '' if title: out += title + '\n' if line is not None: num_digits = len(str(line + len(lines))) if width: n_lines = max([width - num_digits - 2, n_lines]) out += '{0:>{1}}{2}{3}{4}'.format(' ', num_digits, '\u250C', '\u2500' * n_lines, '\u2510') for i, x in enumerate(lines): out += '\n{0:>{1}}{2}{3:<{4}}{2}'.format(line + i, num_digits, '\u2502', x, n_lines) out += '\n{0:>{1}}{2}{3}{4}'.format(' ', num_digits, '\u2514', '\u2500' * n_lines, '\u2518') else: if width: n_lines = max([width - 2, n_lines]) out += '{}{}{}'.format('\u250C', '\u2500' * n_lines, '\u2510') for i, x in enumerate(lines): out += '\n{0}{1:<{2}}{0}'.format('\u2502', x, n_lines) out += '\n{}{}{}'.format('\u2514', '\u2500' * n_lines, '\u2518') if color is None: return out return mooseutils.colorText(out, color)
def __repr__(self): """ Print the node name. """ oname = self.__class__.__name__[:-4] msg = '{}: {}'.format(oname, self.full_name) return mooseutils.colorText(msg, self.COLOR)
def box(content, title=None, line=None, width=None, color='RESET'): """Tool for building unicode box around text, this is used for error reporting.""" lines = content.split('\n') n_lines = len(max(lines, key=len)) out = '' if title: out += title + '\n' if line is not None: num_digits = len(str(line + len(lines))) if width: n_lines = max([width - num_digits - 2, n_lines]) out += u'{0:>{1}}{2}{3}{4}'.format(' ', num_digits, u'\u250C', u'\u2500'*n_lines, u'\u2510') for i, x in enumerate(lines): out += u'\n{0:>{1}}{2}{3:<{4}}{2}'.format(line+i, num_digits, u'\u2502', x, n_lines) out += u'\n{0:>{1}}{2}{3}{4}'.format(' ', num_digits, u'\u2514', u'\u2500'*n_lines, u'\u2518') else: if width: n_lines = max([width - 2, n_lines]) out += u'{}{}{}'.format(u'\u250C', u'\u2500'*n_lines, u'\u2510') for i, x in enumerate(lines): out += u'\n{0}{1:<{2}}{0}'.format(u'\u2502', x, n_lines) out += u'\n{}{}{}'.format(u'\u2514', u'\u2500'*n_lines, u'\u2518') if color is None: return out return mooseutils.colorText(out, color)
def __repr__(self): """ Print the node name. """ oname = self.__class__.__name__[:-4] msg = '{}: {}'.format(oname, self.full_name) return mooseutils.colorText(msg, self.COLOR)
def compare(out_fname, out_dir, gold_dir, update_gold=False): """Compare supplied file with gold counter part.""" errno = 0 gold_fname = out_fname.replace(out_dir, gold_dir) # Read the content to be tested out_content = MooseDocs.common.read(out_fname) out_content = prepare_content(out_content) # Update gold content if update_gold: update_gold_helper(gold_fname, out_content) if not os.path.exists(gold_fname): # The verify command is going away because work is being done to move all testing to be # unittest based; but the test pages are still useful for development and will be used to # make sure the various executioner types achieve the same results. # # To allow test pages to exist without testing assume that if a gold doesn't exist then # it should not be tested. pass else: with open(gold_fname, 'r') as fid: gold_content = fid.read() gold_content = bs4.BeautifulSoup(gold_content, 'lxml').prettify() out_content = bs4.BeautifulSoup(out_content, 'lxml').prettify() diff = mooseutils.text_unidiff(out_content, gold_content, out_fname=out_fname, gold_fname=gold_fname, color=True, num_lines=2) if diff: print( mooseutils.colorText( "DIFF: {} != {}".format(out_fname, gold_fname), 'YELLOW')) print(str(diff)) errno = 1 else: print( mooseutils.colorText( "PASS: {} == {}".format(out_fname, gold_fname), 'GREEN')) return errno
def markdown(self): # Extract all the slide set content output = [] print colorText('\nRetrieving Markdown', 'CYAN') for obj in self.objects: md = obj.markdown() if not md: print 'Warning: The slide set, ' + obj.name( ) + ', does not contain content.' else: output.append(md) if self.format == 'remark': return '\n\n---\n\n'.join(output) elif self.format == 'reveal': return '\n\n'.join(output)
def workshop(**kwargs): # List of input files to compile into a single presentation files = ['input/cover.i', 'input/overview.i', 'input/problem.i', 'input/problem_details.i', 'input/tutorial_summary.i', 'input/step01.i', 'input/fem.i', 'input/step02.i', # Mesh, Outputs, MooseObject, validParams, MOOSE Types, Kernels 'input/laplace_young.i', 'input/step03.i', # Materials 'input/step04.i', # Aux Variables and Aux Kernels 'input/step05.i', # Executioners, TimeSteppers, BoundaryConditions 'input/step06.i', # Coupling, Functions 'input/step07.i', # Adaptivity 'input/step08.i', # Postprocessors 'input/step09.i', # Solid Mechanics 'input/step10.i', # MultiApps, Transfers, InitialConditions 'input/mooseapp.i', 'input/code_verification.i', 'input/preconditioning.i', 'input/debugging.i', # 'input/vis_tools.i', # We don't really need this, we talk about all of this throughout 'input/testing.i', 'input/ad_kernels.i', 'input/mesh_modifiers.i', 'input/oversampling.i', 'input/random.i', 'input/action.i', 'input/dirac.i', 'input/scalar_kernels.i', 'input/geom_search.i', 'input/dampers.i', 'input/dg.i', 'input/user_objects.i', 'input/restart.i', 'input/controls.i' ] # Create the presentation containing the entire moose workshop print mooseutils.colorText('Building MOOSE Workshop', 'MAGENTA') merger = blaster.base.PresentationMerger('moose.i', files, style='inl', title='MOOSE Workshop', format=kwargs.pop('format','remark')) merger.write()
def toString(self, prefix='', level=0): """Create a string of Parameter information.""" from .InputParameters import InputParameters is_sub_option = (self.__vtype is not None) and ( InputParameters in self.__vtype) and (self.__value is not None) out = [mooseutils.colorText(self.__name, 'LIGHT_YELLOW')] if prefix is not None: out[0] = '{} | {}{}'.format(out[0], prefix, self.__name) if prefix else out[0] if self.__doc is not None: wrapper = textwrap.TextWrapper() wrapper.initial_indent = ' ' * 2 wrapper.subsequent_indent = ' ' * 2 wrapper.width = 100 out += [ mooseutils.colorText(w, 'GREY') for w in wrapper.wrap(self.__doc) ] if is_sub_option: out += [ self.__value.toString(prefix=self.__name + '_', level=level + 1) ] else: out += [' Value: {}'.format(repr(self.value))] if self.__default is not None: out += [' Default: {}'.format(repr(self.__default))] if self.__vtype is not None: out += [ ' Type(s): {}'.format( tuple([t.__name__ for t in self.__vtype])) ] if self.__allow is not None: wrapper = textwrap.TextWrapper() wrapper.initial_indent = ' Allow: ' wrapper.subsequent_indent = ' ' * len(wrapper.initial_indent) wrapper.width = 100 - len(wrapper.initial_indent) out += wrapper.wrap(repr(self.__allow)) return textwrap.indent('\n'.join(out), ' ' * 2 * level)
def format(self, record): """Format the supplied logging record and count the occurrences.""" msg = logging.Formatter.format(self, record) msg = mooseutils.colorText(u'{}: {}'.format(record.name, msg), self.COLOR[record.levelname]) with self.COUNTS[record.levelname].get_lock(): self.COUNTS[record.levelname].value += 1 return msg
def __repr__(self): """ Print the node name. """ oname = self.__class__.__name__[:-4] msg = '{}: {} hidden={} groups={}'.format(oname, str(self.full_name), self.hidden, self.groups.keys()) return mooseutils.colorText(msg, self.COLOR)
def format(self, record): """Format the supplied logging record and count the occurrences.""" tid = multiprocessing.current_process().name msg = u'{} ({}): {}'.format(record.name, tid, logging.Formatter.format(self, record)) with self.COUNTS[record.levelname].get_lock(): self.COUNTS[record.levelname].value += 1 return mooseutils.colorText(msg, self.COLOR[record.levelname])
def getReport(self): """Generate the report.""" # Execute the function to report about, getting the LogHelper in return self.logger = self.execute(**self.attributes) # Return the logging records from the SilentReportHandler records = self._handler.getRecords() self._handler.clearRecords() # Update status from logging if len(records[logging.WARNING]) > 0: self.status = 1 if len(records[logging.ERROR]) > 0 or len( records[logging.CRITICAL]) > 0: self.status = 2 # Determine the text length for the LogHelper keys width = 0 for k in self.logger.modes.keys(): width = max(width, len(k)) # Start the report text text = '{} {}\n'.format( mooseutils.colorText(self.title, 'CYAN', colored=self.color_text), self._getStatusText(self.status)) # Report the error counts for key, mode in self.logger.modes.items(): cnt = self.logger.counts[key] msg = '{:>{width}}: {}\n'.format(key, cnt, width=width + 2) text += self._colorTextByMode(msg, mode) if cnt > 0 else msg text += '\n' # Add logged items to the report, if desired n = self.number_of_newlines_after_log if len(records[logging.CRITICAL]) > 0 and self.show_critical: text += self._colorTextByStatus('CRITICAL:\n', SQAReport.Status.ERROR) for record in records[logging.CRITICAL]: text += record.getMessage() + '\n' * n if len(records[logging.ERROR]) > 0 and self.show_error: text += self._colorTextByStatus('ERRORS:\n', SQAReport.Status.ERROR) for record in records[logging.ERROR]: text += record.getMessage() + '\n' * n if (len(records[logging.WARNING]) > 0) and self.show_warning: text += self._colorTextByStatus('WARNINGS:\n', SQAReport.Status.WARNING) for record in records[logging.WARNING]: text += record.getMessage() + '\n' * n return text.strip('\n')
def format(self, record): """Format the supplied logging record and count the occurrences.""" msg = logging.Formatter.format(self, record) msg = mooseutils.colorText(u'{}: {}'.format(record.name, msg), self.COLOR[record.levelname]) with self.COUNTS[record.levelname].get_lock(): self.COUNTS[record.levelname].value += 1 return msg
def __str__(self): """ Overload the str function so that information is printed when print is called on the object. """ self.checkUpdateState() # The string to return out = '' # Variables variables = [(ExodusReader.NODAL, 'NODAL VARIABLES'), (ExodusReader.ELEMENTAL, 'ELEMENTAL VARIABLES'), (ExodusReader.GLOBAL, 'POSTPROCESSORS')] for vartype, vartitle in variables: out += '\n{}:\n'.format(mooseutils.colorText(vartitle, 'GREEN')) for varinfo in self.getVariableInformation([vartype]).itervalues(): out += ' {}\n'.format(mooseutils.colorText(varinfo.name, 'CYAN')) out += '{:>16}: {}\n'.format('components', varinfo.num_components) return out
def __repr__(self): """ Print the node name. """ oname = self.__class__.__name__[:-4] msg = '{}: {} hidden={} groups={}'.format(oname, str(self.full_name), self.hidden, self.groups.keys()) return mooseutils.colorText(msg, self.COLOR)
def __str__(self): """ Overload the str function so that information is printed when print is called on the object. """ self.checkUpdateState() # The string to return out = '' # Variables variables = [(ExodusReader.NODAL, 'NODAL VARIABLES'), (ExodusReader.ELEMENTAL, 'ELEMENTAL VARIABLES'), (ExodusReader.GLOBAL, 'POSTPROCESSORS')] for vartype, vartitle in variables: out += '\n{}:\n'.format(mooseutils.colorText(vartitle, 'GREEN')) for varinfo in self.getVariableInformation([vartype]).itervalues(): out += ' {}\n'.format(mooseutils.colorText(varinfo.name, 'CYAN')) out += '{:>16}: {}\n'.format('components', varinfo.num_components) return out
def _colorTextByStatus(self, text, status): """Helper for coloring text based on status.""" if not isinstance(text, str): text = str(text) color = 'LIGHT_GREEN' if status == SQAReport.Status.ERROR: color = 'LIGHT_RED' elif status == SQAReport.Status.WARNING: color = 'LIGHT_YELLOW' return mooseutils.colorText(text, color, colored=self.color_text)
def format(self, record): msg = logging.Formatter.format(self, record) if record.levelname in self.COLOR: msg = mooseutils.colorText(msg, self.COLOR[record.levelname]) if record.levelname in self.COUNTS: with self.COUNTS[record.levelname].get_lock(): self.COUNTS[record.levelname].value += 1 return msg
def format(self, record): """Format the supplied logging record and count the occurrences.""" tid = multiprocessing.current_process().name msg = u'{} ({}): {}'.format(record.name, tid, logging.Formatter.format(self, record)) with self.COUNTS[record.levelname].get_lock(): self.COUNTS[record.levelname].value += 1 return mooseutils.colorText(msg, self.COLOR[record.levelname])
def __contents(self): # Initialize the table-of-contents slides for obj in self.objects: obj.initContents() # Initial slide index idx = 1 # Loop through each object and slide and set the slide index print colorText('\nGenerating contents:', 'CYAN') for obj in self.objects: for slide in obj.warehouse().activeObjects(): print ' ' + slide.name() slide.number = idx idx += 1 + len(re.findall('\n--', slide.markdown)) # Call the contents object on each slide set for obj in self.objects: obj.contents()
def callFunction(obj, name, args=tuple()): """Helper for calling a function on the supplied object.""" func = getattr(obj, name, None) if func is not None: try: func(*args) except Exception: msg = "Failed to execute '{}' method within the '{}' object.\n" \ .format(name, obj.__class__.__name__) msg += mooseutils.colorText(traceback.format_exc(), 'GREY') LOG.critical(msg)
def format(self, record): msg = logging.Formatter.format(self, record) if record.levelname in self.COLOR: msg = mooseutils.colorText(msg, self.COLOR[record.levelname]) if record.levelname in self.COUNTS: with self.COUNTS[record.levelname].get_lock(): self.COUNTS[record.levelname].value += 1 self.MESSAGES[record.levelname].append(msg) return msg
def _print_reports(title, reports, status): """Helper for printing SQAReport objects and propagating status""" if reports: print(mooseutils.colorText( '\n{0}\n{1} REPORT(S):\n{0}\n'.format('-' * 80, title.upper()), 'MAGENTA'), end='', flush=True) for report in reports: print(report.getReport(), '\n') status = report.status if status < report.status else status return status
def __str__(self): """Display requirement stats.""" out = [] out.append(colorText(self.title, 'LIGHT_YELLOW')) out.append(' Complete: {:2.1f}%'.format(self.complete*100)) out.append(' Total Number of Files: {}'.format(self.files)) out.append(' Files with Requirements: {}'.format(self.files_with_requirement)) out.append(' Total Number of Tests: {}'.format(self.tests)) out.append(' Tests with Requirements: {}'.format(self.tests_with_requirement)) out.append(' Deprecated Tests: {}'.format(self.tests_deprecated)) out.append(' Tests Remaining: {}'.format(self.tests - self.tests_deprecated - self.tests_with_requirement)) return '\n'.join(out)
def execute(self): # Number of slide set objects n = len(self.objects) # Read and build the slides for i in range(n): # Display the current object being executed obj = self.objects[i] name = obj.name() msg = ['Building Set:', name, '(' + str(i + 1), 'of', str(n) + ')'] print colorText(' '.join(msg), 'CYAN') # Read and build the content print ' Reading content...' raw = obj.read() print ' Building content...' obj.build(raw) # Build the table-of-contents self.__contents()
def report_error(message, filename, line=None, src='', traceback=None, prefix=u'ERROR'): """ Helper for reporting error to logging module. Inputs: message[str]: The message to report, ERROR is automatically appended page[pages.Page]: The page that created the error info[Information]: The lexer information object traceback: The traceback (should be a string from traceback.format_exc()) """ title = '{}: {}'.format(prefix, message) if src: src = mooseutils.colorText(box(src, line=line, width=100), 'LIGHT_CYAN') if line is not None: filename = mooseutils.colorText('{}:{}\n'.format(filename, line), 'RESET') else: filename = mooseutils.colorText('{}\n'.format(filename), 'RESET') trace = u'\n' + mooseutils.colorText(traceback, 'GREY') if traceback else '' return u'\n{}\n{}{}{}\n'.format(title, filename, src, trace)
def formatResult(tester_data, result, options, color=True): tester = tester_data.getTester() timing = tester_data.getTiming() f_result = '' caveats = '' first_directory = tester.specs['first_directory'] test_name = tester.getTestName() status = tester.getStatus() cnt = (TERM_COLS - 2) - len(test_name + result) color_opts = {'code': options.code, 'colored': options.colored} if color: if options.color_first_directory: test_name = colorText( first_directory, 'CYAN', **color_opts) + test_name.replace( first_directory, '', 1) # Strip out first occurence only # Color the Caveats CYAN m = re.search(r'(\[.*?\])', result) if m: caveats = m.group(1) f_result = colorText(caveats, 'CYAN', **color_opts) # Color test results based on status. # Keep any caveats that may have been colored if status: f_result += colorText(result.replace(caveats, ''), tester.getColor(), **color_opts) f_result = test_name + '.' * cnt + ' ' + f_result else: f_result = test_name + '.' * cnt + ' ' + result # Tack on the timing if it exists if options.timing: f_result += ' [' + '%0.3f' % float(timing) + 's]' if options.debug_harness: f_result += ' Start: ' + '%0.3f' % tester_data.getStartTime( ) + ' End: ' + '%0.3f' % tester_data.getEndTime() return f_result
def _callFunction(obj, name, page, args=tuple()): """Helper for calling a function on the supplied object.""" func = getattr(obj, name, None) if func is not None: try: func(*args) except Exception: msg = "Failed to execute '{}' method within the '{}' object.\n" \ .format(name, obj.__class__.__name__) if page is not None: msg += " Error occurred in {}\n".format(page.local) msg += mooseutils.colorText(traceback.format_exc(), 'GREY') LOG.critical(msg)#, name, obj.__class__.__name__)
def print_reports(title, reports, status): print(colorText( '\n{0}\n{1} REPORT(S):\n{0}\n'.format('-' * 80, title.upper()), 'MAGENTA'), end='', flush=True) if reports: for report in reports: print(report.getReport(), '\n') status = report.status if status < report.status else status else: print('No results') return status
def _reportLatexWarnings(self, lnode, content): """Helper to display latex warnings.""" # Locate the Page object where the error was producec. pnode = None for page in content: if lnode.filename in page.destination: pnode = page break # Get the rendered result tree and locate the start/end lines for each node result = None if pnode is not None: result = self.translator.getResultTree(pnode) result['_start_line'] = 1 self._lineCounter(result) # Report warning(s) for w in lnode.warnings: # Locate the rendered node that that caused the error r_node = None if result: for r in anytree.PreOrderIter(result): if w.line >= r.get('_start_line', float('Inf')): r_node = r # Build message msg = '\n' msg += mooseutils.colorText('pdfLaTeX Warning: {}\n'.format(w.content), 'LIGHT_YELLOW') if r_node: msg += box(r_node.write(), title='IN: {}:{}'.format(pnode.destination, w.line), width=100, color='GREY') msg += '\n' info = r_node.get('info', None) if info is not None: msg += box(info[0], title='FROM: {}:{}'.format(pnode.source, info.line), color='GREY') LOG.warning(msg)
def compare(out_fname, out_dir, gold_dir, update_gold=False): """Compare supplied file with gold counter part.""" errno = 0 gold_fname = out_fname.replace(out_dir, gold_dir) # Read the content to be tested with open(out_fname, 'r') as fid: out_content = insert_moose_dir(replace_uuid4(fid.read())) # Update gold content if update_gold: update_gold_helper(gold_fname, out_content) if not os.path.exists(gold_fname): print mooseutils.colorText('MISSING GOLD: {}'.format(gold_fname), 'RED') errno = 1 else: with open(gold_fname, 'r') as fid: gold_content = fid.read() gold_content = bs4.BeautifulSoup(gold_content, 'lxml').prettify() out_content = bs4.BeautifulSoup(out_content, 'lxml').prettify() diff = mooseutils.text_unidiff(out_content, gold_content, out_fname=out_fname, gold_fname=gold_fname, color=True, num_lines=2) if diff: print mooseutils.colorText("DIFF: {} != {}".format(out_fname, gold_fname), 'YELLOW') print diff errno = 1 else: print mooseutils.colorText("PASS: {} == {}".format(out_fname, gold_fname), 'GREEN') return errno
def mooseMessage(*args, **kwargs): """ A generic message function. Args: args[tuple]: Comma separated items to be printed, non strings are converted with 'repr'. Kwargs: error[bool]: (Default: False) When True and 'dialog=True' the the "Critical" icon is included with the message. warning[bool]: (Default: False) When True and 'dialog=True' the the "Critical" icon is included with the message. traceback[bool]: (Default: False) When True the stack trace is printed with the message. dialog[bool]: (Default: False) When true a QDialog object is created, the error will still print to the console as well. color[str]: (Default: None) Add the bash color string to the message (see colorText). debug[bool]: (Default: False) Print the message only if tools.MOOSE_DEBUG_MODE = True. test[bool]: FOR TESTING ONLY! (Default: False) When True the QDialog is not executed, just returned. indent[int]: Number of levels to indent (2 spaces are applied to each level) """ # Grab the options error = kwargs.pop('error', False) warning = kwargs.pop('warning', False) trace = kwargs.pop('traceback', False) dialog = kwargs.pop('dialog', False) color = kwargs.pop('color', None) test = kwargs.pop('test', False) indent = kwargs.pop('indent', 0) # Build the message message = [] for arg in args: if not isinstance(arg, str): message.append(repr(arg)) else: message.append(arg) message = '{}{}'.format(' '*2*indent, ' '.join(message)) # Show a dialog box if MOOSE_USE_QT5 and dialog and not MOOSE_TESTING_MODE: box = QtWidgets.QMessageBox() box.setText(message) if warning: box.setIcon(QtWidgets.QMessageBox.Warning) elif error: box.setIcon(QtWidgets.QMessageBox.Critical) if test: return box box.exec_() # Emit the message to any listeners messageEmitter.write(message, color) # Print the message to screen if color: message = colorText(message, color) print message # Show the traceback if trace: traceback.print_stack() stack = ''.join(traceback.format_stack()) messageEmitter.write(stack, color)
def __repr__(self): """ Prints the name of the token, this works in union with __str__ to print the tree structure of any given node. """ return mooseutils.colorText(self.console(), self.COLOR)
def __str__(self): """Define the anytree screen output.""" return '{}: {}, {}'.format(mooseutils.colorText(self.__class__.__name__, self.COLOR), self.local, self.source)
def formatResult(tester_data, result, options, color=True): # Support only one instance of a format identifier, but obey the order terminal_format = list(OrderedDict.fromkeys(list(TERM_FORMAT))) tester = tester_data.getTester() status = tester.getStatus() color_opts = {'code' : options.code, 'colored' : options.colored} # container for every printable item formatted_results = dict.fromkeys(terminal_format) # Populate formatted_results for those we support, with requested items # specified by the user. Caveats and justifications are parsed outside of # loop as these two items change based on character count consumed by others. caveat_index = None justification_index = None for i, f_key in enumerate(terminal_format): # Store the caveat request. We will use this later. if str(f_key).lower() == 'c': caveat_index = terminal_format[i] # Store the justification request. We will use this later. if str(f_key).lower() == 'j': justification_index = terminal_format[i] if str(f_key).lower() == 'p': pre_result = ' '*(8-len(status.status)) + status.status formatCase(f_key, (pre_result, status.color), formatted_results) if str(f_key).lower() == 's': if not result: result = str(tester.getStatusMessage()) # refrain from printing a duplicate pre_result if it will match result if 'p' in [x.lower() for x in terminal_format] and result == status.status: formatCase(f_key, None, formatted_results) else: formatCase(f_key, (result, status.color), formatted_results) if str(f_key).lower() == 'n': formatCase(f_key, (tester.getTestName(), None), formatted_results) # Adjust the precision of time, so we can justify the length. The higher the # seconds, the lower the decimal point, ie: [0.000s] - [100.0s]. Max: [99999s] if str(f_key).lower() == 't' and options.timing: actual = float(tester_data.getTiming()) int_len = len(str(int(actual))) precision = min(3, max(0,(4-int_len))) f_time = '[' + '{0: <6}'.format('%0.*fs' % (precision, actual)) + ']' formatCase(f_key, (f_time, None), formatted_results) # Decorate Caveats if tester.getCaveats() and caveat_index is not None: caveats = ','.join(tester.getCaveats()) caveat_color = status.color if tester.didPass() or tester.isSkipped(): caveat_color = 'CYAN' f_caveats = '[' + caveats + ']' # +1 space created later by join character_count = resultCharacterCount(formatted_results) + len(f_caveats) + 1 # If caveats are the last items the user wants printed, or -e (extra_info) is # called, allow caveats to consume available character count beyond TERM_COLS. # Else, we trim caveats: if terminal_format[-1].lower() != 'c' \ and not options.extra_info \ and character_count > TERM_COLS: over_by_amount = character_count - TERM_COLS f_caveats = '[' + caveats[:len(caveats) - (over_by_amount + 3)] + '...]' formatCase(caveat_index, (f_caveats, caveat_color), formatted_results) # Fill the available space left, with dots if justification_index is not None: j_dot = None # +1 space created later by join character_count = resultCharacterCount(formatted_results) + 1 if character_count < TERM_COLS: j_dot = ('.'*max(0, (TERM_COLS - character_count)), 'GREY') elif character_count == TERM_COLS: j_dot = ('', 'GREY') formatCase(justification_index, j_dot, formatted_results) # If color, decorate those items which support it if color: for format_rule, printable in formatted_results.iteritems(): if printable and (printable[0] and printable[1]): formatted_results[format_rule] = (colorText(printable[0], printable[1], **color_opts), printable[1]) # Do special coloring for first directory if format_rule == 'n' and options.color_first_directory: formatted_results[format_rule] = (colorText(tester.specs['first_directory'], 'CYAN', **color_opts) +\ formatted_results[format_rule][0].replace(tester.specs['first_directory'], '', 1), 'CYAN') # Strip out first occurence only # join printable results in the order in which the user asked final_results = ' '.join([formatted_results[x][0] for x in terminal_format if formatted_results[x]]) # Decorate debuging if options.debug_harness: final_results += ' Start: ' + '%0.3f' % tester_data.getStartTime() + ' End: ' + '%0.3f' % tester_data.getEndTime() return final_results
def report_exception(msg, *args): """Helper to output exceptions in logs.""" msg = msg.format(*args) msg += u'\n{}\n'.format(mooseutils.colorText(traceback.format_exc(), 'GREY')) return msg
def report(self, current): out = ErrorToken.report(self, current) trace = mooseutils.colorText(self.traceback, 'GREY') return u'{}\n{}'.format(out, trace)