Skip to content

Commit

Permalink
csmock: export handle_known_fp_list() and require_file()
Browse files Browse the repository at this point in the history
... so that they can be reused by cspodman

Related: https://issues.redhat.com/browse/OSH-151
Closes: csutils#115
  • Loading branch information
lbossis authored and kdudka committed Sep 13, 2023
1 parent 0671e2a commit 4fe62b6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 34 deletions.
30 changes: 30 additions & 0 deletions py/common/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,33 @@ def transform_results(js_file, results):
results.exec_cmd("csgrep --mode=evtstat %s '%s' | tee '%s'" % \
(CSGREP_FINAL_FILTER_ARGS, js_file, stat_file), shell=True)
return err_file, html_file


def handle_known_fp_list(props):
"""Update props.result_filters based on props.known_false_positives"""

# install global filter of known false positives
filter_cmd = f'csdiff --json-output --show-internal "{props.known_false_positives}" -'
props.result_filters += [filter_cmd]

if props.pkg is None:
# no package name available
return

kfp_dir = re.sub("\\.js", ".d", props.known_false_positives)
if not os.path.isdir(kfp_dir):
# no per-pkg known false positives available
return

ep_file = os.path.join(kfp_dir, props.pkg, "exclude-paths.txt")
if not os.path.exists(ep_file):
# no list of path regexes to exclude for this pkg
return

# install path exclusion filters for this pkg
with open(ep_file) as file_handle:
lines = file_handle.readlines()
for line in lines:
path_re = line.strip()
filter_cmd = f'csgrep --mode=json --invert-match --path="{shell_quote(path_re)}"'
props.result_filters += [filter_cmd]
7 changes: 7 additions & 0 deletions py/common/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# You should have received a copy of the GNU General Public License
# along with csmock. If not, see <http://www.gnu.org/licenses/>.

import os
import re


Expand Down Expand Up @@ -121,3 +122,9 @@ def dirs_to_scan_by_args(parser, args, props, tool):
props.need_rpm_bi = True

return dirs_to_scan


def require_file(parser, name):
"""Print an error and exit unsuccessfully if 'name' is not a file"""
if not os.path.isfile(name):
parser.error(f"'{name}' is not a file")
36 changes: 2 additions & 34 deletions py/csmock
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ import time

# local imports
import csmock.common.util
from csmock.common.util import require_file
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 handle_known_fp_list
from csmock.common.results import transform_results


Expand Down Expand Up @@ -671,34 +673,6 @@ def re_from_checker_set(checker_set):
return chk_re


def handle_known_fp_list(props):
# install global filter of known false positives
filter_cmd = 'csdiff --json-output --show-internal "%s" -' % props.known_false_positives
props.result_filters += [ filter_cmd ]

if props.pkg is None:
# no package name available
return

kfp_dir = re.sub("\\.js", ".d", props.known_false_positives)
if not os.path.isdir(kfp_dir):
# no per-pkg known false positives available
return

ep_file = os.path.join(kfp_dir, props.pkg, "exclude-paths.txt")
if not os.path.exists(ep_file):
# no list of path regexes to exclude for this pkg
return

# install path exclusion filters for this pkg
with open(ep_file) as f:
lines = f.readlines()
for l in lines:
path_re = l.strip()
filter_cmd = 'csgrep --mode=json --invert-match --path=%s' % shell_quote(path_re)
props.result_filters += [ filter_cmd ]


# transform scan-results.js to scan-results.{err,html} and write stats
def finalize_results(js_file, results, props):
if props.imp_checker_set:
Expand Down Expand Up @@ -758,12 +732,6 @@ is a file name, please use the './' prefix." % val)
setattr(namespace, self.dest, val)


def require_file(parser, name):
"""Print an error and exit unsuccessfully if 'name' is not a file"""
if not os.path.isfile(name):
parser.error("'%s' is not a file" % name)


def main():
# load plug-ins
plugins = PluginManager()
Expand Down

0 comments on commit 4fe62b6

Please sign in to comment.