def main(): parser = argparse.ArgumentParser( prog="docgen", formatter_class=argparse.RawDescriptionHelpFormatter, usage=USAGE, description=__doc__, epilog='defaults or the filename - can be used for stdin/stdout') parser.add_argument('--version', action='store_true', help='Show version and exit') parser.add_argument('-v', '--verbose', action='store_true', help='Verbose output during processing') parser.add_argument('-c', '--inline-css', action='store_true', help='Put CSS inline in output file') parser.add_argument('-d', '--asdiv', action='store_true', help='Output as a <div> without html header/trailer') parser.add_argument('-j', '--wavesvg-usejs', action='store_true', help='Waveforms should use javascript wavedrom ' 'rather than generating inline svg') parser.add_argument('-o', '--output', type=argparse.FileType('w'), default=sys.stdout, metavar='file', help='Output file (default stdout)') parser.add_argument('srcfile', nargs='?', metavar='file', default='-', help='source markdown file (default stdin)') args = parser.parse_args() if args.version: version.show_and_exit(__file__, ["Hjson", "Mistletoe"]) if (args.verbose): log.basicConfig(format="%(levelname)s: %(message)s", level=log.DEBUG) else: log.basicConfig(format="%(levelname)s: %(message)s") outfile = args.output with outfile: outfile.write( generate.generate_doc(args.srcfile, args.verbose, args.inline_css, not args.wavesvg_usejs, args.asdiv))
def main(): verbose = 0 parser = argparse.ArgumentParser( prog="regtool", formatter_class=argparse.RawDescriptionHelpFormatter, usage=USAGE, description=DESC) parser.add_argument('input', nargs='?', metavar='file', type=argparse.FileType('r'), default=sys.stdin, help='input file in Hjson type') parser.add_argument('-d', action='store_true', help='Output register documentation (html)') parser.add_argument('--cdefines', '-D', action='store_true', help='Output C defines header') parser.add_argument('--doc', action='store_true', help='Output source file documentation (gfm)') parser.add_argument('-j', action='store_true', help='Output as formatted JSON') parser.add_argument('-c', action='store_true', help='Output as JSON') parser.add_argument('-r', action='store_true', help='Output as SystemVerilog RTL') parser.add_argument('-s', action='store_true', help='Output as UVM Register class') parser.add_argument('-f', action='store_true', help='Output as FPV CSR rw assertion module') parser.add_argument('--outdir', '-t', help='Target directory for generated RTL; ' 'tool uses ../rtl if blank.') parser.add_argument('--dv-base-prefix', default='dv_base', help='Prefix for the DV register classes from which ' 'the register models are derived.') parser.add_argument('--outfile', '-o', type=argparse.FileType('w'), default=sys.stdout, help='Target filename for json, html, gfm.') parser.add_argument('--verbose', '-v', action='store_true', help='Verbose and run validate twice') parser.add_argument('--param', '-p', type=str, default="", help='''Change the Parameter values. Only integer value is supported. You can add multiple param arguments. Format: ParamA=ValA;ParamB=ValB ''') parser.add_argument('--version', '-V', action='store_true', help='Show version') parser.add_argument('--novalidate', action='store_true', help='Skip validate, just output json') args = parser.parse_args() if args.version: version.show_and_exit(__file__, ["Hjson", "Mako"]) verbose = args.verbose if (verbose): log.basicConfig(format="%(levelname)s: %(message)s", level=log.DEBUG) else: log.basicConfig(format="%(levelname)s: %(message)s") # Entries are triples of the form (arg, (format, dirspec)). # # arg is the name of the argument that selects the format. format is the # name of the format. dirspec is None if the output is a single file; if # the output needs a directory, it is a default path relative to the source # file (used when --outdir is not given). arg_to_format = [('j', ('json', None)), ('c', ('compact', None)), ('d', ('html', None)), ('doc', ('doc', None)), ('r', ('rtl', 'rtl')), ('s', ('dv', 'dv')), ('f', ('fpv', 'fpv/vip')), ('cdefines', ('cdh', None))] format = None dirspec = None for arg_name, spec in arg_to_format: if getattr(args, arg_name): if format is not None: log.error('Multiple output formats specified on ' 'command line ({} and {}).'.format(format, spec[0])) sys.exit(1) format, dirspec = spec if format is None: format = 'hjson' infile = args.input # Split parameters into key=value pairs. raw_params = args.param.split(';') if args.param else [] params = [] for idx, raw_param in enumerate(raw_params): tokens = raw_param.split('=') if len(tokens) != 2: raise ValueError('Entry {} in list of parameter defaults to ' 'apply is {!r}, which is not of the form ' 'param=value.'.format(idx, raw_param)) params.append((tokens[0], tokens[1])) # Define either outfile or outdir (but not both), depending on the output # format. outfile = None outdir = None if dirspec is None: if args.outdir is not None: log.error('The {} format expects an output file, ' 'not an output directory.'.format(format)) sys.exit(1) outfile = args.outfile else: if args.outfile is not sys.stdout: log.error('The {} format expects an output directory, ' 'not an output file.'.format(format)) sys.exit(1) if args.outdir is not None: outdir = args.outdir elif infile is not sys.stdin: outdir = str(PurePath(infile.name).parents[1].joinpath(dirspec)) else: # We're using sys.stdin, so can't infer an output directory name log.error( 'The {} format writes to an output directory, which ' 'cannot be inferred automatically if the input comes ' 'from stdin. Use --outdir to specify it manually.'.format( format)) sys.exit(1) if format == 'doc': with outfile: gen_selfdoc.document(outfile) exit(0) srcfull = infile.read() try: obj = IpBlock.from_text(srcfull, params, infile.name) except ValueError as err: log.error(str(err)) exit(1) if args.novalidate: with outfile: gen_json.gen_json(obj, outfile, format) outfile.write('\n') else: if format == 'rtl': return gen_rtl.gen_rtl(obj, outdir) if format == 'dv': return gen_dv.gen_dv(obj, args.dv_base_prefix, outdir) if format == 'fpv': return gen_fpv.gen_fpv(obj, outdir) src_lic = None src_copy = '' found_spdx = None found_lunder = None copy = re.compile(r'.*(copyright.*)|(.*\(c\).*)', re.IGNORECASE) spdx = re.compile(r'.*(SPDX-License-Identifier:.+)') lunder = re.compile(r'.*(Licensed under.+)', re.IGNORECASE) for line in srcfull.splitlines(): mat = copy.match(line) if mat is not None: src_copy += mat.group(1) mat = spdx.match(line) if mat is not None: found_spdx = mat.group(1) mat = lunder.match(line) if mat is not None: found_lunder = mat.group(1) if found_lunder: src_lic = found_lunder if found_spdx: src_lic += '\n' + found_spdx with outfile: if format == 'html': return gen_html.gen_html(obj, outfile) elif format == 'cdh': return gen_cheader.gen_cdefines(obj, outfile, src_lic, src_copy) else: return gen_json.gen_json(obj, outfile, format) outfile.write('\n')
def main(): format = 'hjson' verbose = 0 parser = argparse.ArgumentParser( prog="regtool", formatter_class=argparse.RawDescriptionHelpFormatter, usage=USAGE, description=DESC) parser.add_argument('input', nargs='?', metavar='file', type=argparse.FileType('r'), default=sys.stdin, help='input file in Hjson type') parser.add_argument('-d', action='store_true', help='Output register documentation (html)') parser.add_argument('--cdefines', '-D', action='store_true', help='Output C defines header') parser.add_argument('--ctdefines', '-T', action='store_true', help='Output C defines header (Titan style)') parser.add_argument('--doc', action='store_true', help='Output source file documentation (gfm)') parser.add_argument('-j', action='store_true', help='Output as formatted JSON') parser.add_argument('-c', action='store_true', help='Output as JSON') parser.add_argument('-r', action='store_true', help='Output as SystemVerilog RTL') parser.add_argument('-s', action='store_true', help='Output as UVM Register class') parser.add_argument('--outdir', '-t', help='Target directory for generated RTL, '\ 'tool uses ../rtl if blank.') parser.add_argument('--outfile', '-o', type=argparse.FileType('w'), default=sys.stdout, help='Target filename for json, html, gfm.') parser.add_argument('--verbose', '-v', action='store_true', help='Verbose and run validate twice') parser.add_argument('--param', '-p', type=str, default="", help='''Change the Parameter values. Only integer value is supported. You can add multiple param arguments. Format: ParamA=ValA;ParamB=ValB ''') parser.add_argument('--version', '-V', action='store_true', help='Show version') parser.add_argument('--novalidate', action='store_true', help='Skip validate, just output json') args = parser.parse_args() if args.version: version.show_and_exit(__file__, ["Hjson", "Mako"]) verbose = args.verbose if args.j: format = 'json' elif args.c: format = 'compact' elif args.d: format = 'html' elif args.doc: format = 'doc' elif args.r: format = 'rtl' elif args.s: format = 'dv' elif args.cdefines: format = 'cdh' elif args.ctdefines: format = 'cth' if (verbose): log.basicConfig(format="%(levelname)s: %(message)s", level=log.DEBUG) else: log.basicConfig(format="%(levelname)s: %(message)s") outfile = args.outfile infile = args.input params = args.param.split(';') if format == 'rtl': if args.outdir: outdir = args.outdir elif infile != sys.stdin: outdir = str(PurePath(infile.name).parents[1].joinpath("rtl")) else: # Using sys.stdin. not possible to generate RTL log.error("-r option cannot be used with pipe or stdin") elif format == 'dv': if args.outdir: outdir = args.outdir elif infile != sys.stdin: outdir = str(PurePath(infile.name).parents[1].joinpath("dv")) else: # Using sys.stdin. not possible to generate RTL log.error("-s option cannot be used with pipe or stdin") else: # Ignore outdir = "." if format == 'doc': with outfile: gen_selfdoc.document(outfile) exit(0) with infile: try: srcfull = infile.read() obj = hjson.loads(srcfull, use_decimal=True, object_pairs_hook=validate.checking_dict) except ValueError: raise SystemExit(sys.exc_info()[1]) if args.novalidate: with outfile: gen_json.gen_json(obj, outfile, format) outfile.write('\n') elif (validate.validate(obj, params=params) == 0): if (verbose): log.info("Second validate pass (should show added optional keys)") validate.validate(obj, params=params) if format == 'rtl': gen_rtl.gen_rtl(obj, outdir) return 0 if format == 'dv': gen_dv.gen_dv(obj, outdir) return 0 src_lic = None src_copy = '' found_spdx = None found_lunder = None copy = re.compile(r'.*(copyright.*)|(.*\(c\).*)', re.IGNORECASE) spdx = re.compile(r'.*(SPDX-License-Identifier:.+)') lunder = re.compile(r'.*(Licensed under.+)', re.IGNORECASE) for line in srcfull.splitlines(): mat = copy.match(line) if mat != None: src_copy += mat.group(1) mat = spdx.match(line) if mat != None: found_spdx = mat.group(1) mat = lunder.match(line) if mat != None: found_lunder = mat.group(1) if found_lunder: src_lic = found_lunder if found_spdx: src_lic += '\n' + found_spdx with outfile: if format == 'html': gen_html.gen_html(obj, outfile) elif format == 'cdh': gen_cheader.gen_cdefines(obj, outfile, src_lic, src_copy) elif format == 'cth': gen_ctheader.gen_cdefines(obj, outfile, src_lic, src_copy) else: gen_json.gen_json(obj, outfile, format) outfile.write('\n')
def main(): parser = argparse.ArgumentParser( description=__doc__, formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument("treeish", default="HEAD", nargs="?", help="git tree or commit to compare against") parser.add_argument('--version', action='store_true', help='Show version and exit') parser.add_argument('-v', '--verbose', action='store_true', help='Verbose output: ls the output directories') args = parser.parse_args() if args.version: version.show_and_exit(__file__, []) args.treeish = shlex.quote(args.treeish) util_path = os.path.dirname(os.path.realpath(__file__)) repo_root = os.path.abspath(os.path.join(util_path, os.pardir)) os.chdir(repo_root) if not os.path.isdir(os.path.join(repo_root, '.git')): print("Script not in expected location in a git repo", file=sys.stderr) sys.exit(1) # Exit early if there are no diffs between the working tree and # args.treeish. output = subprocess.check_output("git diff " + args.treeish, shell=True) if not output: sys.exit(0) # Create temporary directories in util_path rather than defaulting to /tmp. # /tmp may be small and may may be mounted noexec. tempfile.tempdir = util_path with tempfile.TemporaryDirectory() as tmpdir: tmpdir_basename = os.path.basename(tmpdir) subprocess.check_call("git archive " + args.treeish + " | tar -x -C util/" + tmpdir_basename, shell=True) # Execute commands for working tree, saving output os.chdir(util_path) newoutdir = os.path.join(tmpdir, "newout") os.mkdir(newoutdir) generate_output(newoutdir, args.verbose) # Execute commands for previous revision, saving output os.chdir(os.path.join(tmpdir_basename, "util")) oldoutdir = os.path.join(tmpdir, "oldout") os.mkdir(oldoutdir) generate_output(oldoutdir, args.verbose) # Show diff (if any) os.chdir(tmpdir) # Don't use a checked call because the exit code indicates whether there # is a diff or not, rather than indicating error. subprocess.call('git diff -p --stat --no-index oldout newout', shell=True)
def main(): done_stdin = False parser = argparse.ArgumentParser( prog="i2csvg.py", formatter_class=argparse.RawDescriptionHelpFormatter, description=__doc__, epilog=ep) parser.add_argument('--version', action='store_true', help='Show version and exit') parser.add_argument('-v', '--verbose', action='store_true', help='Verbose output during processing') parser.add_argument('-d', '--debug', action='store_true', help='Include internal representation in output file') parser.add_argument('-t', '--text', action='store_true', help='Include text output in output file') parser.add_argument('-n', '--nosvg', action='store_true', help="Don't include svg in output") parser.add_argument('-f', '--fifodata', action='store_true', help='Data is hexdump of writes to FDATA fifo') parser.add_argument( '-p', '--prefix', action='store', help='Only process lines with this prefix (the prefix is removed)') parser.add_argument( '-m', '--multiout', action='store_true', help='Generate separate output file with .svg extension from inputs') parser.add_argument('-o', '--output', type=argparse.FileType('w'), default=sys.stdout, metavar='file', help='Output file (default stdout)') parser.add_argument('srcfile', nargs='*', metavar='input', default='-', help='source i2c file (default stdin)') args = parser.parse_args() if args.version: version.show_and_exit(__file__, ["Hjson"]) if (args.verbose): log.basicConfig(format="%(levelname)s: %(message)s", level=log.DEBUG) else: log.basicConfig(format="%(levelname)s: %(message)s") outfile = args.output # output will be: # .svg if a single input file and no additional details (debug/text) # or --multiout and no additional details # .html if combining multiple files (not --multiout) or additional details # .txt if --nosvg combiningfiles = (len(args.srcfile) > 1) and not args.multiout extrainfo = args.debug or args.text or combiningfiles if args.nosvg: makehtml = False outext = '.txt' elif extrainfo: makehtml = True outext = '.html' else: makehtml = False outext = '.svg' with outfile: for filename in args.srcfile: if (filename == '-'): if (done_stdin): log.warn("Ignore stdin after first use\n") continue done_stdin = True infile = sys.stdin else: infile = open(filename, 'r', encoding='UTF-8') with infile: log.info("\nFile now " + filename) if args.multiout: outfname = str(PurePath(filename).with_suffix('.svg')) outf = open(outfname, 'w', encoding='UTF-8') else: outf = outfile if makehtml: outf.write("<H2>" + filename + "</H2>") tr = convert.parse_file(infile, args.fifodata, args.prefix) if args.debug: convert.output_debug(outf, tr, '<br>\n' if makehtml else '\n') outf.write('<br>\n' if makehtml else '\n') if args.text: if makehtml: outf.write("<pre>\n") convert.output_text(outf, tr, '<br>\n' if makehtml else '\n') if makehtml: outf.write("</pre>\n") if not args.nosvg: convert.output_svg(outf, tr, makehtml) if args.multiout: outf.close()
def main(): done_stdin = False parser = argparse.ArgumentParser( prog="wavetool", formatter_class=argparse.RawDescriptionHelpFormatter, usage=USAGE, description=__doc__, epilog='defaults or the filename - can be used for stdin/stdout') parser.add_argument('--version', action='store_true', help='Show version and exit') parser.add_argument('-v', '--verbose', action='store_true', help='Verbose output during processing') parser.add_argument('-T', '--testmode', action='store_true', help='Run test with built-in source') parser.add_argument('-o', '--output', type=argparse.FileType('w'), default=sys.stdout, metavar='file', help='Output file (default stdout)') parser.add_argument('srcfile', nargs='*', metavar='input', default='-', help='source wavejson file (default stdin)') args = parser.parse_args() if args.version: version.show_and_exit(__file__, ["Hjson"]) if (args.verbose): log.basicConfig(format="%(levelname)s: %(message)s", level=log.DEBUG) else: log.basicConfig(format="%(levelname)s: %(message)s") outfile = args.output with outfile: if args.testmode: obj = hjson.loads(wavejson) svg0 = wavesvg.convert(obj, 0) svg1 = wavesvg.convert(obj, 1) outfile.write(svg0) outfile.write('<h2>Generate again, should not repeat defs</h2>\n') outfile.write(svg1) else: num = 0 for filename in args.srcfile: if (filename == '-'): if (done_stdin): log.warn("Ignore stdin after first use\n") continue done_stdin = True infile = sys.stdin else: infile = open(filename, 'r', encoding='UTF-8') with infile: obj = hjson.load(infile) log.info("\nFile now " + filename) outfile.write("<H2>" + filename + "</H2>") outfile.write(wavesvg.convert(obj, num)) num += 1