55import subprocess
66import json
77import glob
8+ from shutil import copyfile
89
910def print_usage (exit_code = 1 ):
1011 print ("Usage: python3 make_stubs.py testDir stubDir\n " ,
@@ -34,13 +35,6 @@ def check_file_exists(path):
3435 print (path , "does not exist or is not a regular file" )
3536 exit (1 )
3637
37-
38- def copy_file (src , dest ):
39- with open (src ) as srcf :
40- with open (dest , "w" ) as destf :
41- destf .write (srcf .read ())
42-
43-
4438check_dir_exists (testDir )
4539check_dir_exists (stubDir )
4640
@@ -53,14 +47,8 @@ def copy_file(src, dest):
5347
5448# Does it contain a .ql file and a .java file?
5549
56- foundJava = False
57- foundQL = False
58-
59- for file in os .listdir (testDir ):
60- if file .endswith (".java" ):
61- foundJava = True
62- if file .endswith (".ql" ) or file .endswith (".qlref" ):
63- foundQL = True
50+ foundJava = any (f .endswith (".java" ) for f in os .listdir (testDir ))
51+ foundQL = any (f .endswith (".ql" ) or f .endswith (".qlref" ) for f in os .listdir (testDir ))
6452
6553if not foundQL :
6654 print ("Test directory does not contain .ql files. Please specify a working qltest directory." )
@@ -89,14 +77,16 @@ def print_javac_output():
8977 b2 = line .find (']' , b1 + 1 )
9078 print (line [b2 + 2 :], end = "" )
9179
80+ def run (cmd ):
81+ """Runs the given command, returning the exit code (nonzero on failure)"""
82+ print ('\n Running ' + ' ' .join (cmd ) + '\n ' )
83+ return subprocess .call (cmd )
9284
9385print ("Stubbing qltest in" , testDir )
9486
95- copy_file (options0File , optionsFile )
87+ copyfile (options0File , optionsFile )
9688
97- cmd = ['codeql' , 'test' , 'run' , '--keep-databases' , testDir ]
98- print ('Running ' + ' ' .join (cmd ))
99- if subprocess .call (cmd ):
89+ if run (['codeql' , 'test' , 'run' , '--keep-databases' , testDir ]):
10090 print_javac_output ()
10191 print ("codeql test failed. Please fix up the test before proceeding." )
10292 exit (1 )
@@ -105,23 +95,21 @@ def print_javac_output():
10595 print ("Expected database directory " + dbDir + " not found." )
10696 exit (1 )
10797
108- cmd = ['codeql' , 'query' , 'run' , os .path .join (
109- javaQueries , 'MinimalStubsFromSource.ql' ), '--database' , dbDir , '--output' , outputBqrsFile ]
110- print ('Running ' + ' ' .join (cmd ))
111- if subprocess .call (cmd ):
98+ if run (['codeql' , 'query' , 'run' , os .path .join (javaQueries , 'MinimalStubsFromSource.ql' ), '--database' , dbDir , '--output' , outputBqrsFile ]):
11299 print ('Failed to run the query to generate the stubs.' )
113100 exit (1 )
114101
115- cmd = ['codeql' , 'bqrs' , 'decode' , outputBqrsFile ,
116- '--format=json' , '--output' , outputJsonFile ]
117- print ('Running ' + ' ' .join (cmd ))
118- if subprocess .call (cmd ):
102+ if run (['codeql' , 'bqrs' , 'decode' , outputBqrsFile , '--format=json' , '--output' , outputJsonFile ]):
119103 print ('Failed to convert ' + outputBqrsFile + ' to JSON.' )
120104 exit (1 )
121105
122106with open (outputJsonFile ) as f :
123107 results = json .load (f )
124108
109+ if not '#select' in results or not 'tuples' in results ['#select' ]:
110+ print ('Unexpected JSON output - no tuples found' )
111+ exit (1 )
112+
125113for (typ , stub ) in results ['#select' ]['tuples' ]:
126114 stubFile = os .path .join (stubDir , typ .replace ("." , "/" ) + ".java" )
127115 os .makedirs (os .path .dirname (stubFile ), exist_ok = True )
@@ -130,11 +118,9 @@ def print_javac_output():
130118
131119print ("Verifying stub correctness" )
132120
133- copy_file (options1File , optionsFile )
121+ copyfile (options1File , optionsFile )
134122
135- cmd = ['codeql' , 'test' , 'run' , testDir ]
136- print ('Running ' + ' ' .join (cmd ))
137- if subprocess .call (cmd ):
123+ if run (['codeql' , 'test' , 'run' , testDir ]):
138124 print_javac_output ()
139125 print ('\n Test failed. You may need to fix up the generated stubs.' )
140126 exit (1 )
0 commit comments