Skip to content

Commit

Permalink
Add spinner with package counts during runtime
Browse files Browse the repository at this point in the history
- Add a spinner with package counts displayed at runtime
- On success or failure, update the spinner message and show an
  appropriate symbol

Signed-off-by: Dan Ryan <dan.ryan@canonical.com>
  • Loading branch information
techalchemy committed Jun 17, 2020
1 parent 5946e99 commit 3d3d259
Showing 1 changed file with 53 additions and 32 deletions.
85 changes: 53 additions & 32 deletions cvescan/__main__.py
Expand Up @@ -5,6 +5,7 @@
import logging
import sys

import vistir
from tabulate import tabulate

import cvescan.constants as const
Expand Down Expand Up @@ -53,8 +54,11 @@ def get_null_logger():
LOGGER = get_null_logger()


def error_exit(msg, code=const.ERROR_RETURN_CODE):
print("Error: %s" % msg, file=sys.stderr)
def error_exit(msg, code=const.ERROR_RETURN_CODE, spinner=None):
if spinner is not None:
spinner.fail(msg)
else:
print("Error: %s" % msg, file=sys.stderr)
sys.exit(code)


Expand Down Expand Up @@ -200,39 +204,56 @@ def main():
error_exit_code = (
const.NAGIOS_UNKNOWN_RETURN_CODE if opt.nagios_mode else const.ERROR_RETURN_CODE
)

try:
target_sysinfo = TargetSysInfo(opt, local_sysinfo)

log_config_options(opt)
log_local_system_info(local_sysinfo, opt.manifest_mode)
log_target_system_info(target_sysinfo)
except (FileNotFoundError, PermissionError) as err:
error_exit("Failed to determine the correct Ubuntu codename: %s" % err)
except DistribIDError as di:
error_exit(
"Invalid linux distribution detected, CVEScan must be run on Ubuntu: %s"
% di
)
except PkgCountError as pke:
error_exit("Failed to determine the local package count: %s" % pke)
with vistir.contextmanagers.spinner(
spinner_name="dots", start_text="Calculating System Info..."
) as spinner:
try:
target_sysinfo = TargetSysInfo(opt, local_sysinfo)

log_config_options(opt)
log_local_system_info(local_sysinfo, opt.manifest_mode)
log_target_system_info(target_sysinfo)
except (FileNotFoundError, PermissionError) as err:
error_exit(
"Failed to determine the correct Ubuntu codename: %s" % err,
spinner=spinner,
)
except DistribIDError as di:
error_exit(
"Invalid linux distribution detected, CVEScan must be run on Ubuntu: %s"
% di,
spinner=spinner,
)
except PkgCountError as pke:
error_exit(
"Failed to determine the local package count: %s" % pke, spinner=spinner
)
spinner.ok("✔ System Information Loaded!")

output_formatter = load_output_formatter(opt)

try:
uct_data = load_uct_data(opt, local_sysinfo)
cve_scanner = CVEScanner(LOGGER)
scan_results = cve_scanner.scan(
target_sysinfo.codename, uct_data, target_sysinfo.installed_pkgs
)
(results, return_code) = output_formatter.format_output(
scan_results, target_sysinfo
)
except Exception as ex:
error_exit(
"An unexpected error occurred while running CVEScan: %s" % ex,
error_exit_code,
)
with vistir.contextmanagers.spinner(
spinner_name="dots",
start_text="Scanning {0} packages...".format(
len(target_sysinfo.installed_pkgs)
),
) as spinner:
try:
uct_data = load_uct_data(opt, local_sysinfo)
cve_scanner = CVEScanner(LOGGER)
scan_results = cve_scanner.scan(
target_sysinfo.codename, uct_data, target_sysinfo.installed_pkgs
)
results, return_code = output_formatter.format_output(
scan_results, target_sysinfo
)
except Exception as ex:
error_exit(
"An unexpected error occurred while running CVEScan: %s" % ex,
error_exit_code,
spinner=spinner,
)
spinner.ok("✔ Scan Complete!")

LOGGER.info(results)
sys.exit(return_code)
Expand Down

0 comments on commit 3d3d259

Please sign in to comment.