Files
sarif-cli/bin/sarif-list-files

47 lines
1.6 KiB
Python
Executable File

#!/usr/bin/env python
import argparse
import json
import sarif_cli as S
import sys
import collections
parser = argparse.ArgumentParser(description='list source files referenced by sarif file')
parser.add_argument('file', metavar='sarif-file', type=str,
help='input file, - for stdin')
args = parser.parse_args()
# Grab the file
with open(args.file, 'r') if args.file != '-' else sys.stdin as fp:
sarif_struct = json.load(fp)
# Make sure there are some results
num_results = len(S.get(sarif_struct, 'runs', 0, 'results'))
if num_results == 0:
S.exit(0)
# Collect the file names
uris = set()
# Locations for @kind problem
# e.g.,
# sarif_struct['runs'][0]['results'][5]['locations'][0]['physicalLocation']['artifactLocation']
for resi in range(0, len(S.get(sarif_struct, 'runs', 0, 'results'))):
uri = S.get(sarif_struct, 'runs', 0, 'results', resi, 'locations', 0,
'physicalLocation', 'artifactLocation', 'uri')
uris.add(uri)
# Locations for @kind path-problem
# e.g. sarif_struct['runs'][0]['results'][22]['codeFlows'][0]['threadFlows'][0]['locations'][1]['location']
for resi in range(0, len(S.get(sarif_struct, 'runs', 0, 'results'))):
if 'codeFlows' in S.get(sarif_struct, 'runs', 0, 'results', resi).keys():
locations = S.get(sarif_struct, 'runs', 0, 'results', resi, 'codeFlows', 0,
'threadFlows', 0, 'locations')
for loci in range(0, len(locations)):
uri = S.get(locations, loci, 'location', 'physicalLocation',
'artifactLocation', 'uri')
uris.add(uri)
uris = list(uris)
uris.sort()
for u in uris:
print(u)