From ec4e408b2bd52ffdb38a6b32a6e7562420bc2b70 Mon Sep 17 00:00:00 2001 From: Wilson Snyder Date: Fri, 18 Dec 2020 22:34:14 -0500 Subject: [PATCH] Add yapf and reformat python code --- Makefile.in | 13 ++++++ examples/xml_py/vl_file_copy | 60 ++++++++++++++----------- examples/xml_py/vl_hier_graph | 73 ++++++++++++++++++------------- nodist/fuzzer/actual_fail | 11 +++-- nodist/fuzzer/generate_dictionary | 43 ++++++++++-------- src/Makefile.in | 2 + 6 files changed, 125 insertions(+), 77 deletions(-) diff --git a/Makefile.in b/Makefile.in index b774e9ec3..87e17558f 100644 --- a/Makefile.in +++ b/Makefile.in @@ -461,6 +461,8 @@ analyzer-include: -rm -rf examples/*/obj* scan-build $(MAKE) -k examples +format: clang-format yapf + CLANGFORMAT = clang-format CLANGFORMAT_FLAGS = -i CLANGFORMAT_FILES = $(CPPCHECK_CPP) $(CPPCHECK_H) $(CPPCHECK_YL) test_regress/t/*.c* test_regress/t/*.h @@ -470,6 +472,17 @@ clang-format: || echo "*** You are not using clang-format 10.0, indents may differ from master's ***" $(CLANGFORMAT) $(CLANGFORMAT_FLAGS) $(CLANGFORMAT_FILES) +YAPF = yapf +YAPF_FLAGS = -i +YAPF_FILES = \ + nodist/fuzzer/actual_fail \ + nodist/fuzzer/generate_dictionary \ + examples/xml_py/vl_file_copy \ + examples/xml_py/vl_hier_graph \ + +yapf: + $(YAPF) $(YAPF_FLAGS) $(YAPF_FILES) + ftp: info install-msg: diff --git a/examples/xml_py/vl_file_copy b/examples/xml_py/vl_file_copy index 0a5125c2a..2749f3442 100755 --- a/examples/xml_py/vl_file_copy +++ b/examples/xml_py/vl_file_copy @@ -10,24 +10,29 @@ import sys import tempfile import xml.etree.ElementTree as ET from shutil import copy2 -from pprint import pprint,pformat +from pprint import pprint, pformat ####################################################################### + class VlFileCopy: - def __init__(self, - verilator_args, # presently all verilator options are passed-thru - # ideally this script would check against options mentioned in help - debug=0, - output_dir='copied'): # directory name we output file uses + def __init__( + self, + verilator_args, # presently all verilator options are passed-thru + # ideally this script would check against options mentioned in help + debug=0, + output_dir='copied'): # directory name we output file uses self.debug = debug xml_temp = tempfile.NamedTemporaryFile() - args = ['--xml-output', xml_temp.name, - '--bbox-sys', # Parse some stuff can't translate - '--bbox-unsup', - '--prefix vlxml'] # So we know name of .xml output + args = [ + '--xml-output', + xml_temp.name, + '--bbox-sys', # Parse some stuff can't translate + '--bbox-unsup', + '--prefix vlxml' + ] # So we know name of .xml output args += verilator_args self.run_verilator(args) self.tree = ET.parse(xml_temp.name) @@ -55,16 +60,18 @@ class VlFileCopy: print("\t%s " % command) status = subprocess.call(command, shell=True) if status != 0: - raise Exception("Command failed running Verilator with '"+command+"', stopped") + raise Exception("Command failed running Verilator with '" + + command + "', stopped") + ####################################################################### -if __name__=='__main__': +if __name__ == '__main__': parser = argparse.ArgumentParser( allow_abbrev=False, formatter_class=argparse.RawTextHelpFormatter, description= -"""Example of using Verilator XML output to copy a list of files to an + """Example of using Verilator XML output to copy a list of files to an output directory (-odir, defaults to 'copied'), e.g. to easily create a tarball of the design to pass to others. @@ -72,8 +79,7 @@ Example usage: vl_file_copy -f input.vc top.v -odir mycopy # This will make at least mycopy/top.v """, - epilog= -"""All other arguments are pass-thru to Verilator: e.g.: + epilog="""All other arguments are pass-thru to Verilator: e.g.: +define+= Set preprocessor define -F Parse options from a file, relatively @@ -87,26 +93,30 @@ Example usage: This file ONLY is placed under the Creative Commons Public Domain, for any use, without warranty, 2019 by Wilson Snyder. SPDX-License-Identifier: CC0-1.0 -""" - ) - parser.add_argument('-debug', '--debug', - action='store_const', const=9, +""") + parser.add_argument('-debug', + '--debug', + action='store_const', + const=9, help='enable debug') - parser.add_argument('-odir', '--odir', - action='store', metavar='directory', required=True, + parser.add_argument('-odir', + '--odir', + action='store', + metavar='directory', + required=True, help='target output directory') (args, rem) = parser.parse_known_args() - print("NOTE: vl_file_copy is only an example starting point for writing your own tool.") + print( + "NOTE: vl_file_copy is only an example starting point for writing your own tool." + ) # That is: # 1. We will accept basic patches # 2. We are not expecting to make this globally useful. (e.g. we don't cleanup obj_dir) # 3. "make install" will not install this. # 4. This has not had production-worthy validation. - fc = VlFileCopy(output_dir = args.odir, - debug = args.debug, - verilator_args = rem) + fc = VlFileCopy(output_dir=args.odir, debug=args.debug, verilator_args=rem) ###################################################################### ### Local Variables: diff --git a/examples/xml_py/vl_hier_graph b/examples/xml_py/vl_hier_graph index 038518614..eaa2d2e28 100755 --- a/examples/xml_py/vl_hier_graph +++ b/examples/xml_py/vl_hier_graph @@ -10,26 +10,31 @@ import sys import tempfile import xml.etree.ElementTree as ET from shutil import copy2 -from pprint import pprint,pformat +from pprint import pprint, pformat ####################################################################### + class VlHierGraph: - def __init__(self, - verilator_args, # presently all verilator options are passed-thru - # ideally this script would check against options mentioned in help - debug=0, - output_filename='graph.dot'): # output filename + def __init__( + self, + verilator_args, # presently all verilator options are passed-thru + # ideally this script would check against options mentioned in help + debug=0, + output_filename='graph.dot'): # output filename self.debug = debug self.next_vertex_number = 0 self.name_to_number = {} xml_temp = tempfile.NamedTemporaryFile() - args = ['--xml-output', xml_temp.name, - '--bbox-sys', # Parse some stuff can't translate - '--bbox-unsup', - '--prefix vlxml'] # So we know name of .xml output + args = [ + '--xml-output', + xml_temp.name, + '--bbox-sys', # Parse some stuff can't translate + '--bbox-unsup', + '--prefix vlxml' + ] # So we know name of .xml output args += verilator_args self.run_verilator(args) self.tree = ET.parse(xml_temp.name) @@ -39,7 +44,9 @@ class VlHierGraph: fh.write("digraph {\n") fh.write(" dpi=300;\n") fh.write(" order=LR;\n") - fh.write(" node [fontsize=8 shape=\"box\" margin=0.01 width=0 height=0]") + fh.write( + " node [fontsize=8 shape=\"box\" margin=0.01 width=0 height=0]" + ) fh.write(" edge [fontsize=6]") # Find cells root = self.tree.getroot() @@ -48,8 +55,7 @@ class VlHierGraph: # origNames are before parameterization, name if after mod_name = module.get('name') mod_number = self.name_to_vertex_number(mod_name) - fh.write(" n%d [label=\"%s\"" - % (mod_number, mod_name)) + fh.write(" n%d [label=\"%s\"" % (mod_number, mod_name)) if module.get('topModule'): fh.write(" color=\"red\" rank=1") fh.write("];\n") @@ -58,8 +64,8 @@ class VlHierGraph: inst_name = instance.get('name') def_name = instance.get('defName') def_number = self.name_to_vertex_number(def_name) - fh.write(" n%d->n%d [label=\"%s\"];\n" - % (mod_number, def_number, inst_name)); + fh.write(" n%d->n%d [label=\"%s\"];\n" % + (mod_number, def_number, inst_name)) fh.write("}\n") @@ -80,24 +86,25 @@ class VlHierGraph: print("\t%s " % command) status = subprocess.call(command, shell=True) if status != 0: - raise Exception("Command failed running Verilator with '"+command+"', stopped") + raise Exception("Command failed running Verilator with '" + + command + "', stopped") + ####################################################################### -if __name__=='__main__': +if __name__ == '__main__': parser = argparse.ArgumentParser( allow_abbrev=False, formatter_class=argparse.RawTextHelpFormatter, description= -"""Example of using Verilator XML output to create a .dot file showing the + """Example of using Verilator XML output to create a .dot file showing the design module hierarchy. Example usage: vl_hier_graph -f input.vc top.v -o graph.dot dot -Tpdf -o graph.pdf graph.dot """, - epilog= -"""All other arguments are pass-thru to Verilator: e.g.: + epilog="""All other arguments are pass-thru to Verilator: e.g.: +define+= Set preprocessor define -F Parse options from a file, relatively @@ -111,26 +118,32 @@ Example usage: This file ONLY is placed under the Creative Commons Public Domain, for any use, without warranty, 2019 by Wilson Snyder. SPDX-License-Identifier: CC0-1.0 -""" - ) - parser.add_argument('-debug', '--debug', - action='store_const', const=9, +""") + parser.add_argument('-debug', + '--debug', + action='store_const', + const=9, help='enable debug') - parser.add_argument('-o', '--o', - action='store', metavar='filename', required=True, + parser.add_argument('-o', + '--o', + action='store', + metavar='filename', + required=True, help='output filename') (args, rem) = parser.parse_known_args() - print("NOTE: vl_hier_graph is only an example starting point for writing your own tool.") + print( + "NOTE: vl_hier_graph is only an example starting point for writing your own tool." + ) # That is: # 1. We will accept basic patches # 2. We are not expecting to make this globally useful. (e.g. we don't cleanup obj_dir) # 3. "make install" will not install this. # 4. This has not had production-worthy validation. - fc = VlHierGraph(output_filename = args.o, - debug = args.debug, - verilator_args = rem) + fc = VlHierGraph(output_filename=args.o, + debug=args.debug, + verilator_args=rem) ###################################################################### ### Local Variables: diff --git a/nodist/fuzzer/actual_fail b/nodist/fuzzer/actual_fail index e8d860fae..4ddcc9c06 100755 --- a/nodist/fuzzer/actual_fail +++ b/nodist/fuzzer/actual_fail @@ -16,6 +16,7 @@ from glob import glob from subprocess import getstatusoutput from argparse import ArgumentParser + def interesting(s): if 'assert' in s: return 1 if 'Assert' in s: return 1 @@ -33,17 +34,19 @@ def interesting(s): def main(): p = ArgumentParser() - p.add_argument('--dir',default='out1/queue') + p.add_argument('--dir', default='out1/queue') args = p.parse_args() - for infile in glob(args.dir+'/*'): + for infile in glob(args.dir + '/*'): # Input filenames are known not to contain spaces or other unusual # characters, therefore this works. - status,output = getstatusoutput('../../bin/verilator_bin --cc '+infile) + status, output = getstatusoutput('../../bin/verilator_bin --cc ' + + infile) if interesting(output): print(infile) print(status) print(output) -if __name__=='__main__': + +if __name__ == '__main__': main() diff --git a/nodist/fuzzer/generate_dictionary b/nodist/fuzzer/generate_dictionary index f7196b2e3..b030f23ea 100755 --- a/nodist/fuzzer/generate_dictionary +++ b/nodist/fuzzer/generate_dictionary @@ -15,7 +15,8 @@ from subprocess import getstatusoutput from os import system -def take_while(f,a): + +def take_while(f, a): # any(a) => (a->bool)->[a]->[a] # Does the same think as Haskell's takewhile. out = [] @@ -26,44 +27,50 @@ def take_while(f,a): return out return out -def skip_while(f,a): + +def skip_while(f, a): # any(a) => (a->bool)->[a]->[a] # Basically, the opposite thing from skipwhile while len(a) and f(a[0]): a = a[1:] return a + def print_lines(a): # printable(a) => [a]->void for elem in a: print(elem) -def write_file(filename,contents): + +def write_file(filename, contents): # str->str->void - f = open(filename,'w') + f = open(filename, 'w') f.write(contents) + def parse_line(s): # str->maybe str - if len(s)==0: return - part = skip_while(lambda x: x!='"',s) - if len(part)==0 or part[0]!='"': return None - literal_part = take_while(lambda x: x!='"',part[1:]) - return ''.join(filter(lambda x: x!='\\',literal_part)) + if len(s) == 0: return + part = skip_while(lambda x: x != '"', s) + if len(part) == 0 or part[0] != '"': return None + literal_part = take_while(lambda x: x != '"', part[1:]) + return ''.join(filter(lambda x: x != '\\', literal_part)) + def main(): - status,output = getstatusoutput('flex -T ../../src/verilog.l') - assert status==0 + status, output = getstatusoutput('flex -T ../../src/verilog.l') + assert status == 0 lines = output.splitlines() - lines = take_while(lambda x: 'beginning dump of nfa' not in x,lines) - tokens = set(filter(lambda x: x,map(parse_line,lines))) + lines = take_while(lambda x: 'beginning dump of nfa' not in x, lines) + tokens = set(filter(lambda x: x, map(parse_line, lines))) dirname = 'dictionary' - r = system('mkdir -p '+dirname) - assert(r==0) - for i,token in enumerate(tokens): - write_file(dirname+'/'+str(i),token) + r = system('mkdir -p ' + dirname) + assert (r == 0) + for i, token in enumerate(tokens): + write_file(dirname + '/' + str(i), token) -if __name__=='__main__': + +if __name__ == '__main__': main() diff --git a/src/Makefile.in b/src/Makefile.in index 8ca9b1d9e..ee32a0927 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -82,6 +82,8 @@ config_rev.h: ${srcdir}/config_rev.pl $(GIT_CHANGE_DEP) $(PERL) ${srcdir}/config_rev.pl ${srcdir} >$@ # Human convenience +format: + $(MAKE) -C .. $@ clang-format: $(MAKE) -C .. $@