From b260a942c3b77bc2a1403236669e70c956b48fae Mon Sep 17 00:00:00 2001 From: Henri Rosten Date: Fri, 15 Dec 2023 10:42:50 +0200 Subject: [PATCH] Move exit_unless_command_exists to common utils Signed-off-by: Henri Rosten --- src/common/utils.py | 10 ++++++++++ src/nixmeta/main.py | 19 +++---------------- src/vulnxscan/vulnxscan_cli.py | 14 +++----------- 3 files changed, 16 insertions(+), 27 deletions(-) diff --git a/src/common/utils.py b/src/common/utils.py index c50e63b..ec279c5 100644 --- a/src/common/utils.py +++ b/src/common/utils.py @@ -14,6 +14,8 @@ import logging import subprocess import importlib.metadata +from shutil import which + import packaging.version from tabulate import tabulate from colorlog import ColoredFormatter, default_log_colors @@ -138,6 +140,14 @@ def exec_cmd( return None +def exit_unless_command_exists(name): + """Check if `name` is an executable in PATH""" + name_is_in_path = which(name) is not None + if not name_is_in_path: + LOG.fatal("command '%s' is not in PATH", name) + sys.exit(1) + + def exit_unless_nix_artifact(path, force_realise=False): """ Exit with error if `path` is not a nix artifact. If `force_realize` is True, diff --git a/src/nixmeta/main.py b/src/nixmeta/main.py index 46bd77f..4bb29fb 100755 --- a/src/nixmeta/main.py +++ b/src/nixmeta/main.py @@ -5,12 +5,10 @@ """ Python script for summarizing nixpkgs meta-attributes """ -import sys import argparse import pathlib -import shutil from nixmeta.scanner import NixMetaScanner -from common.utils import LOG, set_log_verbosity +from common.utils import set_log_verbosity, exit_unless_command_exists ################################################################################ @@ -68,24 +66,13 @@ def _getargs(): ############################################################################### -def _exit_unless_command_exists(name): - """Check if `name` is an executable in PATH""" - name_is_in_path = shutil.which(name) is not None - if not name_is_in_path: - LOG.fatal("command '%s' is not in PATH", name) - sys.exit(1) - - -############################################################################### - - def main(): """main entry point""" args = _getargs() set_log_verbosity(args.verbose) # Fail early if the following commands are not in PATH - _exit_unless_command_exists("nix") - _exit_unless_command_exists("nix-env") + exit_unless_command_exists("nix") + exit_unless_command_exists("nix-env") # Scan metadata from the flakeref pinned nixpkgs scanner = NixMetaScanner() scanner.scan(args.flakeref) diff --git a/src/vulnxscan/vulnxscan_cli.py b/src/vulnxscan/vulnxscan_cli.py index b0fd023..b57d5df 100755 --- a/src/vulnxscan/vulnxscan_cli.py +++ b/src/vulnxscan/vulnxscan_cli.py @@ -22,7 +22,6 @@ import time import urllib.parse from tempfile import NamedTemporaryFile -from shutil import which import pandas as pd import numpy as np @@ -42,6 +41,7 @@ df_from_csv_file, df_log, exit_unless_nix_artifact, + exit_unless_command_exists, nix_to_repology_pkg_name, parse_version, version_distance, @@ -761,14 +761,6 @@ def _is_json(path): return False -def _exit_unless_command_exists(name): - """Check if `name` is an executable in PATH""" - name_is_in_path = which(name) is not None - if not name_is_in_path: - LOG.fatal("command '%s' is not in PATH", name) - sys.exit(1) - - ################################################################################ # Whitelist @@ -868,8 +860,8 @@ def main(): set_log_verbosity(args.verbose) # Fail early if following commands are not in path - _exit_unless_command_exists("grype") - _exit_unless_command_exists("vulnix") + exit_unless_command_exists("grype") + exit_unless_command_exists("vulnix") target_path_abs = args.TARGET.resolve().as_posix() scanner = VulnScan()