def parse_to_xml(cls, filepath, verbose_error=False): success, lab, out, err = cls.exec_gracefully(filepath, args=["-xml"]) if not success: io.write_result("Failed to parse %s" % filepath, error=True) if verbose_error: io.output(err) else: xml = out + "\n" return xml
def inject(filepath, skiplist=None): filelist = txlparser.get_file_list(filepath) count = len(filelist) for (i, fp) in enumerate(filelist): localname = io.relpath(fp, relative_to=filepath) localname = localname == "." and filepath or localname if localname in skiplist: io.output("Skip file (%s/%s) %s\n" % (i+1, count, localname)) else: io.output("Inject into (%s/%s) %s\n" % (i+1, count, localname)) inject_into_file(fp, localname)
def inject_into_file(filepath, localname): tree = delphiparser.read_file(filepath) trans = transformer.Transformer() trans.listify_stmlist(tree) pairs = trans.inject_writeln(tree) delphiparser.write_file(filepath, tree) for (guid, funcname) in pairs: io.output("%s %s\n %s\n\n" % (guid, localname, funcname))
def prune(executable, guidlistfile): filelist, funcdict = compute_prune_list(executable, guidlistfile) count = sum(map(lambda x: len(x), funcdict.values())) i = 0 for fp in filelist: if fp in funcdict: funclist = sorted(funcdict[fp]) io.output("Prune in %s\n" % fp) for funcname in funclist: i += 1 io.output(" (%s/%s) %s\n" % (i, count, funcname)) prune_in_file(fp, funclist)
def lint(graph): def get_abs_searchpath(searchpath): paths = [] for path in searchpath: if io.path_is_abs(path): paths.append(path) return paths df = graph.rootnode abspaths = get_abs_searchpath(graph.searchpath) notfounds = df.collect_nodes(DelphiFile.NodePredicates.file_notexists) notfounds = map(lambda n: n.filepath, notfounds) exit = 0 if abspaths or notfounds: exit = 1 if abspaths: io.output('Searchpath paths not relative:\n') for path in abspaths: io.output(' * %s\n' % path) if notfounds: io.output('Files not found:\n') for fp in notfounds: io.output(' * %s\n' % fp) return exit
def parse_test(cls, filepath, unparse=False): filelist = get_file_list(filepath) failures = 0 for fp in filelist: success, lab, out, err = cls.exec_gracefully(fp) if not success: failures += 1 elif unparse: s = out[-1] == "\n" and out or out + "\n" open(fp, "w").write(s) localname = io.relpath(fp, relative_to=filepath) localname = localname == "." and filepath or localname io.output("%-9.9s %s\n" % (lab.upper(), localname)) io.output("Processed %s files, %s failed\n" % (len(filelist), failures)) exitcode = failures > 0 and 1 or 0 return exitcode
def show_diff(filepath1, filepath2): graph1 = DelphiGraph.from_file(filepath1) graph2 = DelphiGraph.from_file(filepath2) color_title = ansicolor.magenta color1 = ansicolor.cyan color2 = ansicolor.yellow keyw_def, fstw_def, sndw_def = 10, 30, 30 diffs = DelphiGraph.diff_graphs(graph1, graph2) exit = 0 if diffs: exit = 1 io.output('Showing diff between:\n') io.output(' - %s\n' % color1(filepath1)) io.output(' - %s\n' % color2(filepath2)) for diff in diffs: io.output(color_title('>>> File: %s\n' % diff.name)) keyw = get_len(diff.keys(), keyw_def) for att, val in diff.iteritems(): fst, snd = val fsts = fmt_obj(fst, fstw_def) snds = fmt_obj(snd, sndw_def) fstw = get_len(fsts, fstw_def) sndw = get_len(snds, sndw_def) if fstw <= fstw_def and sndw <= sndw_def: s = show_sidebyside(att, fsts, snds, (keyw,fstw,sndw), (color1,color2)) else: s = show_oneafterother(att, fsts, snds, (keyw,fstw,sndw), (color1,color2)) io.output(s) return exit
def verify(graph): df = graph.rootnode nodes = df.collect_nodes(DelphiFile.NodePredicates.path_not_in(graph.stdlibpath)) # if the file wasn't found during indexing there is no point complaining # about it now nodes = filter(lambda n: n.exists, nodes) fps = map(lambda n: n.filepath, nodes) notfounds = [] exit = 0 for fp in fps: if not io.ifile_exists(os.path.join(graph.abspath, fp)): notfounds.append(fp) if notfounds: exit = 1 io.output('Files not found:\n') for fp in notfounds: io.output(' * %s\n' % fp) return exit
def ls_stdlibpath(graph): for path in graph.stdlibpath: io.output("%s\n" % path)
def ls_searchpath(graph): for path in graph.searchpath: io.output("%s\n" % path)
def ls_abspath(graph): path = dg.abspath io.output("%s\n" % path)
def ls(graph): df = dg.rootnode nodes = df.collect_nodes(DelphiFile.NodePredicates.true) for n in nodes: io.output("%s\n" % n.filepath)
def parse_file(filepath): return TXLParser.parse_to_xml(filepath) if __name__ == "__main__": usage = "%s <path>" % sys.argv[0] optparser = OptionParser(usage=usage) optparser.add_option("-v", "", dest="verbose_error", action="store_true", help="Display verbose errors") optparser.add_option("", "--parsetest", dest="parse_test", action="store_true", help="Test TXL parser on input") optparser.add_option( "", "--parsetest-unparse", dest="parse_test_unparse", action="store_true", help="Parse and unparse using TXL" ) (options, args) = optparser.parse_args() try: filepath = args[0] except IndexError: optparser.print_help() sys.exit(1) if options.parse_test or options.parse_test_unparse: unparse = False if options.parse_test_unparse: unparse = True exitcode = TXLParser.parse_test(filepath, unparse=unparse) sys.exit(exitcode) else: xml = TXLParser.parse_to_xml(filepath, verbose_error=options.verbose_error) if xml: io.output(xml)
def write(msg): if not interactive: io.output(msg)
def output_code(tree): code = unparser.unparse(tree) io.output(code)
def output_tree(tree): tree_fmt = astpp.sprint(tree) codec = compilertools.DelphiCompiler.get_dcc_charset() tree_fmt = tree_fmt.encode(codec) io.output(tree_fmt)