Skip to content

Commit

Permalink
Move exit_unless_command_exists to common utils
Browse files Browse the repository at this point in the history
Signed-off-by: Henri Rosten <[email protected]>
  • Loading branch information
henrirosten committed Dec 15, 2023
1 parent f140d53 commit b260a94
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 27 deletions.
10 changes: 10 additions & 0 deletions src/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
19 changes: 3 additions & 16 deletions src/nixmeta/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


################################################################################
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 3 additions & 11 deletions src/vulnxscan/vulnxscan_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit b260a94

Please sign in to comment.