File: util.py

package info (click to toggle)
cppcheck 2.18.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,132 kB
  • sloc: cpp: 268,935; python: 20,890; ansic: 8,090; sh: 1,045; makefile: 1,008; xml: 1,005; cs: 291
file content (46 lines) | stat: -rw-r--r-- 1,281 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Helpers for pytest tests
import subprocess
import json
import os


def find_cppcheck_binary():
    possible_locations = [
        "./cppcheck",
        "./build/bin/cppcheck",
        r".\bin\cppcheck.exe",
    ]
    for location in possible_locations:
        if os.path.exists(location):
            break
    else:
        raise RuntimeError("Could not find cppcheck binary")

    return location

def dump_create(fpath, *argv):
    cppcheck_binary = find_cppcheck_binary()
    cmd = [cppcheck_binary, "--dump", "-DDUMMY", "--quiet", fpath] + list(argv)
    with subprocess.Popen(cmd) as p:
        p.communicate()
        if p.returncode != 0:
            raise OSError("cppcheck returns error code: %d" % p.returncode)


def dump_remove(fpath):
    os.remove(fpath + ".dump")


def convert_json_output(raw_json_strings):
    """Convert raw stdout/stderr cppcheck JSON output to python dict."""
    json_output = {}
    for line in raw_json_strings:
        if line.startswith('{"summary":'):
            continue
        try:
            json_line = json.loads(line)
            #  json_output[json_line['errorId']] = json_line
            json_output.setdefault(json_line['errorId'], []).append(json_line)
        except ValueError:
            pass
    return json_output