diff --git a/bin/sarif-results-summary b/bin/sarif-results-summary index 3bb4691..6bb4c2c 100755 --- a/bin/sarif-results-summary +++ b/bin/sarif-results-summary @@ -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") diff --git a/sarif_cli/__init__.py b/sarif_cli/__init__.py index 9b8da57..14388cd 100644 --- a/sarif_cli/__init__.py +++ b/sarif_cli/__init__.py @@ -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()