5
5
import subprocess
6
6
import json
7
7
import glob
8
+ from shutil import copyfile
8
9
9
10
def print_usage (exit_code = 1 ):
10
11
print ("Usage: python3 make_stubs.py testDir stubDir\n " ,
@@ -34,13 +35,6 @@ def check_file_exists(path):
34
35
print (path , "does not exist or is not a regular file" )
35
36
exit (1 )
36
37
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
-
44
38
check_dir_exists (testDir )
45
39
check_dir_exists (stubDir )
46
40
@@ -53,14 +47,8 @@ def copy_file(src, dest):
53
47
54
48
# Does it contain a .ql file and a .java file?
55
49
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 ))
64
52
65
53
if not foundQL :
66
54
print ("Test directory does not contain .ql files. Please specify a working qltest directory." )
@@ -89,14 +77,16 @@ def print_javac_output():
89
77
b2 = line .find (']' , b1 + 1 )
90
78
print (line [b2 + 2 :], end = "" )
91
79
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 )
92
84
93
85
print ("Stubbing qltest in" , testDir )
94
86
95
- copy_file (options0File , optionsFile )
87
+ copyfile (options0File , optionsFile )
96
88
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 ]):
100
90
print_javac_output ()
101
91
print ("codeql test failed. Please fix up the test before proceeding." )
102
92
exit (1 )
@@ -105,23 +95,21 @@ def print_javac_output():
105
95
print ("Expected database directory " + dbDir + " not found." )
106
96
exit (1 )
107
97
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 ]):
112
99
print ('Failed to run the query to generate the stubs.' )
113
100
exit (1 )
114
101
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 ]):
119
103
print ('Failed to convert ' + outputBqrsFile + ' to JSON.' )
120
104
exit (1 )
121
105
122
106
with open (outputJsonFile ) as f :
123
107
results = json .load (f )
124
108
109
+ if not '#select' in results or not 'tuples' in results ['#select' ]:
110
+ print ('Unexpected JSON output - no tuples found' )
111
+ exit (1 )
112
+
125
113
for (typ , stub ) in results ['#select' ]['tuples' ]:
126
114
stubFile = os .path .join (stubDir , typ .replace ("." , "/" ) + ".java" )
127
115
os .makedirs (os .path .dirname (stubFile ), exist_ok = True )
@@ -130,11 +118,9 @@ def print_javac_output():
130
118
131
119
print ("Verifying stub correctness" )
132
120
133
- copy_file (options1File , optionsFile )
121
+ copyfile (options1File , optionsFile )
134
122
135
- cmd = ['codeql' , 'test' , 'run' , testDir ]
136
- print ('Running ' + ' ' .join (cmd ))
137
- if subprocess .call (cmd ):
123
+ if run (['codeql' , 'test' , 'run' , testDir ]):
138
124
print_javac_output ()
139
125
print ('\n Test failed. You may need to fix up the generated stubs.' )
140
126
exit (1 )
0 commit comments