Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7d86a8d

Browse files
committed
Python: Improve speed of process-mrva-results.py
Same trick as 'generate-code-scanning-query-list.py'
1 parent 750f14f commit 7d86a8d

1 file changed

Lines changed: 44 additions & 7 deletions

File tree

python/ql/src/meta/ClassHierarchy/process-mrva-results.py

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import subprocess
88
from collections import defaultdict
99
import yaml
10+
import shutil
11+
import os
1012

1113
VERSION = "process-mrva-results 0.0.1"
1214

@@ -18,6 +20,40 @@
1820

1921
# process data
2022

23+
class CodeQL:
24+
def __init__(self):
25+
pass
26+
27+
def __enter__(self):
28+
self.proc = subprocess.Popen(['codeql', 'execute','cli-server'],
29+
executable=shutil.which('codeql'),
30+
stdin=subprocess.PIPE,
31+
stdout=subprocess.PIPE,
32+
stderr=sys.stderr,
33+
env=os.environ.copy(),
34+
)
35+
return self
36+
def __exit__(self, type, value, tb):
37+
self.proc.stdin.write(b'["shutdown"]\0')
38+
self.proc.stdin.close()
39+
try:
40+
self.proc.wait(5)
41+
except:
42+
self.proc.kill()
43+
44+
def command(self, args):
45+
data = json.dumps(args)
46+
data_bytes = data.encode('utf-8')
47+
self.proc.stdin.write(data_bytes)
48+
self.proc.stdin.write(b'\0')
49+
self.proc.stdin.flush()
50+
res = b''
51+
while True:
52+
b = self.proc.stdout.read(1)
53+
if b == b'\0':
54+
return res.decode('utf-8')
55+
res += b
56+
2157
def wrap_in_template(data):
2258
return {
2359
"extensions": [
@@ -46,15 +82,16 @@ def parse_from_file(path: Path) -> set:
4682

4783

4884
def gather_from_bqrs_results():
49-
for f in glob.glob(f"{sys.argv[1]}/**/results.bqrs", recursive=True):
50-
print(f"Processing {f}")
85+
with CodeQL() as codeql:
86+
for f in glob.glob(f"{sys.argv[1]}/**/results.bqrs", recursive=True):
87+
print(f"Processing {f}")
5188

52-
json_data = subprocess.check_output(["codeql", "bqrs", "decode", "--format=json", f])
53-
select = json.loads(json_data)
89+
json_data = codeql.command(["bqrs", "decode", "--format=json", f])
90+
select = json.loads(json_data)
5491

55-
for t in select["#select"]["tuples"]:
56-
pkg = t[1]
57-
package_data[pkg].add(tuple(t))
92+
for t in select["#select"]["tuples"]:
93+
pkg = t[1]
94+
package_data[pkg].add(tuple(t))
5895

5996
def gather_from_existing():
6097
for f in glob.glob(f"{mad_path}/auto-*.model.yml", recursive=True):

0 commit comments

Comments
 (0)