OnJava8-Examples/tools/AttachResults.py

468 lines
16 KiB
Python
Raw Normal View History

2015-06-05 10:39:12 -07:00
#! py -3
"""
Append output and error files to Java files
"""
TODO = """
2015-06-05 14:18:52 -07:00
- Test to make sure that None files indeed have no output
2015-06-14 22:12:43 -07:00
- Collect all tests under single flag
2015-06-05 10:39:12 -07:00
"""
from pathlib import Path
import pprint
import textwrap
import os, sys, re
from itertools import chain
2015-06-10 17:12:10 -07:00
from sortedcontainers import SortedSet
2015-06-05 10:39:12 -07:00
from betools import CmdLine, visitDir, ruler, head
2015-06-05 14:18:52 -07:00
maxlinewidth = 59
2015-06-15 00:18:36 -07:00
examplePath = Path(r"C:\Users\Bruce\Dropbox\___OnJava\ExtractedExamples")
2015-06-05 10:39:12 -07:00
2015-06-06 15:07:33 -07:00
class JavaMain:
2015-06-06 18:27:39 -07:00
max_output_length = 32 # lines beyond which we flag this
2015-06-06 15:07:33 -07:00
maindef = re.compile("public\s+static\s+void\s+main")
2015-06-10 17:12:10 -07:00
leader = "_" * 8
ellipses = [leader + leader.join(["..."] * 4) + leader]
# ellipses = ["[...]".center(14, '_') * 4]
2015-06-06 15:07:33 -07:00
class JFile:
@staticmethod
def with_main(javaFilePath):
with javaFilePath.open() as doc:
code = doc.read()
2015-06-06 18:27:39 -07:00
if JavaMain.maindef.search(code) or "{Exec:" in code:
2015-06-06 15:07:33 -07:00
return JavaMain.JFile(javaFilePath, code)
return None
def __init__(self, javaFilePath, code):
self.javaFilePath = javaFilePath
self.code = code
self.lines = self.code.splitlines()
self.output_line = None
for line in self.lines:
if "} /* Output:" in line:
self.output_line = line
self.newcode = ""
2015-06-05 14:18:52 -07:00
@staticmethod
2015-06-06 15:07:33 -07:00
def create(javaFilePath):
j_file = JavaMain.JFile.with_main(javaFilePath)
if j_file is None:
return None
if "{ValidateByHand}" in j_file.code:
return None
if "/* Output: (None) */" in j_file.code:
return None
2015-06-06 18:27:39 -07:00
if "/* Output: (Execute to see)" in j_file.code:
return None
2015-06-06 15:07:33 -07:00
outfile = javaFilePath.with_name(javaFilePath.stem + "-output.txt")
errfile = javaFilePath.with_name(javaFilePath.stem + "-erroroutput.txt")
if outfile.exists() or errfile.exists():
return JavaMain(javaFilePath, j_file, outfile, errfile)
2015-06-05 14:18:52 -07:00
return None
2015-06-06 15:07:33 -07:00
def __init__(self, javaFilePath, j_file, outfile, errfile):
self.javaFilePath = javaFilePath
self.j_file = j_file
self.outfile = outfile
self.errfile = errfile
self.first_and_last = None
self.first_lines = None
2015-06-06 18:27:39 -07:00
self.long_output = False
2015-06-06 15:07:33 -07:00
ol = self.j_file.output_line
if ol:
if "(First and last" in ol:
self.first_and_last = int(ol.partition("(First and last")[2].split()[0])
elif "(First" in ol:
self.first_lines = int(ol.partition("(First")[2].split()[0])
result =""
if outfile.exists():
with outfile.open() as f:
out = f.read().strip()
if out:
if self.first_and_last:
lines = out.splitlines()
out = "\n".join(lines[:self.first_and_last] + JavaMain.ellipses + lines[-self.first_and_last:])
elif self.first_lines:
lines = out.splitlines()
2015-06-10 17:12:10 -07:00
out = "\n".join(lines[:self.first_lines] + [" " * 18 + "..."])
2015-06-06 15:07:33 -07:00
result += out + "\n"
if errfile.exists(): # Always include all of errfile
with errfile.open() as f:
err = f.read().strip()
if err:
result += "___[ Error Output ]___\n"
result += err
self.result = JavaMain.wrapOutput(result) + "\n"
2015-06-06 18:27:39 -07:00
if len(self.result.splitlines()) > JavaMain.max_output_length:
self.long_output = True
2015-06-06 15:07:33 -07:00
for line in self.j_file.lines:
if line.startswith("} ///:~"):
self.j_file.newcode += "} /* Output:\n"
self.j_file.newcode += self.result + "*///:~\n"
break
if line.startswith("} /* Output:"):
line = line.partition("*///:~")[0]
self.j_file.newcode += line + "\n"
self.j_file.newcode += self.result + "*///:~\n"
break
else:
self.j_file.newcode += line + "\n"
def new_code(self):
return self.j_file.newcode
@staticmethod
def wrapOutput(output):
2015-06-10 17:12:10 -07:00
"""
Wrap to line limit and perform other fixups for display and comparison
"""
output = output.replace('\0', "NUL")
2015-06-06 15:07:33 -07:00
lines = output.splitlines()
result = []
for line in lines:
result += textwrap.wrap(line.rstrip(), width=maxlinewidth)
return "\n".join(result)
2015-06-06 18:27:39 -07:00
def write_modified_file(self):
with self.j_file.javaFilePath.open('w') as modified:
modified.write(self.j_file.newcode)
2015-06-06 15:07:33 -07:00
2015-06-05 10:39:12 -07:00
2015-06-05 14:18:52 -07:00
@CmdLine('o')
def allOutputTagLines():
"""Shows all lines starting with } /*"""
allvariations = set()
os.chdir(str(examplePath))
for jfp in Path(".").rglob("*.java"):
with jfp.open() as code:
for line in code.readlines():
if line.startswith("} /*"):
allvariations.add(line)
pprint.pprint(allvariations)
2015-06-05 10:39:12 -07:00
@CmdLine('t')
def outputTagTypes():
"""Show different output tag variations"""
types = set()
os.chdir(str(examplePath))
for jfp in Path(".").rglob("*.java"):
2015-06-06 15:07:33 -07:00
jf = JavaMain.JFile.with_main(jfp)
2015-06-05 14:18:52 -07:00
if jf is None:
continue
2015-06-06 15:07:33 -07:00
if jf.output_line:
types.add(jf.output_line)
2015-06-05 10:39:12 -07:00
pprint.pprint(types)
2015-06-06 15:07:33 -07:00
2015-06-05 14:18:52 -07:00
@CmdLine('e')
def extractResults():
"""Test extraction of all results"""
os.chdir(str(examplePath))
with Path("AttachedResults.txt").open('w') as results:
for jfp in Path(".").rglob("*.java"):
2015-06-06 15:07:33 -07:00
j_main = JavaMain.create(jfp)
if j_main:
2015-06-05 14:18:52 -07:00
results.write(ruler(jfp))
2015-06-06 15:07:33 -07:00
outline = j_main.j_file.output_line
2015-06-05 14:18:52 -07:00
if outline:
results.write(outline + "\n")
2015-06-06 15:07:33 -07:00
results.write(j_main.result)
os.system("subl AttachedResults.txt")
2015-06-05 14:18:52 -07:00
2015-06-10 17:12:10 -07:00
#@CmdLine('n')
2015-06-05 14:18:52 -07:00
def noOutputFixup():
2015-06-06 15:07:33 -07:00
"""Attach "Output: (None)" lines to empty output files"""
2015-06-05 14:18:52 -07:00
os.chdir(str(examplePath))
2015-06-06 15:07:33 -07:00
# test = open("test.txt", 'w')
2015-06-05 14:18:52 -07:00
for jfp in Path(".").rglob("*.java"):
2015-06-06 15:07:33 -07:00
if "gui" in jfp.parts or "swt" in jfp.parts:
continue
jf = JavaMain.JFile.with_main(jfp)
2015-06-05 14:18:52 -07:00
if jf is None:
continue
2015-06-06 15:07:33 -07:00
if "{ValidateByHand}" in jf.code:
continue
if not jf.output_line:
if JavaMain.create(jfp):
continue
2015-06-05 14:18:52 -07:00
newcode = ""
for line in jf.lines:
if line.startswith("} ///:~"):
newcode += "} /* Output: (None) *///:~\n"
else:
newcode += line + "\n"
2015-06-06 15:07:33 -07:00
with jfp.open('w') as f:
f.write(newcode)
os.system("subl {}".format(jfp))
# test.write(ruler(jfp))
# test.write(newcode)
2015-06-05 14:18:52 -07:00
2015-06-06 18:27:39 -07:00
@CmdLine('v')
def viewAttachedFiles():
2015-06-09 11:43:40 -07:00
"""Sublime edit all files containing output in this directory and below"""
2015-06-06 18:27:39 -07:00
for java in Path(".").rglob("*.java"):
with java.open() as codefile:
code = codefile.read()
if "/* Output:" in code:
if "/* Output: (None)" in code:
continue
if "/* Output: (Execute to see)" in code:
continue
for n, line in enumerate(code.splitlines()):
if "/* Output:" in line:
2015-06-09 11:31:18 -07:00
# os.system("subl {}:{}".format(java, n))
os.system("subl {}".format(java))
2015-06-06 18:27:39 -07:00
continue
2015-06-10 17:12:10 -07:00
@CmdLine('x')
def showNulBytesInOutput():
"""Look for NUL bytes in output files`"""
os.chdir(str(examplePath))
for normal in Path(".").rglob("*-output.txt"):
with normal.open() as codeFile:
if "\0" in codeFile.read():
print(normal)
for errors in Path(".").rglob("*-erroroutput.txt"):
with errors.open() as codeFile:
if "\0" in codeFile.read():
print(normal)
2015-06-09 11:31:18 -07:00
@CmdLine('s')
def showJavaFiles():
2015-06-09 11:43:40 -07:00
"""Sublime edit all java files in this directory and below"""
2015-06-09 11:31:18 -07:00
for java in Path(".").rglob("*.java"):
os.system("subl {}".format(java))
2015-06-10 17:12:10 -07:00
# @CmdLine('w')
def boldWords():
"""
Create list of bolded words to be used as a Word dictionary
"""
from bs4 import BeautifulSoup
import codecs
import string
clean = lambda dirty: ''.join(filter(string.printable.__contains__, dirty))
def flense(word):
word = clean(word)
word = word.split('(')[0]
word = word.split('[')[0]
return word.strip()
os.chdir(str(examplePath / ".."))
spelldict = SortedSet()
2015-06-15 00:18:36 -07:00
with codecs.open(str(Path("OnJava.htm")),'r', encoding='utf-8', errors='ignore') as book:
2015-06-10 17:12:10 -07:00
soup = BeautifulSoup(book.read())
for b in soup.find_all("b"):
text = (" ".join(b.text.split())).strip()
if " " in text:
continue
text = flense(text)
if text:
spelldict.add(text)
with Path("BoldedWords.txt").open('w') as boldwords:
for word in spelldict:
if len(word):
if word[0] in string.ascii_letters:
boldwords.write(word + "\n")
2015-06-09 11:31:18 -07:00
@CmdLine('b')
def blankOutputFiles():
"""Show java files with expected output where there is none"""
find_output = re.compile(r"/\* Output:(.*)\*///:~", re.DOTALL)
os.chdir(str(examplePath))
for java in Path(".").rglob("*.java"):
with java.open() as codeFile:
output = find_output.search(codeFile.read())
if output:
# print(output.group(1))
if not output.group(1).strip():
print(java)
2015-06-05 14:18:52 -07:00
2015-06-09 11:43:40 -07:00
@CmdLine('u')
def unexpectedOutput():
"""Show java files with output where none was expected"""
os.chdir(str(examplePath))
for java in Path(".").rglob("*.java"):
with java.open() as codeFile:
if "/* Output: (None) */" in codeFile.read():
outfile = java.with_name(java.stem + "-output.txt")
errfile = java.with_name(java.stem + "-erroroutput.txt")
if outfile.exists():
if outfile.stat().st_size:
print("Unexpected output: {}".format(java))
if errfile.exists():
if errfile.stat().st_size:
print("Unexpected error output: {}".format(java))
2015-06-10 17:12:10 -07:00
exclude_files = [
r"concurrency\ActiveObjectDemo.java",
r"concurrency\AtomicityTest.java",
r"concurrency\CachedThreadPool.java",
r"concurrency\CountDownLatchDemo.java",
r"concurrency\DaemonFromFactory.java",
r"concurrency\DeadlockingDiningPhilosophers.java",
r"concurrency\FastSimulation.java",
r"concurrency\FixedDiningPhilosophers.java",
r"concurrency\FixedThreadPool.java",
r"concurrency\MoreBasicThreads.java",
r"concurrency\NIOInterruption.java",
r"concurrency\SelfManaged.java",
r"concurrency\SemaphoreDemo.java",
r"concurrency\SimpleDaemons.java",
r"concurrency\SleepingTask.java",
r"concurrency\ThreadLocalVariableHolder.java",
r"patterns\PaperScissorsRock.java",
r"patterns\recyclea\RecycleA.java",
r"patterns\visitor\BeeAndFlowers.java",
r"concurrency\EvenGenerator.java",
r"concurrency\GreenhouseScheduler.java",
r"concurrency\OrnamentalGarden.java",
r"concurrency\PipedIO.java",
r"concurrency\SimplePriorities.java",
r"concurrency\SimpleThread.java",
r"concurrency\ThreadVariations.java",
r"generics\DynamicProxyMixin.java",
r"logging\LoggingLevelManipulation.java",
r"logging\SimpleFilter.java",
r"annotations\AtUnitExample4.java",
r"concurrency\BankTellerSimulation.java",
r"concurrency\CarBuilder.java",
r"concurrency\ListComparisons.java",
r"concurrency\MapComparisons.java",
r"concurrency\ReaderWriterList.java",
r"concurrency\restaurant2\RestaurantWithQueues.java",
r"generics\Mixins.java",
r"io\LockingMappedFiles.java",
r"logging\ConfigureLogging.java",
r"logging\LoggingLevels.java",
r"operators\HelloDate.java",
r"annotations\AtUnitComposition.java",
r"annotations\AtUnitExample3.java",
r"annotations\AtUnitExternalTest.java",
r"annotations\HashSetTest.java",
r"annotations\UseCaseTracker.java",
r"concurrency\Interrupting.java",
r"concurrency\SerialNumberChecker.java",
r"concurrency\SimpleMicroBenchmark.java",
r"concurrency\SynchronizationComparisons.java",
r"concurrency\SyncObject.java",
r"containers\ListPerformance.java",
r"io\Logon.java",
r"logging\CustomHandler.java",
r"object\HelloDate.java",
r"annotations\AtUnitExample1.java",
r"annotations\AtUnitExample2.java",
r"concurrency\ExchangerDemo.java",
r"concurrency\ExplicitCriticalSection.java",
r"concurrency\Restaurant.java",
r"containers\MapPerformance.java",
r"containers\SetPerformance.java",
r"exceptions\LoggingExceptions.java",
r"logging\InfoLogging.java",
r"logging\InfoLogging2.java",
r"logging\LogToFile.java",
r"logging\LogToFile2.java",
r"logging\MultipleHandlers.java",
r"logging\MultipleHandlers2.java",
r"annotations\AtUnitExample5.java",
r"concurrency\Daemons.java",
r"concurrency\HorseRace.java",
r"concurrency\ToastOMatic.java",
r"enums\ConstantSpecificMethod.java",
r"exceptions\LoggingExceptions2.java",
r"io\MakeDirectories.java",
r"io\MappedIO.java",
r"io\PreferencesDemo.java",
r"logging\PrintableLogRecord.java",
r"references\Compete.java",
# Keep an eye on:
r"strings\JGrep.java",
]
@CmdLine('c')
def compare_output():
"""Compare attached and newly-generated output"""
TODO = """
- Could also compare number of lines
"""
ratio_target = 1.0
from difflib import SequenceMatcher
os.chdir(str(examplePath))
def generated_output(jfp):
j_main = JavaMain.create(jfp)
if not j_main:
return None
return j_main.result.strip()
def embedded_output(jfp):
find_output = re.compile(r"/\* (Output:.*)\*///:~", re.DOTALL)
with jfp.open() as java:
output = find_output.search(java.read())
assert output, "No embedded output: in {}".format(jfp)
return "\n".join(output.group(1).strip().splitlines()[1:])
if Path("CompareExclusions.txt").is_file():
Path("CompareExclusions.txt").unlink()
with Path("OutputComparisons.txt").open('w') as comparisions:
for jfp in Path(".").rglob("*.java"):
if "gui" in jfp.parts or "swt" in jfp.parts:
continue
if str(jfp) in exclude_files:
continue
generated = generated_output(jfp)
if generated is None:
continue
embedded = embedded_output(jfp)
comp = SequenceMatcher(None, embedded, generated)
ratio = comp.ratio()
if ratio < ratio_target:
print(jfp)
print("ratio: {}\n".format(ratio))
comparisions.write("\n" + ruler(jfp))
comparisions.write("ratio: {}\n".format(ratio))
comparisions.write(ruler("Attached"))
comparisions.write(embedded)
comparisions.write("\n" + ruler("Generated"))
comparisions.write(generated)
with Path("CompareExclusions.txt").open('a') as exclusions:
exclusions.write('r"' + str(jfp) + "\",\n")
if Path("CompareExclusions.txt").is_file():
os.system("ed CompareExclusions.txt")
if Path("OutputComparisons.txt").is_file():
os.system("ed OutputComparisons.txt")
2015-06-05 10:39:12 -07:00
@CmdLine('a')
def attachFiles():
"""Attach standard and error output to all files"""
os.chdir(str(examplePath))
2015-06-06 18:27:39 -07:00
test = open("AllFilesWithOutput.txt", 'w')
longOutput = open("LongOutput.txt", 'w')
2015-06-05 10:39:12 -07:00
for jfp in Path(".").rglob("*.java"):
2015-06-06 15:07:33 -07:00
if "gui" in jfp.parts or "swt" in jfp.parts:
2015-06-05 14:18:52 -07:00
continue
2015-06-06 15:07:33 -07:00
j_main = JavaMain.create(jfp)
if j_main is None:
continue
2015-06-06 18:27:39 -07:00
j_main.write_modified_file()
2015-06-06 15:07:33 -07:00
test.write(ruler())
test.write(j_main.new_code())
2015-06-06 18:27:39 -07:00
if j_main.long_output:
longOutput.write(ruler())
longOutput.write(j_main.new_code())
2015-06-10 17:12:10 -07:00
# os.system("subl AllFilesWithOutput.txt")
# os.system("subl LongOutput.txt")
2015-06-05 10:39:12 -07:00
if __name__ == '__main__': CmdLine.run()
2015-06-06 15:07:33 -07:00