From 416b03c0c844a28512143826453ad956e98abd7c Mon Sep 17 00:00:00 2001 From: Paul Horton Date: Thu, 9 Dec 2021 14:18:44 +0000 Subject: [PATCH] feat: removed `terminaltables` and replaced with `rich`s table implementation Signed-off-by: Paul Horton --- jake/command/oss.py | 70 ++++++++++++++++++++++----------------------- poetry.lock | 14 +-------- pyproject.toml | 1 - 3 files changed, 35 insertions(+), 50 deletions(-) diff --git a/jake/command/oss.py b/jake/command/oss.py index c2edeb4..85ec9a2 100644 --- a/jake/command/oss.py +++ b/jake/command/oss.py @@ -31,15 +31,20 @@ from ossindex.model import OssIndexComponent, Vulnerability from ossindex.ossindex import OssIndex from packageurl import PackageURL +from rich.console import Console from rich.progress import Progress -from terminaltables import DoubleTable +from rich.table import Table from . import BaseCommand class OssCommand(BaseCommand): + _console: Console + def handle_args(self) -> int: + self._console = Console() + exit_code: int = 0 with Progress() as progress: @@ -114,7 +119,6 @@ def handle_args(self) -> int: return exit_code - def setup_argument_parser(self, subparsers: argparse._SubParsersAction): parser = subparsers.add_parser('ddt', help='perform a scan backed by OSS Index') @@ -131,7 +135,6 @@ def setup_argument_parser(self, subparsers: argparse._SubParsersAction): choices={'1.3', '1.2', '1.1', '1.0'}, default='1.3', dest='oss_schema_version') - def _build_bom(self, oss_index_results: List[OssIndexComponent]) -> Bom: bom = Bom() oic: OssIndexComponent = None @@ -156,7 +159,6 @@ def _build_bom(self, oss_index_results: List[OssIndexComponent]) -> Bom: return bom - def _print_oss_index_report(self, oss_index_results: List[OssIndexComponent]): total_vulnerabilities = 0 total_packages = len(oss_index_results) @@ -181,40 +183,37 @@ def _print_oss_index_report(self, oss_index_results: List[OssIndexComponent]): i += 1 print('') - table_data = [ - ["Audited Dependencies", len(oss_index_results)], - ["Vulnerablities Found", total_vulnerabilities], - - ] - table_instance = DoubleTable(table_data, "Summary") - print(table_instance.table) - - - @staticmethod - def _print_vulnerability_as_table(v: Vulnerability) -> None: - table_data = [ - ["ID", v.get_id()], - ["Title", v.get_title()], - ["Description", '\n'.join(wrap(v.get_description(), 100))], - ["CVSS Score", f"{v.get_cvss_score()} - {OssCommand._get_severity_for_cvss_score(v.get_cvss_score())}"], - ] - if v.get_cvss_vector(): - table_data.append( - ["CVSS Vector", v.get_cvss_vector()] - ) - - table_data.extend( - [ - ["CWE", v.get_cwe()], - ["Reference", v.get_oss_index_reference_url()] - ] + table = Table(title='Summary') + table.add_column("Audited Dependencies", justify="left", no_wrap=True) + table.add_column("Vulnerabilities Found", justify="left", no_wrap=True) + table.add_row('{}'.format(len(oss_index_results)), f'{total_vulnerabilities}') + + self._console.print(table) + + def _print_vulnerability_as_table(self, v: Vulnerability) -> None: + table = Table(title='Vulnerability Details') + table.add_column("ID", justify="center", no_wrap=True) + table.add_column("Title", justify="left", no_wrap=False) + table.add_column("Description", justify="left", no_wrap=False) + table.add_column("CVSS Score", justify="center", no_wrap=True) + table.add_column("CVSS Vector", justify="right", no_wrap=True) + table.add_column("CWE", justify="center", no_wrap=True) + table.add_column("Ref.", justify="left", no_wrap=True) + + table.add_row( + v.get_id(), + v.get_title(), + v.get_description(), # '\n'.join(wrap(v.get_description(), 100)), + f"{v.get_cvss_score()} - {OssCommand._get_severity_for_cvss_score(v.get_cvss_score())}", + v.get_cvss_vector() if v.get_cvss_vector() else 'Unknown', + v.get_cwe(), + v.get_oss_index_reference_url() ) - table_instance = DoubleTable(table_data) - table_instance.inner_heading_row_border = False - table_instance.inner_row_border = True - print(OssCommand._get_color_for_cvss_score(cvss_score=v.get_cvss_score()) + table_instance.table + Fore.RESET) + print(OssCommand._get_color_for_cvss_score(cvss_score=v.get_cvss_score())) + self._console.print(table) + print(Fore.RESET) @staticmethod def _get_color_for_cvss_score(cvss_score: float = 0.0): @@ -229,7 +228,6 @@ def _get_color_for_cvss_score(cvss_score: float = 0.0): else: return Fore.GREEN - @staticmethod def _get_severity_for_cvss_score(cvss_score: float = None) -> str: if cvss_score >= 9.0: diff --git a/poetry.lock b/poetry.lock index 2903bcf..8219b4a 100644 --- a/poetry.lock +++ b/poetry.lock @@ -368,14 +368,6 @@ category = "main" optional = false python-versions = ">=2.6" -[[package]] -name = "terminaltables" -version = "3.1.7" -description = "Generate simple tables in terminals from a nested list of strings." -category = "main" -optional = false -python-versions = ">=2.6" - [[package]] name = "tinydb" version = "4.5.2" @@ -475,7 +467,7 @@ testing = ["pytest (>=4.6)", "pytest-checkdocs (>=2.4)", "pytest-flake8", "pytes [metadata] lock-version = "1.1" python-versions = "^3.6.2" -content-hash = "969ad69aa8fcc7d8fe0fa68c7e39a7d3b89eef0017e5287af08e56a4129201c9" +content-hash = "9ba30e6c57313dc840962ae334cb63d3bfeb2a2904916c6c1ec66479666f505c" [metadata.files] "backports.entry-points-selectable" = [ @@ -690,10 +682,6 @@ six = [ termcolor-whl = [ {file = "termcolor_whl-1.1.2-py2.py3-none-any.whl", hash = "sha256:3e7eda7348bb90ddea2d7a2171df65ed4a37adf62574fbd5459198410fdba881"}, ] -terminaltables = [ - {file = "terminaltables-3.1.7-py2.py3-none-any.whl", hash = "sha256:18ba14fdfbc6a6d7dcf65aa0871a5dbea81d7d324ecfad8dc8c2f94b3efb4f2e"}, - {file = "terminaltables-3.1.7.tar.gz", hash = "sha256:5dab2f33927c0a020b8011c81b92830ff9fd4ba701657da5d7bfdc41048360a6"}, -] tinydb = [ {file = "tinydb-4.5.2-py3-none-any.whl", hash = "sha256:3c5e5c72c98db07e707be4e25f9e135a8a14b96938e4745b1b7187fec523ff58"}, {file = "tinydb-4.5.2.tar.gz", hash = "sha256:7d18b2d0217827c188f177cd23df60e5cd5316a717e836a8e21c8c2488262cf5"}, diff --git a/pyproject.toml b/pyproject.toml index a882fcc..f5adcd2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -48,7 +48,6 @@ colorama = "^0.4.4" tinydb = "^4.5.1" PyYAML = "^5.4.1" requests = "^2.26.0" -terminaltables = "^3.1.7" cyclonedx-python-lib = "^0.10.2" polling2 = "^0.5.0" ossindex-lib = "^0.2.1"