Skip to content

Commit

Permalink
csmock: export finalize_results()
Browse files Browse the repository at this point in the history
... so that it can be reused by cspodman

Related: https://issues.redhat.com/browse/OSH-151
Closes: csutils#115
  • Loading branch information
kdudka committed Sep 22, 2023
1 parent 8a7d66c commit 22752ad
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 50 deletions.
50 changes: 50 additions & 0 deletions py/common/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,20 @@ def append(self, key, value):
self.write("%s = %s\n" % (key, val_str))


def re_from_checker_set(checker_set):
"""return operand for the --checker option of csgrep based on checker_set"""
chk_re = "^("
first = True
for chk in sorted(checker_set):
if first:
first = False
else:
chk_re += "|"
chk_re += chk
chk_re += ")$"
return chk_re


def transform_results(js_file, results):
err_file = re.sub("\\.js", ".err", js_file)
html_file = re.sub("\\.js", ".html", js_file)
Expand All @@ -285,6 +299,42 @@ def transform_results(js_file, results):
return err_file, html_file


def finalize_results(js_file, results, props):
"""transform scan-results.js to scan-results.{err,html} and write stats"""
if props.imp_checker_set:
# filter out "important" defects, first based on checkers only
cmd = "csgrep '%s' --mode=json --checker '%s'" % \
(js_file, re_from_checker_set(props.imp_checker_set))

# then apply custom per-checker filters
for (chk, csgrep_args) in props.imp_csgrep_filters:
chk_re = re_from_checker_set(props.imp_checker_set - set([chk]))
cmd += " | csdiff <(csgrep '%s' --mode=json --drop-scan-props --invert-regex --checker '%s' %s) -" \
% (js_file, chk_re, csgrep_args)

# write the result into *-imp.js
imp_js_file = re.sub("\\.js", "-imp.js", js_file)
cmd += " > '%s'" % imp_js_file

# bash is needed to process <(...)
cmd = strlist_to_shell_cmd(["bash", "-c", cmd], escape_special=True)
results.exec_cmd(cmd, shell=True)

# generate *-imp.{err,html}
transform_results(imp_js_file, results)

# initialize the "imp" flag in the resulting full .js output file
tmp_js_file = re.sub("\\.js", "-tmp.js", js_file)
cmd = "cslinker --implist '%s' '%s' > '%s' && mv -v '%s' '%s'" \
% (imp_js_file, js_file, tmp_js_file, tmp_js_file, js_file)
results.exec_cmd(cmd, shell=True)

(err_file, _) = transform_results(js_file, results)

if props.print_defects:
os.system("csgrep '%s'" % err_file)


def handle_known_fp_list(props, results):
"""Update props.result_filters based on props.known_false_positives"""
if not props.known_false_positives:
Expand Down
51 changes: 1 addition & 50 deletions py/csmock
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ from csmock.common.util import shell_quote
from csmock.common.util import strlist_to_shell_cmd
from csmock.common.results import FatalError
from csmock.common.results import ScanResults
from csmock.common.results import finalize_results
from csmock.common.results import handle_known_fp_list
from csmock.common.results import transform_results

Expand Down Expand Up @@ -659,56 +660,6 @@ class PluginManager:
return len(self.enabled_plugins())


def re_from_checker_set(checker_set):
"""return operand for the --checker option of csgrep based on checker_set"""
chk_re = "^("
first = True
for chk in sorted(checker_set):
if first:
first = False
else:
chk_re += "|"
chk_re += chk
chk_re += ")$"
return chk_re


# transform scan-results.js to scan-results.{err,html} and write stats
def finalize_results(js_file, results, props):
if props.imp_checker_set:
# filter out "important" defects, first based on checkers only
cmd = "csgrep '%s' --mode=json --checker '%s'" % \
(js_file, re_from_checker_set(props.imp_checker_set))

# then apply custom per-checker filters
for (chk, csgrep_args) in props.imp_csgrep_filters:
chk_re = re_from_checker_set(props.imp_checker_set - set([chk]))
cmd += " | csdiff <(csgrep '%s' --mode=json --drop-scan-props --invert-regex --checker '%s' %s) -" \
% (js_file, chk_re, csgrep_args)

# write the result into *-imp.js
imp_js_file = re.sub("\\.js", "-imp.js", js_file)
cmd += " > '%s'" % imp_js_file

# bash is needed to process <(...)
cmd = strlist_to_shell_cmd(["bash", "-c", cmd], escape_special=True)
results.exec_cmd(cmd, shell=True)

# generate *-imp.{err,html}
transform_results(imp_js_file, results)

# initialize the "imp" flag in the resulting full .js output file
tmp_js_file = re.sub("\\.js", "-tmp.js", js_file)
cmd = "cslinker --implist '%s' '%s' > '%s' && mv -v '%s' '%s'" \
% (imp_js_file, js_file, tmp_js_file, tmp_js_file, js_file)
results.exec_cmd(cmd, shell=True)

(err_file, _) = transform_results(js_file, results)

if props.print_defects:
os.system("csgrep '%s'" % err_file)


# argparse._VersionAction would write to stderr, which breaks help2man
class VersionPrinter(argparse.Action):
def __init__(self, option_strings, dest=None, default=None, help=None):
Expand Down

0 comments on commit 22752ad

Please sign in to comment.