3
3
import csv
4
4
import sys
5
5
import os
6
+ import argparse
6
7
7
8
"""
8
9
This script collects CodeQL queries that are part of code scanning query packs
12
13
are on the PATH. It'll try to automatically set the CodeQL search path correctly,
13
14
as long as you run the script from one of the following locations:
14
15
- anywhere from within a clone of the CodeQL Git repo
15
- - from the parent directory of a clone of the CodeQL Git repo (assuming 'codeql'
16
+ - from the parent directory of a clone of the CodeQL Git repo (assuming 'codeql'
16
17
and 'codeql-go' directories both exist)
17
18
"""
18
19
20
+ parser = argparse .ArgumentParser (__name__ )
21
+ parser .add_argument (
22
+ "--ignore-missing-query-packs" ,
23
+ action = "store_true" ,
24
+ help = "Don't fail if a query pack can't be found" ,
25
+ )
26
+ arguments = parser .parse_args ()
27
+ assert hasattr (arguments , "ignore_missing_query_packs" )
28
+
19
29
# Define which languages and query packs to consider
20
30
languages = [ "cpp" , "csharp" , "go" , "java" , "javascript" , "python" ]
21
31
packs = [ "code-scanning" , "security-and-quality" , "security-extended" ]
@@ -27,14 +37,14 @@ def prefix_repo_nwo(filename):
27
37
This function relies on `git` being available.
28
38
29
39
For example:
30
- /home/alice/git/ql/java/ql/src/MyQuery.ql
40
+ /home/alice/git/ql/java/ql/src/MyQuery.ql
31
41
becomes:
32
42
github/codeql/java/ql/src/MyQuery.ql
33
-
43
+
34
44
If we can't detect a known NWO (e.g. github/codeql, github/codeql-go), the
35
45
path will be truncated to the root of the git repo:
36
46
ql/java/ql/src/MyQuery.ql
37
-
47
+
38
48
If the filename is not part of a Git repo, the return value is the
39
49
same as the input value: the whole path.
40
50
"""
@@ -45,9 +55,9 @@ def prefix_repo_nwo(filename):
45
55
except :
46
56
# Not a Git repo
47
57
return filename
48
-
58
+
49
59
git_toplevel_dir = git_toplevel_dir_subp .stdout .strip ()
50
-
60
+
51
61
# Detect 'github/codeql' and 'github/codeql-go' repositories by checking the remote (it's a bit
52
62
# of a hack but will work in most cases, as long as the remotes have 'codeql' and 'codeql-go'
53
63
# in the URL
@@ -100,7 +110,7 @@ def subprocess_run(cmd):
100
110
#
101
111
# (and assumes the codeql-go repo is in a similar location)
102
112
codeql_search_path = "./codeql:./codeql-go:." # will be extended further down
103
-
113
+
104
114
# Extend CodeQL search path by detecting root of the current Git repo (if any). This means that you
105
115
# can run this script from any location within the CodeQL git repository.
106
116
try :
@@ -116,7 +126,7 @@ def subprocess_run(cmd):
116
126
# Create CSV writer and write CSV header to stdout
117
127
csvwriter = csv .writer (sys .stdout )
118
128
csvwriter .writerow ([
119
- "Query filename" , "Suite" , "Query name" , "Query ID" ,
129
+ "Query filename" , "Suite" , "Query name" , "Query ID" ,
120
130
"Kind" , "Severity" , "Precision" , "Tags"
121
131
])
122
132
@@ -129,29 +139,32 @@ def subprocess_run(cmd):
129
139
except Exception as e :
130
140
# Resolving queries might go wrong if the github/codeql and github/codeql-go repositories are not
131
141
# on the search path.
142
+ level = "Warning" if arguments .ignore_missing_query_packs else "Error"
132
143
print (
133
- "Warning : couldn't find query pack '%s' for language '%s'. Do you have the right repositories in the right places (search path: '%s')?" % (pack , lang , codeql_search_path ),
144
+ "%s : couldn't find query pack '%s' for language '%s'. Do you have the right repositories in the right places (search path: '%s')?" % (level , pack , lang , codeql_search_path ),
134
145
file = sys .stderr
135
- )
136
- continue
146
+ )
147
+ if arguments .ignore_missing_query_packs :
148
+ continue
149
+ else :
150
+ sys .exit ("You can use '--ignore-missing-query-packs' to ignore this error" )
137
151
138
152
# Investigate metadata for every query by using 'codeql resolve metadata'
139
153
for queryfile in queries_subp .stdout .strip ().split ("\n " ):
140
154
query_metadata_json = subprocess_run (["codeql" ,"resolve" ,"metadata" ,queryfile ]).stdout .strip ()
141
-
155
+
142
156
# Turn an absolute path to a query file into an nwo-prefixed path (e.g. github/codeql/java/ql/src/....)
143
157
queryfile_nwo = prefix_repo_nwo (queryfile )
144
158
145
159
meta = json .loads (query_metadata_json )
146
160
147
161
# Python's CSV writer will automatically quote fields if necessary
148
162
csvwriter .writerow ([
149
- queryfile_nwo , pack ,
163
+ queryfile_nwo , pack ,
150
164
get_query_metadata ('name' , meta , queryfile_nwo ),
151
165
get_query_metadata ('id' , meta , queryfile_nwo ),
152
166
get_query_metadata ('kind' , meta , queryfile_nwo ),
153
167
get_query_metadata ('problem.severity' , meta , queryfile_nwo ),
154
168
get_query_metadata ('precision' , meta , queryfile_nwo ),
155
169
get_query_metadata ('tags' , meta , queryfile_nwo )
156
170
])
157
-
0 commit comments