Skip to content

Commit 11d5437

Browse files
committed
Tooling improvements
1 parent edfd973 commit 11d5437

3 files changed

Lines changed: 82 additions & 24 deletions

File tree

tools/Examples.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def __init__(self, codeFile):
159159
self.cmdargs = line.split("{Args:")[1].strip()
160160
self.cmdargs = self.cmdargs.rsplit("}", 1)[0]
161161

162-
self.runbyhand = "{RunByHand}" in self.codeFile.code
162+
self.validatebyhand = "{ValidateByHand}" in self.codeFile.code
163163

164164
self.exclude = None
165165
if "{CompileTimeError}" in self.codeFile.code:
@@ -208,7 +208,10 @@ def dirPath(self):
208208

209209
def arguments(self):
210210
if self.cmdargs:
211-
return """arguments="%s" """ % self.cmdargs
211+
if '"' in self.cmdargs:
212+
return """arguments='%s' """ % self.cmdargs
213+
else:
214+
return """arguments="%s" """ % self.cmdargs
212215
return ""
213216

214217
def failOnError(self):
@@ -306,7 +309,7 @@ def checkPackages(self):
306309
def makeBuildFile(self):
307310
buildFile = startBuild % (self.dir.name, " ".join(self.excludes))
308311
for cf in self.code_files:
309-
if any([cf.name + ".java" in f for f in self.excludes]) or cf.options.runbyhand:
312+
if any([cf.name + ".java" in f for f in self.excludes]) or cf.options.validatebyhand:
310313
continue
311314
buildFile += cf.run_command()
312315
buildFile += endBuild
@@ -349,6 +352,7 @@ def extractAndCreateBuildFiles():
349352
with open("run.bat", 'w') as run:
350353
run.write(r"python ..\Validate.py -p" + "\n")
351354
run.write(r"powershell .\runall.ps1" + "\n")
355+
run.write(r"python ..\Validate.py -e" + "\n")
352356

353357
@CmdLine('g')
354358
def generateAntClean():

tools/Validate.py

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
from collections import defaultdict
1515
from betools import CmdLine, visitDir, ruler, head
1616

17+
examplePath = Path(r"C:\Users\Bruce\Dropbox\__TIJ4-ebook\ExtractedExamples")
18+
1719
maindef = re.compile("public\s+static\s+void\s+main")
1820

1921
###############################################################################
@@ -35,7 +37,7 @@ def __init__(self, lines):
3537
self.flaglines = [line for line in self.flaglines if not [d for d in Flags.discard if d in line]]
3638
self.flags = dict()
3739
for flag in self.flaglines:
38-
flag = flag[flag.index("{") + 1 : flag.index("}")].strip()
40+
flag = flag[flag.index("{") + 1 : flag.rfind("}")].strip()
3941
if ":" in flag:
4042
fl, arg = flag.split(":")
4143
fl = fl.strip()
@@ -60,7 +62,7 @@ def values(self):
6062
return str(self.flags.values())
6163

6264
def jvm_args(self):
63-
return self.flags["JVMArgs"] if "JVMArgs" in self.flags else ""
65+
return self.flags["JVMArgs"] + " " if "JVMArgs" in self.flags else ""
6466

6567
def cmd_args(self):
6668
return " " + self.flags["Args"] if "Args" in self.flags else ""
@@ -82,6 +84,9 @@ def __init__(self, path, body):
8284
self._package = line.split("package ")[1].strip()[:-1]
8385
if self._package.replace('.', '/') not in self.lines[0]:
8486
self._package = ""
87+
self.main = self.name
88+
if "main" in self.flags:
89+
self.main = self.flags.flags["main"]
8590

8691
def __contains__(self, elt):
8792
return elt in self.flags
@@ -97,7 +102,7 @@ def rundir(self):
97102
return self.path.parent
98103

99104
def javaArguments(self):
100-
return self.flags.jvm_args() + self.package() + self.name + self.flags.cmd_args()
105+
return self.flags.jvm_args() + self.package() + self.main + self.flags.cmd_args()
101106

102107
def runCommand(self):
103108
return "java " + self.javaArguments()
@@ -106,7 +111,7 @@ def runCommand(self):
106111

107112
class RunFiles:
108113
# RunFirst is temporary?
109-
not_runnable = ["RunByHand", "TimeOutDuringTesting", "CompileTimeError", 'TimeOut', 'RunFirst']
114+
not_runnable = ["ValidateByHand", "TimeOutDuringTesting", "CompileTimeError", 'TimeOut', 'RunFirst']
110115
skip_dirs = ["gui", "swt"]
111116

112117
base = Path(".")
@@ -152,22 +157,28 @@ def createPowershellScript():
152157
"""
153158
Create Powershell Script to run all programs and capture the output
154159
"""
155-
assert Path.cwd().stem is "ExtractedExamples"
160+
os.chdir(str(examplePath))
156161
runFiles = RunFiles()
157162
startDir = os.getcwd()
158163
with open("runall.ps1", 'w') as ps:
159164
ps.write('''Start-Process -FilePath "ant" -ArgumentList "build" -NoNewWindow -Wait \n\n''')
160165
for rf in runFiles:
161166
with visitDir(rf.rundir()):
167+
argquote = '"'
168+
if '"' in rf.javaArguments() or '$' in rf.javaArguments():
169+
argquote = "'"
162170
pstext = """\
163171
Start-Process
164172
-FilePath "java.exe"
165-
-ArgumentList "{}"
173+
-ArgumentList {}{}{}
166174
-NoNewWindow
167175
-RedirectStandardOutput {}-output.txt
168176
-RedirectStandardError {}-erroroutput.txt
169-
""".format(rf.javaArguments(), rf.name, rf.name)
177+
""".format(argquote, rf.javaArguments(), argquote, rf.name, rf.name)
170178
pstext = textwrap.dedent(pstext).replace('\n', ' ')
179+
if "ThrowsException" in rf:
180+
pstext += " -Wait\n"
181+
pstext += "Add-Content {}-erroroutput.txt '---[ Exception is Expected ]---'".format(rf.name)
171182
ps.write("cd {}\n".format(os.getcwd()))
172183
ps.write(pstext + "\n")
173184
ps.write('Write-Host [{}] {}\n'.format(rf.relative, rf.name))
@@ -343,16 +354,34 @@ def fillInUnexcludedOutput():
343354
@CmdLine("e")
344355
def findExceptionsFromRun():
345356
"""
346-
Find all the exceptions produced by runall.ps1
357+
Put the exceptions produced by runall.ps1 into errors.txt
347358
"""
348359
errors = [r for r in [Result.create(jfp) for jfp in RunFiles.base.rglob("*.java")]
349360
if r and r.errFilePath.stat().st_size]
350361
assert len(errors), "Must run runall.ps1 first"
351-
for e in errors:
352-
with e.errFilePath.open() as errfile:
353-
head(e.errFilePath, "#")
354-
print(errfile.read())
355-
head()
362+
with (examplePath / "errors.txt").open('w') as errors_txt:
363+
for e in errors:
364+
with e.errFilePath.open() as errfile:
365+
errors_txt.write("\n" + ruler(e.errFilePath, width=80))
366+
errors_txt.write(errfile.read())
367+
errors_txt.write("<-:->")
368+
showProblemErrors()
369+
370+
@CmdLine("b")
371+
def showProblemErrors():
372+
"""
373+
Show unexpected errors inside errors.txt
374+
"""
375+
with (examplePath / "errors.txt").open() as errors_txt:
376+
for err in errors_txt.read().split("<-:->"):
377+
if "_[ logging\\" in err:
378+
continue
379+
if "LoggingException" in err:
380+
continue
381+
if "---[ Exception is Expected ]---" in err:
382+
continue
383+
print(err)
384+
356385

357386

358387
@CmdLine("a")

tools/update_git.py

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#! Py -3
22
from pathlib import Path
33
from filecmp import cmpfiles
4+
from filecmp import dircmp
45
import sys, os
56
from sortedcontainers import SortedSet
67
from betools import *
@@ -43,17 +44,41 @@ def retain(lst):
4344
result = [f for f in result if not str(f).endswith(k)]
4445
return result
4546

47+
def print_diff_files(dcmp):
48+
for name in dcmp.diff_files:
49+
print("diff_file %s found in %s and %s" % (name, dcmp.left, dcmp.right))
50+
for sub_dcmp in dcmp.subdirs.values():
51+
print_diff_files(sub_dcmp)
52+
4653

4754
@CmdLine('x')
4855
def clean():
49-
"Write batch file to remove unused files from git directory"
50-
os.chdir(str(gitpath))
51-
with Path("clean.bat").open("w") as clean:
52-
toclean = retain([g for g in git if g not in book])
53-
for tc in toclean:
54-
clean.write("del " + str(tc) + "\n")
55-
if Path("clean.bat").stat().st_size == 0:
56-
Path("clean.bat").unlink()
56+
"Show differences with git directory"
57+
os.chdir(str(examplePath))
58+
os.system("diff -q -r . " + str(gitpath))
59+
# common = [str(b) for b in book if not b.is_dir()]
60+
# dcmp = dircmp(str(examplePath), str(gitpath))
61+
# print_diff_files(dcmp)
62+
# print(dcmp.right_only)
63+
# with Path("clean.bat").open('w') as outfile:
64+
# outfile.write("\n" + ruler("match"))
65+
# outfile.write(pformat(match))
66+
# outfile.write("\n" + ruler("mismatch"))
67+
# outfile.write(pformat(mismatch))
68+
# outfile.write("\n" + ruler("errors"))
69+
# outfile.write(pformat(errors))
70+
# head("files to update")
71+
# for f in mismatch:
72+
# outfile.write("copy {} {}\{}\n".format(f, str(gitpath), f))
73+
# print(f)
74+
75+
# os.chdir(str(gitpath))
76+
# with Path("clean.bat").open("w") as clean:
77+
# toclean = retain([g for g in git if g not in book])
78+
# for tc in toclean:
79+
# clean.write("del " + str(tc) + "\n")
80+
# if Path("clean.bat").stat().st_size == 0:
81+
# Path("clean.bat").unlink()
5782

5883
@CmdLine('u')
5984
def update_to_git():

0 commit comments

Comments
 (0)