From a0af2c8c59ce4c521ef18f7ae71769ea0154cb28 Mon Sep 17 00:00:00 2001 From: Michael Hohn Date: Tue, 9 Nov 2021 14:29:31 -0800 Subject: [PATCH] fix: traverse all languages --- bin/sarif-list-files | 52 +++++++++++++++++++++------------------ bin/sarif-results-summary | 32 +++++++++++------------- sarif_cli/__init__.py | 4 +++ 3 files changed, 47 insertions(+), 41 deletions(-) diff --git a/bin/sarif-list-files b/bin/sarif-list-files index d3c39e6..30a116f 100755 --- a/bin/sarif-list-files +++ b/bin/sarif-list-files @@ -14,32 +14,36 @@ args = parser.parse_args() 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 +# File name collection 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) +# Traverse all runs +for runi in S.indices(sarif_struct, 'runs'): + # Make sure there are some results + num_results = len(S.get(sarif_struct, 'runs', runi, 'results')) + if num_results == 0: continue + + # Collect the file names + + # Locations for @kind problem + # e.g., + # sarif_struct['runs'][0]['results'][5]['locations'][0]['physicalLocation']['artifactLocation'] + for resi in S.indices(sarif_struct, 'runs', runi, 'results'): + uri = S.get(sarif_struct, 'runs', runi, '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 S.indices(sarif_struct, 'runs', runi, 'results'): + if 'codeFlows' in S.get(sarif_struct, 'runs', runi, 'results', resi).keys(): + locations = S.get(sarif_struct, 'runs', runi, '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) +# Dump the file names uris = list(uris) uris.sort() for u in uris: diff --git a/bin/sarif-results-summary b/bin/sarif-results-summary index 4099c54..81d58d9 100644 --- a/bin/sarif-results-summary +++ b/bin/sarif-results-summary @@ -12,21 +12,19 @@ args = parser.parse_args() with open(args.file, 'r') if args.file != '-' else sys.stdin as fp: sarif_struct = json.load(fp) -num_results = len(S.get(sarif_struct, 'runs', 0, 'results')) -S.msg("Found %d results\n\n" % num_results) -if num_results == 0: - S.exit(0) - -for resi in range(0, len(S.get(sarif_struct, 'runs', 0, 'results'))): - message = S.get(sarif_struct, 'runs', 0, 'results', resi, 'message', 'text') - artifact = S.get(sarif_struct, 'runs', 0, 'results', resi, 'locations', 0, - 'physicalLocation', 'artifactLocation') - region = S.get(sarif_struct, 'runs', 0, 'results', resi, 'locations', 0, - 'physicalLocation', 'region') - filepath = "%s:%d:%d" % (artifact['uri'], region['startLine'], - region.get('startColumn', -1)) - S.msg("%s: %s\n" % (filepath, message)) - - - +for runi in S.indices(sarif_struct, 'runs'): + num_results = len(S.get(sarif_struct, 'runs', runi, 'results')) + language = S.get(sarif_struct, 'runs', runi, 'properties', + 'semmle.sourceLanguage') + S.msg("Found %d results for %s \n\n" % (num_results, language)) + if num_results == 0: continue + for resi in S.indices(sarif_struct, 'runs', runi, 'results'): + message = S.get(sarif_struct, 'runs', runi, 'results', resi, 'message', 'text') + artifact = S.get(sarif_struct, 'runs', runi, 'results', resi, 'locations', 0, + 'physicalLocation', 'artifactLocation') + region = S.get(sarif_struct, 'runs', runi, 'results', resi, 'locations', 0, + 'physicalLocation', 'region') + filepath = "%s:%d:%d" % (artifact['uri'], region['startLine'], + region.get('startColumn', -1)) + S.msg("%s: %s\n" % (filepath, message)) diff --git a/sarif_cli/__init__.py b/sarif_cli/__init__.py index 67709c7..860332f 100644 --- a/sarif_cli/__init__.py +++ b/sarif_cli/__init__.py @@ -4,6 +4,10 @@ MIN_PYTHON = (3, 7) if sys.version_info < MIN_PYTHON: sys.exit("Python %s.%s or later is required.\n" % MIN_PYTHON) +def indices(sarif_struct, *path): + """ Return a range for the indices of PATH """ + return range(0, len(get(sarif_struct, *path))) + def get(sarif_struct, *path): """ Get the sarif entry at PATH """ res = sarif_struct