def table(header, rows): if not HAVE_TERMTAB: print_error("Missing dependency, install terminaltables (`pip install terminaltables`)") return # TODO: Refactor this function, it is some serious ugly code. content = [header] + rows # Make sure everything is string try: content = [[a.replace('\t', ' ') for a in list(map(unicode, l))] for l in content] except: # Python3 way of doing it: content = [[a.replace('\t', ' ') for a in list(map(str, l))] for l in content] t = AsciiTable(content) if not t.ok: longest_col = t.column_widths.index(max(t.column_widths)) max_length_col = t.column_max_width(longest_col) if max_length_col > 0: for i, content in enumerate(t.table_data): if len(content[longest_col]) > max_length_col: temp = '' for l in content[longest_col].splitlines(): if len(l) > max_length_col: temp += '\n'.join(textwrap.wrap(l, max_length_col)) + '\n' else: temp += l + '\n' content[longest_col] = temp.strip() t.table_data[i] = content return t.table
def terminal_printer(results): any_message = any(bool(r.message) for r in results) headers = ['#', 'Time', 'Test name'] if any_message: headers.append('Description') table_data = [headers] for i, result in enumerate(results, 1): text_color = { 0: Colors.OK, 1: Colors.WARNING, 2: Colors.FAIL, }[result.status] data_row = [ i, f'{text_color}{result.duration}ms{Colors.ENDC}', f'{text_color}{result.title}{Colors.ENDC}', ] if any_message: data_row.append(result.message or '') table_data.append(data_row) table = AsciiTable(table_data) if any_message: message_size = table.column_max_width(2) for column in table.table_data: column[2] = '\n'.join(wrap(column[2], message_size)) print(table.table)
def display_results(results, save, out=None): table_data = [] table_data.append(['Issue ID', 'Description', 'Comments']) table = AsciiTable(table_data) max_width = table.column_max_width(1) align_width = int(max_width / 2) for result in results: description = results[result]["description"] comments = results[result]["comments"] if not description and not comments: continue if not description: description = "--" if not comments: comments = "--" if len(str(description)) > align_width: description = '\n'.join(wrap(str(description), align_width)) if len(str(comments)) > align_width: comments = '\n'.join(wrap(str(comments), align_width)) table.table_data.append([result, description, comments]) table.inner_row_border = True print(table.table) print("[+] Returned " + str(len(table.table_data) - 1) + " items\n") if save: output = "\n[+] Jecretz Results\n\n" + table.table + "\n\n[+] Returned " + str( len(table.table_data) - 1) + " items\n\n" with open(out, "w") as file: file.write(output)
def table(header, rows): if not HAVE_TERMTAB: self.log( 'error', "Missing dependency, install terminaltables (`pip install terminaltables`)" ) return content = [header] + rows # Make sure everything is string try: content = [[a.replace('\t', ' ') for a in list(map(unicode, l))] for l in content] except: # Python3 way of doing it: content = [[a.replace('\t', ' ') for a in list(map(str, l))] for l in content] t = AsciiTable(content) if not t.ok: longest_col = t.column_widths.index(max(t.column_widths)) max_length_col = t.column_max_width(longest_col) if max_length_col > 0: for i, content in enumerate(t.table_data): if len(content[longest_col]) > max_length_col: temp = '' for l in content[longest_col].splitlines(): if len(l) > max_length_col: temp += '\n'.join(textwrap.wrap( l, max_length_col)) + '\n' else: temp += l + '\n' content[longest_col] = temp.strip() t.table_data[i] = content return t.table
def do_search(self, arg): """Search for modules""" search_table = [['Module', 'Description']] table = AsciiTable(search_table) max_width = table.column_max_width(1) if not arg: self.do_list(None) return if arg in self.modules.keys(): for name, module in self.modules[arg].items(): wrapped = '\n'.join(textwrap.wrap(module.desc, max_width - 40)) table.table_data.append([arg + "/" + name, wrapped]) else: for k, v in self.modules.items(): for name, module in v.items(): if arg.lower() in name.lower(): wrapped = '\n'.join( textwrap.wrap(module.desc, max_width - 40)) table.table_data.append([k + "/" + name, wrapped]) if len(table.table_data) > 1: print(table.table) else: logger.error(f"Nothing found for \"{arg}\".")
def get_music(self, music_id): self.view_capture(int(music_id), 1) url = uapi.music_api.format(music_id, music_id) data = tools.curl(url,self.__headers) music = data['songs'] print("《" + tools.encode(music[0]['name']) + "》") author = [] for a in music[0]['artists']: author.append(tools.encode(a['name'])) album = str(tools.encode(music[0]['album']['name'])) print("演唱:{} 专辑:{}".format(",".join(author), album)) comments = self.session.query(pysql.Comment163).filter( pysql.Comment163.song_id == int(music_id) ) tb = AsciiTable([["序号", "作者", "评论", "点赞"]]) max_width = tb.column_max_width(2) - tb.column_max_width(2) % 3 cnt = 0 try: for cmt in comments: cnt = cnt + 1 au = tools.encode(cmt.author) txt = "" length = 0 for u in cmt.txt: txt = txt + u if ord(u) < 128: length = length + 3 else: length = length + 1 if length == max_width: txt = txt + "\n" length = 0 liked = str(cmt.liked) tb.table_data.append([str(cnt), str(au), str(txt), liked]) print(tb.table) except UnicodeEncodeError: pylog.log.info("获取歌曲详情编码存在问题,转为非表格形式,歌曲ID:{}".format(music_id)) for cmt in comments: print("评论: {}".format(tools.encode(cmt.txt))) print( "作者: {} 点赞: {}".format( tools.encode(cmt.author), str(cmt.liked) ) ) print("") except Exception as e: pylog.print_warn("获取歌曲时出现异常: {} 歌曲ID:{}".format(e, music_id))
def list(self): """ """ online_mode = self.cloudshell_config_reader.read().online_mode.lower() == "true" template_location = self.cloudshell_config_reader.read().template_location try: standards = self.standards.fetch() if online_mode: try: templates = self.template_retriever.get_templates(standards=standards) except SSLError: raise click.UsageError("Could not retrieve the templates list. Are you offline?") else: templates = self.template_retriever.get_templates(template_location=template_location, standards=standards) except FatalError as err: raise click.UsageError(str(err)) except FeatureUnavailable: if online_mode: templates = self.template_retriever.get_templates(alternative=ALTERNATIVE_TEMPLATES_PATH) else: templates = self.template_retriever.get_templates(template_location=template_location) if not templates: raise click.ClickException("No templates matched the view criteria(gen1/gen2) or " "available templates and standards are not compatible") template_rows = [["Template Name", "CloudShell Ver.", "Description"]] for template in list(templates.values()): template = template[0] cs_ver_txt = str(template.min_cs_ver) + " and up" template_rows.append( [template.name, cs_ver_txt, template.description]) # description is later wrapped based on the size of the console table = AsciiTable(template_rows) table.outer_border = False table.inner_column_border = False max_width = table.column_max_width(2) if max_width <= 0: # verify that the console window is not too small, and if so skip the wrapping logic click.echo(table.table) return row = 1 for template in list(templates.values()): template = template[0] wrapped_string = linesep.join(wrap(template.description, max_width)) table.table_data[row][2] = wrapped_string row += 1 output = table.table click.echo(output) if self.show_info_msg: click.echo(""" As of CloudShell 8.0, CloudShell uses 2nd generation shells, to view the list of 1st generation shells use: shellfoundry list --gen1. For more information, please visit our devguide: https://qualisystems.github.io/devguide/""")
def get_music(self, music_id): self.view_capture(int(music_id), 1) url = uapi.music_api.format(music_id, music_id) s = requests.session() s = BeautifulSoup( s.get(url, headers=self.__headers).content, "html.parser") music = json.loads(s.text)['songs'] print("《" + music[0]['name'].encode('utf-8') + "》") author = [] for a in music[0]['artists']: author.append(a['name'].encode('utf-8')) album = str(music[0]['album']['name'].encode('utf-8')) print("演唱:{} 专辑:{}".format(",".join(author), album)) comments = self.session.query( pysql.Comment163).filter(pysql.Comment163.song_id == int(music_id)) tb = AsciiTable([["序号", "作者", "评论", "点赞"]]) max_width = tb.column_max_width(2) - tb.column_max_width(2) % 3 cnt = 0 try: for cmt in comments: cnt = cnt + 1 au = cmt.author.encode("utf-8") txt = "" length = 0 for u in cmt.txt: txt = txt + u if ord(u) < 128: length = length + 3 else: length = length + 1 if length == max_width: txt = txt + "\n" length = 0 liked = str(cmt.liked) tb.table_data.append([str(cnt), str(au), str(txt), liked]) print(tb.table) except UnicodeEncodeError: pylog.log.info("获取歌曲详情编码存在问题,转为非表格形式,歌曲ID:" + str(music_id)) for cmt in comments: print("评论: {}".format(cmt.txt.encode("utf-8"))) print("作者: {} 点赞: {}".format(cmt.author.encode("utf-8"), str(cmt.liked))) print("") except Exception: raise
def output(buf, dowrap=False): bbuf = [["Bot", "Pack#", "Size", "File"]] + buf t = AsciiTable(bbuf) t.inner_column_border = False t.outer_border = False if dowrap and sys.stdout.isatty(): mw = t.column_max_width(3) for e in bbuf: if len(e[3])>mw: e[3] = "\n".join(wrap(e[3], mw)) print(t.table) sys.stdout.flush()
def list_campaigns(options): table = AsciiTable([['ID', 'Results', 'Command', 'Arch', 'Simics']], 'DrSEUs Campaigns') try: campaigns = list(get_campaign('all')) except: print('error connecting to database, try creating a new campaign first') return for campaign in campaigns: results = campaign.result_set.count() items = [] for i, item in enumerate((campaign.id, results, campaign.command, campaign.architecture, campaign.simics)): if not isinstance(item, str): item = str(item) if len(item) < table.column_max_width(i): items.append(item) else: items.append('{}...'.format( item[:table.column_max_width(i)-4])) table.table_data.append(items) print(table.table)
def print_overview_errors(errors): print("Errors: %s" % len(errors)) for error in errors: table_data = [ ['Feature', 'Step', 'Line', 'Error message'], [error.filename, error.name, str(error.line), ''] ] table = AsciiTable(table_data) error_message_max_width = table.column_max_width(3) w = textwrap.TextWrapper(width=error_message_max_width, break_long_words=False,replace_whitespace=False) wrapped_string = '\n'.join(w.wrap(error.error_message)) print(wrapped_string) table.table_data[1][3] = wrapped_string print(table.table)
def PrintThing(ret_cmd): if not options.printer: print("++++++++++++++++++++++++++++++") print("Command ID : " + str(ret_cmd[0])) print("Command : " + str(ret_cmd[1]) + '\n') print("Comment : " + str(ret_cmd[2])) print("Tags : " + str(ret_cmd[5])) print("Date Added : " + str(ret_cmd[3])) print("Added By : " + str(ret_cmd[4])) print("References\n__________\n" + str(ret_cmd[6].replace(',', '\n'))) print("++++++++++++++++++++++++++++++\n") elif options.printer is 'c': print(str(ret_cmd[1])) elif options.printer is 'p': print("++++++++++++++++++++++++++++++") print(str(ret_cmd[1]) + '\n') print(str(ret_cmd[2])) print("++++++++++++++++++++++++++++++\n") elif options.printer is 'd': print(str(ret_cmd[1])) print(str(ret_cmd[2])) print(str(ret_cmd[4])) print('EOC') print(str(ret_cmd[5].replace(',', '\n'))) print('EOT') print(str(ret_cmd[6].replace(',', '\n'))) print('EOR') elif options.printer is 'w': print("= " + str(ret_cmd[2]) + " = ") print(" " + str(ret_cmd[1])) print(str(ret_cmd[5].replace(',', ', '))) print(str(ret_cmd[6].replace(',', '\n'))) elif options.printer is 'P': table_data = [\ ["Added By " + str(ret_cmd[4]), "Cmd ID : " + str(ret_cmd[0])], ["Command ", str(ret_cmd[1])], ["Comment ", str(ret_cmd[2])], ["Tags ", str(ret_cmd[5]).replace(',', '\n')], ["Date added", str(ret_cmd[3])], ["References", str(ret_cmd[6]).replace(',', '\n')]\ ] table = AsciiTable(table_data) max_width = table.column_max_width(1) wrapped_string = '\n'.join(wrap(str(ret_cmd[1]), max_width)) + "\n" table.table_data[1][1] = wrapped_string print(table.table) else: err("Please seek help")
def list(self): try: standards = self.standards.fetch() templates = self.template_retriever.get_templates( standards=standards) except (SSLError, FatalError): raise click.UsageError( 'Could not retrieve the templates list. Are you offline?') except FeatureUnavailable: templates = self.template_retriever.get_templates( alternative=ALTERNATIVE_TEMPLATES_PATH) if not templates: click.echo('No templates matched the criteria') return template_rows = [['Template Name', 'CloudShell Ver.', 'Description']] for template in templates.values(): cs_ver_txt = str(template.min_cs_ver) + " and up" template_rows.append([ template.name, cs_ver_txt, template.description ]) # description is later wrapped based on the size of the console table = AsciiTable(template_rows) table.outer_border = False table.inner_column_border = False max_width = table.column_max_width(2) if max_width <= 0: # verify that the console window is not too small, and if so skip the wrapping logic click.echo(table.table) return row = 1 for template in templates.values(): wrapped_string = linesep.join(wrap(template.description, max_width)) table.table_data[row][2] = wrapped_string row += 1 output = table.table click.echo(output) if self.show_info_msg: click.echo(''' As of CloudShell 8.0, CloudShell uses 2nd generation shells, to view the list of 1st generation shells use: shellfoundry list --gen1. For more information, please visit our devguide: https://qualisystems.github.io/devguide/''' )
def print_overview_errors(errors): print("Errors: %s" % len(errors)) for error in errors: table_data = [['Feature', 'Step', 'Line', 'Error message'], [error.filename, error.name, str(error.line), '']] table = AsciiTable(table_data) error_message_max_width = table.column_max_width(3) w = textwrap.TextWrapper(width=error_message_max_width, break_long_words=False, replace_whitespace=False) wrapped_string = '\n'.join(w.wrap(error.error_message)) print(wrapped_string) table.table_data[1][3] = wrapped_string print(table.table)
def PrintThing(ret_cmd): if not options.printer: print "++++++++++++++++++++++++++++++" print "Command ID : "+str(ret_cmd[0]) print "Command : "+str(ret_cmd[1])+'\n' print "Comment : "+str(ret_cmd[2]) print "Tags : "+str(ret_cmd[5]) print "Date Added : "+str(ret_cmd[3]) print "Added By : "+str(ret_cmd[4]) print "References\n__________\n"+str(ret_cmd[6].replace(',', '\n')) print "++++++++++++++++++++++++++++++\n" elif options.printer is 'p': print "++++++++++++++++++++++++++++++" print str(ret_cmd[1])+'\n' print str(ret_cmd[2]) print "++++++++++++++++++++++++++++++\n" elif options.printer is 'd': print str(ret_cmd[1]) print str(ret_cmd[2]) print str(ret_cmd[4]) print 'EOC' print str(ret_cmd[5].replace(',', '\n')) print 'EOT' print str(ret_cmd[6].replace(',', '\n')) print 'EOR' elif options.printer is 'w': print "= "+str(ret_cmd[2])+" = " print " "+str(ret_cmd[1]) print str(ret_cmd[5].replace(',', ', ')) print str(ret_cmd[6].replace(',', '\n')) elif options.printer is 'P': table_data = [\ ["Added By " + str(ret_cmd[4]), "Cmd ID : " + str(ret_cmd[0])], ["Command ", str(ret_cmd[1])], ["Comment ", str(ret_cmd[2])], ["Tags ", str(ret_cmd[5]).replace(',', '\n')], ["Date added", str(ret_cmd[3])], ["References", str(ret_cmd[6]).replace(',', '\n')]\ ] table = AsciiTable(table_data) max_width = table.column_max_width(1) wrapped_string = '\n'.join(wrap(str(ret_cmd[1]), max_width))+"\n" table.table_data[1][1] = wrapped_string print table.table else: err("Please seek help")
def do_list(self, arg): """List available hardening modules""" modules_table = [['Module', 'Description']] table = AsciiTable(modules_table) max_width = table.column_max_width(1) if self.namespace: for name, module in self.modules[self.namespace].items(): wrapped = '\n'.join(textwrap.wrap(module.desc, max_width - 40)) table.table_data.append([self.namespace + "/" + name, wrapped]) else: for k, v in self.modules.items(): for name, module in v.items(): wrapped = '\n'.join( textwrap.wrap(module.desc, max_width - 40)) table.table_data.append([k + "/" + name, wrapped]) print(table.table)
def table(header, rows): if not HAVE_TERMTAB: print_error( "Missing dependency, install terminaltables (`pip install terminaltables`)" ) return # TODO: Refactor this function, it is some serious ugly code. content = [] for l in [header] + rows: to_append = [] for a in l: if isinstance(a, bytes): if sys.version_info < (3, 4): a = a.decode('utf-8', 'ignore') else: a = a.decode('utf-8', 'backslashreplace') if not isinstance(a, six.text_type): a = six.text_type(a) to_append.append(a.replace('\t', ' ').replace('\v', '\\v')) content.append(to_append) t = AsciiTable(content) if not t.ok: t.inner_row_border = True longest_col = t.column_widths.index(max(t.column_widths)) max_length_col = t.column_max_width(longest_col) if max_length_col > 0: for i, content in enumerate(t.table_data): if len(content[longest_col]) > max_length_col: temp = '' for l in content[longest_col].splitlines(): if len(l) > max_length_col: temp += '\n'.join(textwrap.wrap( l, max_length_col)) + '\n' else: temp += l + '\n' content[longest_col] = temp.strip() t.table_data[i] = content return t.table
def table(header, rows): if not HAVE_TERMTAB: print_error("Missing dependency, install terminaltables (`pip install terminaltables`)") return # TODO: Refactor this function, it is some serious ugly code. content = [] for l in [header] + rows: to_append = [] for a in l: if isinstance(a, bytes): if sys.version_info < (3, 4): a = a.decode('utf-8', 'ignore') else: a = a.decode('utf-8', 'backslashreplace') if not isinstance(a, six.text_type): a = six.text_type(a) to_append.append(a.replace('\t', ' ').replace('\v', '\\v')) content.append(to_append) t = AsciiTable(content) if not t.ok: t.inner_row_border = True longest_col = t.column_widths.index(max(t.column_widths)) max_length_col = t.column_max_width(longest_col) if max_length_col > 0: for i, content in enumerate(t.table_data): if len(content[longest_col]) > max_length_col: temp = '' for l in content[longest_col].splitlines(): if len(l) > max_length_col: temp += '\n'.join(textwrap.wrap(l, max_length_col)) + '\n' else: temp += l + '\n' content[longest_col] = temp.strip() t.table_data[i] = content return t.table
def user_width(_table: AsciiTable) -> int: return _table.column_max_width(3)
def bot_width(_table: AsciiTable) -> int: return _table.column_max_width(1)
printarray1.sort(key=lambda x: x[0][9:], reverse=False) printarray2.sort(key=lambda x: x[0][9:], reverse=False) printarray1.insert(0, [ "Queue", "PID", "Amount", "Paste start time", "Processing time for current paste (H:M:S)", "Paste hash" ]) printarray2.insert(0, [ "Queue", "PID", "Amount", "Paste start time", "Time since idle (H:M:S)", "Last paste hash" ]) printarray3.insert(0, ["Queue", "State"]) os.system('clear') t1 = AsciiTable(printarray1, title="Working queues") t1.column_max_width(1) if not t1.ok: longest_col = t1.column_widths.index(max(t1.column_widths)) max_length_col = t1.column_max_width(longest_col) if max_length_col > 0: for i, content in enumerate(t1.table_data): if len(content[longest_col]) > max_length_col: temp = '' for l in content[longest_col].splitlines(): if len(l) > max_length_col: temp += '\n'.join( textwrap.wrap( l, max_length_col)) + '\n' else: temp += l + '\n' content[longest_col] = temp.strip()
def run_evaluation(corpus, golden): """ Generate tables containing evaluation data """ wordsTable, correctTable, statsTable = None, None, None results = [] for i in range(State.LOOPS): results.append(evaluate_corpus(corpus, golden)) if State.DEBUG: # TODO: format this output with labels and a table? print("[DEBUG]" , results) # Initialize counters percentCor, percentIncor, percentWithin = 0, 0, 0 distances, totalWithin, totalWithinCor = 0, 0, 0 for evalResult in results: # Format output into tables WORDS_DATA = ( ("Within Correct", "Within Incorrect", "Without Number"), ("\n".join([ val[0] for val in evalResult["withinCor"] ]) , "\n".join(evalResult["withinIncor"]), evalResult["withoutN"]) ) wordsTable = AsciiTable(WORDS_DATA, "Word Results") # Only output word result tables if NOT looping --minimize output if State.DEBUG or State.LOOPS == 1: print("\n" + wordsTable.table) MATCH_DATA = ("Word" , "Jaccard Value" , "Matching Corpus Value" , "Hint", "Max Jaccard Value", "Jaccard Distance") data = [] correctTable = AsciiTable([MATCH_DATA, []]) # Cap column widths at 35 chars for widest columns maxValWidth = min(correctTable.column_max_width(2), 35) maxHintWidth = min(correctTable.column_max_width(3), 35) for i in range(len(evalResult["withinCor"])): r = list(evalResult["withinCor"][i]) # Format text to wrap wrappedVal = '\n'.join(wrap(r[2], maxValWidth)) wrappedHint = '\n'.join(wrap(r[3], maxHintWidth)) r[2] = wrappedVal r[3] = wrappedHint if State.DEBUG: print(r) data.append(r) correctTable = AsciiTable(tuple([MATCH_DATA] + data), "Correct Matches Results") # Only output word result tables if NOT looping --minimize output if State.DEBUG or State.LOOPS == 1: print("\n" + correctTable.table) # Check if we had any hits at all, otherwise output zeroes if (State.SAMPLES != evalResult["withoutN"]): withinN = State.SAMPLES - evalResult["withoutN"] withinCor = len(evalResult["withinCor"]) percentCor += withinCor / withinN percentIncor += len(evalResult["withinIncor"]) / withinN percentWithin += withinN / State.SAMPLES # Sum the distances for each result from this loop to calc overal average for word in evalResult["withinCor"]: distances += word[5] totalWithin += withinN totalWithinCor += withinCor STATS_DATA = ( ("Average Percentage Within Correct" , "Average Percentage Within Incorrect", "Average Percentage Within", "Average Jaccard Distance"), (percentCor / State.LOOPS, percentIncor / State.LOOPS, percentWithin / State.LOOPS, distances / totalWithinCor if totalWithinCor > 0 else 0.0) ) statsTable = AsciiTable(STATS_DATA, "Statistics") print("\n" + statsTable.table)
def printable_summary(list_of_changed_files, status_cmake_configure, status_make, status_make_install, status_amazon_s3_upload, status_tests, summary_vera, summary_cppcheck, summary_format, summary_pep8, summary_errors, summary_warnings, number_of_errors, number_of_warnings, expected_warnings, number_of_tests_total, number_of_tests_failed, number_of_tests_skipped, failed_tests, test_time, ignore_vera, ignore_cppcheck, ignore_format, ignore_pep8, exit_code): """Create an overall build summary in a printable format. Parameters ---------- list_of_changed_files: List of changed source files. status_cmake_configure: Status of the 'CMake configure': True, False or None status_make: Status of the 'make': True, False or None status_make_install: Status of the 'make install': True, False or None status_amazon_s3_upload: Status of the Amazon S3 upload: True, False status_tests: Status of the test suite run: True, False or None summary_vera: Dictionary of dictionaries of VERA++ messages per file. summary_cppcheck: Dictionary of dictionaries of cppcheck messages per file. summary_format: Dictionary of dictionaries of clang-format messages per file. summary_pep8: Dictionary of dictionaries of PEP8 messages per file. summary_errors: Dictionary of build error messages. summary_warnings: Dictionary of build warning messages. number_of_errors: Number of errors. number_of_warnings: Number of warnings. expected_warnings: Number of warnings expected. number_of_tests_total: Number of tests total. number_of_tests_failed: Number of tests failed. number_of_tests_skipped: Number of tests skipped. failed_tests: List of failed tests. test_time: Time required to run testsuite. exit_code: Build exit code: 0 or 1. Returns ------- Formatted build summary string. """ header = """ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + N E S T G i t H u b A c t i o n s B u i l d S u m m a r y + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \n\n""" build_summary = header if get_num_msgs(summary_vera) > 0 or \ get_num_msgs(summary_cppcheck) > 0 or \ get_num_msgs(summary_format) > 0 or \ get_num_msgs(summary_pep8) > 0: build_summary += ' S T A T I C C O D E A N A L Y S I S\n' # Create formatted per-file-tables of VERA++, Cppcheck, clang-format # and PEP8 messages. build_summary += code_analysis_per_file_tables(summary_vera, summary_cppcheck, summary_format, summary_pep8) if number_of_warnings > 0: build_summary += '\n W A R N I N G S\n' build_summary += warnings_table(summary_warnings) if number_of_errors > 0: build_summary += '\n E R R O R S\n' build_summary += errors_table(summary_errors) build_summary += '\n\n B U I L D R E P O R T\n' summary_table = [ ['Changed Files :', ''], ['', 'No files have been changed.'], ['Static Code Analysis :', ''], ['VERA++', convert_summary_to_status_string(summary_vera, ignore_vera) + '\n' + '\nNumber of messages (MSGBLD0135): ' + str(get_num_msgs(summary_vera))], ['Cppcheck', convert_summary_to_status_string(summary_cppcheck, ignore_cppcheck) + '\n' + '\nNumber of messages (MSGBLD0155): ' + str(get_num_msgs(summary_cppcheck))], ['clang-format', convert_summary_to_status_string(summary_format, ignore_format) + '\n' + '\nNumber of messages (MSGBLD0175): ' + str(get_num_msgs(summary_format))], ['PEP8', convert_summary_to_status_string(summary_pep8, ignore_pep8) + '\n' + '\nNumber of messages (MSGBLD0195): ' + str(get_num_msgs(summary_pep8))], ['NEST Build :', ''], ['CMake configure', convert_bool_value_to_status_string(status_cmake_configure)], ['Make', convert_bool_value_to_status_string(status_make) + '\n' + '\nErrors : ' + str(number_of_errors) + '\nWarnings: ' + str(number_of_warnings)], ['Make install', convert_bool_value_to_status_string(status_make_install)], ['Make installcheck', convert_bool_value_to_status_string(status_tests) + '\n' + '\nTestsuite runtime : {:d}s'.format(int(test_time)) + '\nTotal number of tests : ' + str(number_of_tests_total) + '\nNumber of tests skipped: ' + str(number_of_tests_skipped) + '\nNumber of tests failed : ' + str(number_of_tests_failed) + report_failed_tests(failed_tests) ], ['Artifacts :', ''], ['Amazon S3 upload', convert_bool_value_to_yes_no_string(status_amazon_s3_upload)] ] table = AsciiTable(summary_table) table.inner_row_border = True max_width = table.column_max_width(1) # Bypass Travis issue: ValueError: invalid width -29 (must be > 0) # (in the wrap() below max_width must be > 0) # The calculation of column_max_width is based on the returned terminal # width which sometimes seems to be zero resulting in a negative value. if max_width < 0: max_width = 70 table.table_data[1][1] = '\n'.join(wrap(', '.join(list_of_changed_files), max_width)) build_summary += table.table + '\n' if exit_code == 0: build_summary += '\nBUILD TERMINATED SUCCESSFULLY' else: build_summary += '\nBUILD FAILED' return build_summary
def do_preset(self, arg): """Show/execute the hardening module presets""" presets = get_presets() if arg: modules = [preset['modules'] for preset in presets \ if preset['name'] == arg] if len(modules) == 0: logger.error(f"Preset not found: '{arg}'") return modules = modules[0] confirm_prompt = [{ 'type': 'confirm', 'name': 'confirm', 'message': 'Run modules without confirmation?', }] try: conf_mod = prompt(confirm_prompt)['confirm'] except: return # Main module loop for module in modules: # Select the namespace self.namespace = module.split("/")[0].lower() # Loop through the modules for name, mod in self.modules[self.namespace].items(): # Select the module if it equals to the module in # the preset or equals to 'all' if module.split("/")[1].lower() == "all" or \ module.split("/")[1].lower() == name.lower(): # Select the module self.module = str(mod) # Show module information self.do_info(None) # If confirmation not needed if conf_mod: self.do_harden(None) else: # Ask for permission for executing the command exec_conf = prompt([{ 'type': 'confirm', 'name': 'confirm', 'message': 'Execute the hardening command?', }]) # Execute the command or cancel try: if exec_conf['confirm']: self.do_harden(None) else: raise Exception("Cancelled by user.") except: logger.info("Hardening cancelled. " + \ f"({self.namespace}/{self.module})") # Go back from the selected module and namespace self.module = "" self.namespace = "" else: if not presets: logger.warn(f"No presets found in {mod_json_file}") return search_table = [['Preset', 'Modules']] table = AsciiTable(search_table) max_width = table.column_max_width(1) # Create a table of presets for preset in presets: mods = "" for module in preset['modules'][:-1]: mods += '\n'.join(textwrap.wrap(module, max_width - 40)) + '\n' mods += '\n'.join( textwrap.wrap(preset['modules'][-1], max_width - 40)) table.table_data.append([preset['name'], mods]) # Show the table if len(table.table_data) > 1: print(table.table) else: logger.warn(f"No presets found in {mod_json_file}")
printarray3.append([curr_queue, "Stuck or idle, restarting in " + str(abs(args.treshold - (int(time.time()) - no_info_modules[curr_queue]))) + "s"]) else: printarray3.append([curr_queue, "Stuck or idle, restarting disabled"]) ## FIXME To add: ## Button KILL Process using Curses printarray1.sort(key=lambda x: x[0][9:], reverse=False) printarray2.sort(key=lambda x: x[0][9:], reverse=False) printarray1.insert(0,["Queue", "PID", "Amount", "Paste start time", "Processing time for current paste (H:M:S)", "Paste hash"]) printarray2.insert(0,["Queue", "PID","Amount", "Paste start time", "Time since idle (H:M:S)", "Last paste hash"]) printarray3.insert(0,["Queue", "State"]) os.system('clear') t1 = AsciiTable(printarray1, title="Working queues") t1.column_max_width(1) if not t1.ok: longest_col = t1.column_widths.index(max(t1.column_widths)) max_length_col = t1.column_max_width(longest_col) if max_length_col > 0: for i, content in enumerate(t1.table_data): if len(content[longest_col]) > max_length_col: temp = '' for l in content[longest_col].splitlines(): if len(l) > max_length_col: temp += '\n'.join(textwrap.wrap(l, max_length_col)) + '\n' else: temp += l + '\n' content[longest_col] = temp.strip() t1.table_data[i] = content
def results_printer(results, no_colors=False): any_message = any(bool(r.message) for r in results) headers = ['#', 'Time', 'Test name'] detailed_report = False if no_colors: headers.append('Result') if any_message: headers.append('Description') table_data = [headers] text_colors = { 0: Colors.OK, 1: Colors.WARNING, 2: Colors.FAIL, } text_statuses = { 0: 'OK', 1: 'WARNINIG', 2: 'FAIL', } for i, result in enumerate(results, 1): if no_colors: data_row = [ i, f'{result.duration}ms', result.title, text_statuses[result.status], ] else: text_color = text_colors[result.status] data_row = [ i, f'{text_color}{result.duration}ms{Colors.ENDC}', f'{text_color}{result.title}{Colors.ENDC}', ] if any_message: data_row.append(result.message or '') table_data.append(data_row) if not detailed_report and result.response: detailed_report = True table = AsciiTable(table_data) if any_message: columns_count = len(headers) - 1 message_size = table.column_max_width(columns_count) for column in table.table_data: column[columns_count] = '\n'.join(wrap(column[columns_count], message_size)) print(table.table) if detailed_report: print('\nREPORTS FROM FAILED TESTS\n') for i, result in enumerate(results, 1): if result.response: if no_colors: print(f'--- #{i} {result.title} ---') print('\nURL') print(result.response.url) print('\nSTATUS_CODE') print(result.response.status_code) print('\nHEADERS') for header, value in result.response.headers.items(): print(f'{header}: {value}') print('\nPAYLOAD') print(result.response.payload) print('\nRESPONSE') print(result.response.body) else: print(f'{Colors.FAIL}--- #{i} {result.title} ---{Colors.ENDC}') print(f'\n{Colors.WARNING}URL{Colors.ENDC}') print(result.response.url) print(f'\n{Colors.WARNING}STATUS_CODE{Colors.ENDC}') print(result.response.status_code) print(f'\n{Colors.WARNING}HEADERS{Colors.ENDC}') for header, value in result.response.headers.items(): print(f'{header}: {value}') print(f'\n{Colors.WARNING}PAYLOAD{Colors.ENDC}') print(result.response.payload) print(f'\n{Colors.WARNING}RESPONSE{Colors.ENDC}') print(result.response.body) print()
def printable_summary(list_of_changed_files, status_cmake_configure, status_make, status_make_install, status_amazon_s3_upload, status_tests, summary_vera, summary_cppcheck, summary_format, summary_pep8, summary_errors, summary_warnings, number_of_errors, number_of_warnings, number_of_tests_total, number_of_tests_failed, ignore_vera, ignore_cppcheck, ignore_format, ignore_pep8, exit_code): """Create an overall build summary in a printable format. Parameters ---------- list_of_changed_files: List of changed source files. status_cmake_configure: Status of the 'CMake configure': True, False or None status_make: Status of the 'make': True, False or None status_make_install: Status of the 'make install': True, False or None status_amazon_s3_upload: Status of the Amazon S3 upload: True, False status_tests: Status of the test suite run: True, False or None summary_vera: Dictionary of dictionaries of VERA++ messages per file. summary_cppcheck: Dictionary of dictionaries of cppcheck messages per file. summary_format: Dictionary of dictionaries of clang-format messages per file. summary_pep8: Dictionary of dictionaries of PEP8 messages per file. summary_errors: Dictionary of build error messages. summary_warnings: Dictionary of build warning messages. number_of_errors: Number of errors. number_of_warnings: Number of warnings. number_of_tests_total: Number of tests total. number_of_tests_failed: Number of tests failed. exit_code: Build exit code: 0 or 1. Returns ------- Formatted build summary string. """ header = """ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + N E S T T r a v i s C I B u i l d S u m m a r y + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \n\n""" build_summary = header if get_num_msgs(summary_vera) > 0 or \ get_num_msgs(summary_cppcheck) > 0 or \ get_num_msgs(summary_format) > 0 or \ get_num_msgs(summary_pep8) > 0: build_summary += ' S T A T I C C O D E A N A L Y S I S\n' # Create formatted per-file-tables of VERA++, Cppcheck, clang-format # and PEP8 messages. build_summary += code_analysis_per_file_tables(summary_vera, summary_cppcheck, summary_format, summary_pep8) if number_of_warnings > 0: build_summary += '\n W A R N I N G S\n' build_summary += warnings_table(summary_warnings) if number_of_errors > 0: build_summary += '\n E R R O R S\n' build_summary += errors_table(summary_errors) build_summary += '\n\n B U I L D R E P O R T\n' summary_table = [ ['Changed Files :', ''], ['', 'No files have been changed.'], ['Static Code Analysis :', ''], ['VERA++', convert_summary_to_status_string(summary_vera, ignore_vera) + '\n' + '\nNumber of messages (MSGBLD0135): ' + str(get_num_msgs(summary_vera))], ['Cppcheck', convert_summary_to_status_string(summary_cppcheck, ignore_cppcheck) + '\n' + '\nNumber of messages (MSGBLD0155): ' + str(get_num_msgs(summary_cppcheck))], ['clang-format', convert_summary_to_status_string(summary_format, ignore_format) + '\n' + '\nNumber of messages (MSGBLD0175): ' + str(get_num_msgs(summary_format))], ['PEP8', convert_summary_to_status_string(summary_pep8, ignore_pep8) + '\n' + '\nNumber of messages (MSGBLD0195): ' + str(get_num_msgs(summary_pep8))], ['NEST Build :', ''], ['CMake configure', convert_bool_value_to_status_string(status_cmake_configure)], ['Make', convert_bool_value_to_status_string(status_make) + '\n' + '\nErrors : ' + str(number_of_errors) + '\nWarnings: ' + str(number_of_warnings)], ['Make install', convert_bool_value_to_status_string(status_make_install)], ['Make installcheck', convert_bool_value_to_status_string(status_tests) + '\n' + '\nTotal number of tests : ' + str(number_of_tests_total) + '\nNumber of tests failed: ' + str(number_of_tests_failed)], ['Artifacts :', ''], ['Amazon S3 upload', convert_bool_value_to_yes_no_string(status_amazon_s3_upload)] ] table = AsciiTable(summary_table) table.inner_row_border = True max_width = table.column_max_width(1) # Bypass Travis issue: ValueError: invalid width -29 (must be > 0) # (in the wrap() below max_width must be > 0) # The calculation of column_max_width is based on the returned terminal # width which sometimes seems to be zero resulting in a negative value. if max_width < 0: max_width = 70 table.table_data[1][1] = '\n'.join(wrap(', '.join(list_of_changed_files), max_width)) build_summary += table.table + '\n' if exit_code == 0: build_summary += '\nBUILD TERMINATED SUCCESSFULLY' else: build_summary += '\nBUILD FAILED' return build_summary
def printable_summary(list_of_changed_files, status_vera_init, status_cppcheck_init, status_format_init, status_cmake_configure, status_make, status_make_install, status_amazon_s3_upload, status_tests, summary_vera, summary_cppcheck, summary_format, summary_pep8, summary_errors, summary_warnings, number_of_errors, number_of_warnings, number_of_tests_total, number_of_tests_failed, exit_code): """Create an overall build summary in a printable format. Parameters ---------- list_of_changed_files: List of changed source files. status_vera_init: Status of the VERA++ initialization: True, False or None status_cppcheck_init: Status of the cppcheck initialization: True, False or None status_format_init: Status of the clang-format initialization: True, False or None status_cmake_configure: Status of the 'CMake configure': True, False or None status_make: Status of the 'make': True, False or None status_make_install: Status of the 'make install': True, False or None status_amazon_s3_upload: Status of the Amazon S3 upload: True, False status_tests: Status of the test suite run: True, False or None summary_vera: Dictionary of dictionaries of VERA++ messages per file. summary_cppcheck: Dictionary of dictionaries of cppcheck messages per file. summary_format: Dictionary of dictionaries of clang-format messages per file. summary_pep8: Dictionary of dictionaries of PEP8 messages per file. summary_errors: Dictionary of build error messages. summary_warnings: Dictionary of build warning messages. number_of_errors: Number of errors. number_of_warnings: Number of warnings. number_of_tests_total: Number of tests total. number_of_tests_failed: Number of tests failed. exit_code: Build exit code: 0 or 1. Returns ------- Formatted build summary string. """ header = """ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + N E S T T r a v i s C I B u i l d S u m m a r y + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \n\n""" build_summary = header if number_of_msgs_in_summary(summary_vera) > 0 or \ number_of_msgs_in_summary(summary_cppcheck) > 0 or \ number_of_msgs_in_summary(summary_format) > 0 or \ number_of_msgs_in_summary(summary_pep8) > 0: build_summary += ' S T A T I C C O D E A N A L Y S I S\n' # Create formatted per-file-tables of VERA++, Cppcheck, clang-format # and PEP8 messages. build_summary += code_analysis_per_file_tables(summary_vera, summary_cppcheck, summary_format, summary_pep8) if number_of_warnings > 0: build_summary += '\n W A R N I N G S\n' build_summary += warnings_table(summary_warnings) if number_of_errors > 0: build_summary += '\n E R R O R S\n' build_summary += errors_table(summary_errors) build_summary += '\n\n B U I L D R E P O R T\n' summary_table = [ ['Changed Files :', ''], ['', 'No files have been changed.'], ['Tools Initialization :', ''], ['VERA++', convert_bool_value_to_status_string(status_vera_init)], [ 'Cppcheck (DEACTIVATED)', convert_bool_value_to_status_string(status_cppcheck_init) ], [ 'clang-format', convert_bool_value_to_status_string(status_format_init) ], ['Static Code Analysis :', ''], [ 'VERA++', convert_summary_to_status_string(summary_vera) + '\n' + '\nNumber of messages (MSGBLD0135): ' + str(number_of_msgs_in_summary(summary_vera)) ], [ 'Cppcheck (DEACTIVATED)', convert_summary_to_status_string(summary_cppcheck) + '\n' + '\nNumber of messages (MSGBLD0155): ' + str(number_of_msgs_in_summary(summary_cppcheck)) ], [ 'clang-format', convert_summary_to_status_string(summary_format) + '\n' + '\nNumber of messages (MSGBLD0175): ' + str(number_of_msgs_in_summary(summary_format)) ], [ 'PEP8', convert_summary_to_status_string(summary_pep8) + '\n' + '\nNumber of messages (MSGBLD0195): ' + str(number_of_msgs_in_summary(summary_pep8)) ], ['NEST Build :', ''], [ 'CMake configure', convert_bool_value_to_status_string(status_cmake_configure) ], [ 'Make', convert_bool_value_to_status_string(status_make) + '\n' + '\nErrors : ' + str(number_of_errors) + '\nWarnings: ' + str(number_of_warnings) ], [ 'Make install', convert_bool_value_to_status_string(status_make_install) ], [ 'Make installcheck', convert_bool_value_to_status_string(status_tests) + '\n' + '\nTotal number of tests : ' + str(number_of_tests_total) + '\nNumber of tests failed: ' + str(number_of_tests_failed) ], ['Artifacts :', ''], [ 'Amazon S3 upload', convert_bool_value_to_yes_no_string(status_amazon_s3_upload) ] ] table = AsciiTable(summary_table) table.inner_row_border = True max_width = table.column_max_width(1) table.table_data[1][1] = '\n'.join( wrap(', '.join(list_of_changed_files), max_width)) build_summary += table.table + '\n' if exit_code == 0: build_summary += '\nBUILD TERMINATED SUCCESSFULLY' else: build_summary += '\nBUILD FAILED' return (build_summary)