|
7 | 7 | import subprocess |
8 | 8 | from collections import defaultdict |
9 | 9 | import yaml |
| 10 | +import shutil |
| 11 | +import os |
10 | 12 |
|
11 | 13 | VERSION = "process-mrva-results 0.0.1" |
12 | 14 |
|
|
18 | 20 |
|
19 | 21 | # process data |
20 | 22 |
|
| 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 | + |
21 | 57 | def wrap_in_template(data): |
22 | 58 | return { |
23 | 59 | "extensions": [ |
@@ -46,15 +82,16 @@ def parse_from_file(path: Path) -> set: |
46 | 82 |
|
47 | 83 |
|
48 | 84 | 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}") |
51 | 88 |
|
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) |
54 | 91 |
|
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)) |
58 | 95 |
|
59 | 96 | def gather_from_existing(): |
60 | 97 | for f in glob.glob(f"{mad_path}/auto-*.model.yml", recursive=True): |
|
0 commit comments