sarif-results-summary -s: include source file lines in output

This commit is contained in:
Michael Hohn
2021-11-09 16:10:12 -08:00
committed by =Michael Hohn
parent ab1d7c27ef
commit b69eec404d
2 changed files with 23 additions and 1 deletions

View File

@@ -7,6 +7,8 @@ import collections
parser = argparse.ArgumentParser(description='summary of results')
parser.add_argument('file', metavar='sarif-file', type=str, help='input file, - for stdin')
parser.add_argument('-s', '--list-source', metavar='srcroot', type=str,
help='list source snippets using srcroot as sarif SRCROOT')
args = parser.parse_args()
with open(args.file, 'r') if args.file != '-' else sys.stdin as fp:
@@ -28,3 +30,9 @@ for runi in S.indices(sarif_struct, 'runs'):
l1, c1, l2, c2 = S.lineinfo(region)
filepath = "%s:%d:%d:%d:%d" % (artifact['uri'], l1, c1, l2, c2)
S.msg("%s: %s\n\n" % (filepath, message))
if args.list_source:
lines = S.load_lines(args.list_source, artifact['uri'], l1, l2)
for line in lines:
S.msg("%s" % (line))
S.msg("\n")
S.msg("\n")

View File

@@ -1,9 +1,18 @@
import sys
import os
MIN_PYTHON = (3, 7)
if sys.version_info < MIN_PYTHON:
sys.exit("Python %s.%s or later is required.\n" % MIN_PYTHON)
def load_lines(root, path, line_from, line_to):
"""Load the line range [line_from, line_to], including both,
from the file at root/path. Lines are counted from 1. Newlines are dropped."""
fname = os.path.join(root, path)
with open(fname, 'r') as file:
lines = file.readlines()
return [line.strip() for line in lines[line_from-1 : line_to-1+1]]
def lineinfo(region):
""" Return sensible values for start/end line/columns for the possibly empty
entries in the sarif 'region' structure.
@@ -33,4 +42,9 @@ def get(sarif_struct, *path):
def msg(message):
""" Print message to stdout """
sys.stdout.write(message)
def dbg(message):
""" Print message to stderr """
sys.stdout.flush()
sys.stderr.write(message)
sys.stderr.flush()