1414from collections import defaultdict
1515from betools import CmdLine , visitDir , ruler , head
1616
17+ examplePath = Path (r"C:\Users\Bruce\Dropbox\__TIJ4-ebook\ExtractedExamples" )
18+
1719maindef = 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
107112class 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" )
344355def 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" )
0 commit comments