def compare(self, jfp, embedded, generated): emb = embedded.strip().splitlines() gen = generated.strip().splitlines() delta = abs(len(emb) - len(gen)) if delta > LineCountDiff.band: report = io.StringIO() report.write("\n" + "*" * 60) report.write("\n" + ruler(jfp.relative_to(config.example_dir))) report.write("line count difference: {}\n".format(delta)) report.write(ruler("Attached")) report.write(embedded) report.write("\n" + ruler("Generated")) report.write(generated) with LineCountDiff.line_count_report.open('a') as lcr: lcr.write(report.getvalue())
def attachFiles(): """Attach standard and error output to all files""" os.chdir(str(config.example_dir)) test = open("AllFilesWithOutput.txt", 'w') longOutput = open("LongOutput.txt", 'w') for jfp in Path(".").rglob("*.java"): if "gui" in jfp.parts or "swt" in jfp.parts: continue j_main = JavaMain.create(jfp) if j_main: j_main.write_modified_file() test.write(ruler()) test.write(j_main.new_code()) if j_main.long_output: longOutput.write(ruler()) longOutput.write(j_main.new_code())
def compare(self, jfp, embedded, generated): def remove_numbers(lst): lst = [w for w in lst if not re.match("\d+", w)] lst = [w for w in lst if not floatnum.match(w)] return lst emb = SortedSet(remove_numbers(re.split('\W+', embedded))) gen = SortedSet(remove_numbers(re.split('\W+', generated))) delta = emb ^ gen if len(delta) > 1: report = io.StringIO() report.write("\n" + "*" * 60) report.write("\n" + ruler(jfp.relative_to(config.example_dir))) for d in delta: report.write(" {} ".format(d)) report.write("\n" + ruler("Attached")) report.write(embedded) report.write("\n" + ruler("Generated")) report.write(generated) with WordContentDiff.content_report.open('a') as lcr: lcr.write(report.getvalue())
def extractResults(): """Test extraction of all results""" os.chdir(str(config.example_dir)) with Path("AttachedResults.txt").open('w') as results: for jfp in Path(".").rglob("*.java"): j_main = JavaMain.create(jfp) if j_main: results.write(ruler(jfp)) outline = j_main.j_file.output_line if outline: results.write(outline + "\n") results.write(j_main.result) os.system("subl AttachedResults.txt")
def __repr__(self): result = "\n" + ruler(self.javaFilePath, "=") + "\n" with self.javaFilePath.open() as jf: for line in jf.readlines(): if "/* Output:" in line: result += line + "\n" break else: result += "no prior /* Output:\n" if self.old_output: result += ruler("Previous Output") result += self.old_output + "\n\n" else: result += ruler("No Previous Output") result += ruler("New Output") result += self.new_output + "\n\n" result += ruler("Difference: {}".format(self.difference), '+') + "\n" if self.difference == 1.0: return '.' return result
def findExceptionsFromRun(): """ Put the exceptions produced by runall.ps1 into errors.txt """ errors = [r for r in [Result.create(jfp) for jfp in RunFiles.base.rglob("*.java")] if r and r.errFilePath.stat().st_size] assert len(errors), "Must run runall.ps1 first" with (examplePath / "errors.txt").open('w') as errors_txt: for e in errors: with e.errFilePath.open() as errfile: errors_txt.write("\n" + ruler(e.errFilePath, width=80)) errors_txt.write(errfile.read()) errors_txt.write("<-:->") showProblemErrors()
def findExceptionsFromRun(): """ Put the exceptions produced by runall.ps1 into errors.txt """ errors = [ r for r in [Result.create(jfp) for jfp in RunFiles.base.rglob("*.java")] if r and r.errFilePath.stat().st_size ] assert len(errors), "Must run runall.ps1 first" with (examplePath / "errors.txt").open('w') as errors_txt: for e in errors: with e.errFilePath.open() as errfile: errors_txt.write("\n" + ruler(e.errFilePath, width=80)) errors_txt.write(errfile.read()) errors_txt.write("<-:->") showProblemErrors()
def purify_code_listings(): """ Ensure all characters in code listings are pure ascii """ def pure_ascii(s): return all(ord(c) < 128 for c in s) badcode = build_dir / "bad_code_chars.txt" with badcode.open('w', encoding="utf8") as weird: for md in markdown_dir.glob("[0-9][0-9]_*.md"): with md.open(encoding="utf8") as chapter: for codeblock in code_listings.findall(chapter.read()): for line in codeblock.splitlines(): if pure_ascii(line): continue else: print(line.encode("utf8")) weird.write(ruler(md.name)) weird.write(line + "\n") os.system("subl {}".format(badcode))
def compare_output(): """Compare attached and newly-generated output""" TODO = """ - Strip out date/time information using regex before comparing - Make it possible to compare/append on a single file - Could also compare number of lines """ ratio_target = 1.0 memlocation = re.compile("@[0-9a-z]{5,7}") reports = list() line_count_diff = LineCountDiff() word_content_diff = WordContentDiff() output_comparison = config.example_dir / "OutputComparisons.txt" # This is support to make it easy to add to exclude_files: compare_exclusions = config.example_dir / "CompareExclusions.txt" for jfp in config.example_dir.rglob("*.java"): if "gui" in jfp.parts or "swt" in jfp.parts: continue if jfp.name in exclude_files: continue generated = generated_output(jfp) if generated is None: continue embedded = embedded_output(jfp) line_count_diff.compare(jfp, embedded, generated) gen_stripped = memlocation.sub("", generated) gen_stripped = stripdates(gen_stripped) emb_stripped = memlocation.sub("", embedded) emb_stripped = stripdates(emb_stripped) word_content_diff.compare(jfp, emb_stripped, gen_stripped) comp = SequenceMatcher(None, emb_stripped, gen_stripped) ratio = comp.ratio() if ratio < ratio_target: print(jfp.relative_to(config.example_dir)) print("ratio: {}\n".format(ratio)) report = io.StringIO() report.write("\n" + "*" * 60) report.write("\n" + ruler(jfp.relative_to(config.example_dir))) report.write("ratio: {}\n".format(ratio)) report.write(ruler("Attached")) report.write(embedded) report.write("\n" + ruler("Generated")) report.write(generated) result = (ratio, report.getvalue(), str(jfp.relative_to(config.example_dir))) reports.append(result) reports = sorted(reports) with output_comparison.open('w') as comparisons: for report in reports: comparisons.write(report[1]) with compare_exclusions.open('w') as exclusions: for report in reports: exclusions.write('r"' + report[2] + "\",\n") if compare_exclusions.exists(): os.system("subl {}".format(compare_exclusions)) if output_comparison.exists(): os.system("subl {}".format(output_comparison)) if LineCountDiff.line_count_report.exists(): os.system("subl {}".format(LineCountDiff.line_count_report)) if WordContentDiff.content_report.exists(): os.system("subl {}".format(WordContentDiff.content_report))