OnJava8-Examples/Validate.py

154 lines
4.8 KiB
Python
Raw Normal View History

2015-05-07 00:32:46 -07:00
#! py -3
from pathlib import Path
2015-05-07 10:54:54 -07:00
import pprint
2015-05-07 17:22:48 -07:00
import os, sys
from contextlib import contextmanager
2015-05-07 10:54:54 -07:00
2015-05-07 17:22:48 -07:00
# Powershell: https://gist.github.com/diyan/2850866
2015-05-07 10:54:54 -07:00
class Flags:
discard = ["{Requires:"]
def __init__(self, lines):
self.flaglines = []
for line in lines:
if line.startswith("//"):
self.flaglines.append(line)
else:
break # Only capture top block
self.flaglines = [line for line in self.flaglines if line.startswith("// {")]
self.flaglines = [line for line in self.flaglines if not [d for d in Flags.discard if d in line]]
self.flags = dict()
for flag in self.flaglines:
flag = flag[flag.index("{") + 1 : flag.index("}")].strip()
if ":" in flag:
fl, arg = flag.split(":")
fl = fl.strip()
arg = arg.strip()
self.flags[fl] = arg
else:
self.flags[flag] = None # Make an entry, but no arg
2015-05-07 17:22:48 -07:00
def __contains__(self, elt):
return elt in self.flags
2015-05-07 10:54:54 -07:00
def __repr__(self):
return pprint.pformat(self.flags)
def __len__(self):
return len(self.flaglines)
def keys(self):
return {key for key in self.flags.keys()}
def values(self):
return str(self.flags.values())
2015-05-07 11:30:19 -07:00
def jvm_args(self):
return self.flags["JVMArgs"] if "JVMArgs" in self.flags else ""
def cmd_args(self):
return " " + self.flags["Args"] if "Args" in self.flags else ""
2015-05-07 00:32:46 -07:00
class RunnableFile:
2015-05-07 10:54:54 -07:00
2015-05-07 00:32:46 -07:00
def __init__(self, path, body):
self.path = path
self.name = path.stem
2015-05-07 10:54:54 -07:00
self.relative = path.relative_to(RunFiles.base)
2015-05-07 00:32:46 -07:00
self.body = body
self.lines = body.splitlines()
2015-05-07 10:54:54 -07:00
self.flags = Flags(self.lines)
2015-05-07 11:30:19 -07:00
self._package = ""
2015-05-07 00:32:46 -07:00
for line in self.lines:
if line.startswith("package "):
2015-05-07 11:30:19 -07:00
self._package = line.split("package ")[1].strip()[:-1]
if self._package.replace('.', '/') not in self.lines[0]:
self._package = ""
2015-05-07 17:22:48 -07:00
def __contains__(self, elt):
return elt in self.flags
2015-05-07 00:32:46 -07:00
def __repr__(self):
2015-05-07 17:22:48 -07:00
return str(self.relative) + ": " + self.name
2015-05-07 00:32:46 -07:00
2015-05-07 11:30:19 -07:00
def package(self):
return self._package + '.' if self._package else ''
2015-05-07 00:32:46 -07:00
2015-05-07 17:22:48 -07:00
def rundir(self):
"Directory to change to before running the command"
return self.path.parent
def javaArguments(self):
return self.flags.jvm_args() + self.package() + self.name + self.flags.cmd_args()
2015-05-07 00:32:46 -07:00
def runCommand(self):
2015-05-07 17:22:48 -07:00
return "java " + self.javaArguments()
2015-05-07 00:32:46 -07:00
2015-05-07 10:54:54 -07:00
class RunFiles:
2015-05-07 17:22:48 -07:00
# RunFirst is temporary?
not_runnable = ["RunByHand", "TimeOutDuringTesting", "CompileTimeError", 'TimeOut', 'RunFirst']
skip_dirs = ["gui", "swt"]
2015-05-07 11:30:19 -07:00
base = Path(".")
2015-05-07 10:54:54 -07:00
def __init__(self):
self.runFiles = []
for java in RunFiles.base.rglob("*.java"):
with java.open() as code:
body = code.read()
if "static void main(String[] args)" in body:
self.runFiles.append(RunnableFile(java, body))
2015-05-07 17:22:48 -07:00
self.runFiles = [f for f in self.runFiles if not [nr for nr in self.not_runnable if nr in f]]
self.runFiles = [f for f in self.runFiles if not [nd for nd in self.skip_dirs if nd in f.path.parts[0]]]
2015-05-07 10:54:54 -07:00
def allFlagKeys(self):
flagkeys = set()
for f in [f for f in self.runFiles if f.flags]:
[flagkeys.add(key) for key in f.flags.keys()]
return flagkeys
def allFlagValues(self):
return [f.flags.values() for f in self.runFiles if f.flags if f.flags.values()]
def allFlags(self):
return [f.flags for f in self.runFiles if f.flags]
def runCommands(self):
return [f.runCommand() for f in self.runFiles]
2015-05-07 00:32:46 -07:00
2015-05-07 17:22:48 -07:00
def runData(self):
return "\n".join(["[{}] {}".format(f.rundir(), f.runCommand()) for f in self.runFiles])
def __iter__(self):
return iter(self.runFiles)
@contextmanager
def visitDir(d):
d = str(d)
old = os.getcwd()
os.chdir(d)
yield d
os.chdir(old)
2015-05-07 10:54:54 -07:00
if __name__ == '__main__':
2015-05-07 11:30:19 -07:00
assert Path.cwd().stem is "ExtractedExamples"
2015-05-07 10:54:54 -07:00
runFiles = RunFiles()
2015-05-07 17:22:48 -07:00
startDir = os.getcwd()
# [print(f, f.flags) for f in runFiles]
# sys.exit()
with open("runall.ps1", 'w') as ps:
for rf in runFiles:
with visitDir(rf.rundir()):
ps.write("cd {}\n".format(os.getcwd()))
ps.write('Start-Process -FilePath "java.exe" -ArgumentList "{}" -NoNewWindow -RedirectStandardOutput {}-output.txt -RedirectStandardError {}-erroroutput.txt \n'.format(rf.javaArguments(), rf.name, rf.name))
ps.write('Write-Host [{}] {}\n'.format(rf.relative, rf.name))
ps.write("cd {}\n".format(startDir))
# pprint.pprint(runFiles.runCommands())
2015-05-07 00:32:46 -07:00