33import csv
44import sys
55import os
6+ import argparse
67
78"""
89This script collects CodeQL queries that are part of code scanning query packs
1213are on the PATH. It'll try to automatically set the CodeQL search path correctly,
1314as long as you run the script from one of the following locations:
1415 - 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'
1617 and 'codeql-go' directories both exist)
1718"""
1819
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+
1929# Define which languages and query packs to consider
2030languages = [ "cpp" , "csharp" , "go" , "java" , "javascript" , "python" ]
2131packs = [ "code-scanning" , "security-and-quality" , "security-extended" ]
@@ -27,14 +37,14 @@ def prefix_repo_nwo(filename):
2737 This function relies on `git` being available.
2838
2939 For example:
30- /home/alice/git/ql/java/ql/src/MyQuery.ql
40+ /home/alice/git/ql/java/ql/src/MyQuery.ql
3141 becomes:
3242 github/codeql/java/ql/src/MyQuery.ql
33-
43+
3444 If we can't detect a known NWO (e.g. github/codeql, github/codeql-go), the
3545 path will be truncated to the root of the git repo:
3646 ql/java/ql/src/MyQuery.ql
37-
47+
3848 If the filename is not part of a Git repo, the return value is the
3949 same as the input value: the whole path.
4050 """
@@ -45,9 +55,9 @@ def prefix_repo_nwo(filename):
4555 except :
4656 # Not a Git repo
4757 return filename
48-
58+
4959 git_toplevel_dir = git_toplevel_dir_subp .stdout .strip ()
50-
60+
5161 # Detect 'github/codeql' and 'github/codeql-go' repositories by checking the remote (it's a bit
5262 # of a hack but will work in most cases, as long as the remotes have 'codeql' and 'codeql-go'
5363 # in the URL
@@ -100,7 +110,7 @@ def subprocess_run(cmd):
100110#
101111# (and assumes the codeql-go repo is in a similar location)
102112codeql_search_path = "./codeql:./codeql-go:." # will be extended further down
103-
113+
104114# Extend CodeQL search path by detecting root of the current Git repo (if any). This means that you
105115# can run this script from any location within the CodeQL git repository.
106116try :
@@ -116,7 +126,7 @@ def subprocess_run(cmd):
116126# Create CSV writer and write CSV header to stdout
117127csvwriter = csv .writer (sys .stdout )
118128csvwriter .writerow ([
119- "Query filename" , "Suite" , "Query name" , "Query ID" ,
129+ "Query filename" , "Suite" , "Query name" , "Query ID" ,
120130 "Kind" , "Severity" , "Precision" , "Tags"
121131])
122132
@@ -129,29 +139,32 @@ def subprocess_run(cmd):
129139 except Exception as e :
130140 # Resolving queries might go wrong if the github/codeql and github/codeql-go repositories are not
131141 # on the search path.
142+ level = "Warning" if arguments .ignore_missing_query_packs else "Error"
132143 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 ),
134145 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" )
137151
138152 # Investigate metadata for every query by using 'codeql resolve metadata'
139153 for queryfile in queries_subp .stdout .strip ().split ("\n " ):
140154 query_metadata_json = subprocess_run (["codeql" ,"resolve" ,"metadata" ,queryfile ]).stdout .strip ()
141-
155+
142156 # Turn an absolute path to a query file into an nwo-prefixed path (e.g. github/codeql/java/ql/src/....)
143157 queryfile_nwo = prefix_repo_nwo (queryfile )
144158
145159 meta = json .loads (query_metadata_json )
146160
147161 # Python's CSV writer will automatically quote fields if necessary
148162 csvwriter .writerow ([
149- queryfile_nwo , pack ,
163+ queryfile_nwo , pack ,
150164 get_query_metadata ('name' , meta , queryfile_nwo ),
151165 get_query_metadata ('id' , meta , queryfile_nwo ),
152166 get_query_metadata ('kind' , meta , queryfile_nwo ),
153167 get_query_metadata ('problem.severity' , meta , queryfile_nwo ),
154168 get_query_metadata ('precision' , meta , queryfile_nwo ),
155169 get_query_metadata ('tags' , meta , queryfile_nwo )
156170 ])
157-
0 commit comments