Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: convert mismatch utility into a standalone entity #4300

Merged
merged 3 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions cve_bin_tool/mismatch_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
ON CONFLICT DO NOTHING;
"""

db_path = DISK_LOCATION_DEFAULT / DBNAME
parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
data_dir = os.path.join(parent_dir, "data")


def setup_sqlite(data_dir: str, db_file: str) -> bool:
"""
Expand Down Expand Up @@ -72,10 +76,6 @@ def setup_args():
Setup command line arguments
"""

db_path = DISK_LOCATION_DEFAULT / DBNAME
parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
data_dir = os.path.join(parent_dir, "data")

parser = argparse.ArgumentParser(description="mismatch loader")
parser.add_argument(
"--dir",
Expand All @@ -95,18 +95,32 @@ def setup_args():
return args


def run_mismatch_loader(dir=data_dir, db_file=db_path):
"""
Runs the mismatch loader to populate the SQLite database with mismatch relationships.

Args:
dir (str): The directory containing the data files to be processed.
db_file (str): The file path to the SQLite database file.

Returns:
bool: True if the database setup was successful, False otherwise.
"""
if not os.path.exists(dir) or not os.path.isdir(dir):
LOGGER.error(
f"Specified data directory does not exist or is not a folder: {dir}"
)
return False
return setup_sqlite(dir, db_file)


def main():
"""
Run the SQLite loader utility
Run the mismatch loader utility
"""
args = setup_args()

if not os.path.exists(args.dir) or not os.path.isdir(args.dir):
LOGGER.error(
f"Specified data directory does not exist or is not a folder: {args.dir}"
)
exit(1)
if not setup_sqlite(args.dir, args.database):
if not run_mismatch_loader(args.dir, args.database):
exit(1)
exit(0)

Expand Down
72 changes: 72 additions & 0 deletions mismatch/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import os
import sqlite3

import click

from cve_bin_tool.cvedb import DBNAME, DISK_LOCATION_DEFAULT
from cve_bin_tool.mismatch_loader import run_mismatch_loader

parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
data_dir = os.path.join(parent_dir, "data")
dbpath = DISK_LOCATION_DEFAULT / DBNAME


@click.group(invoke_without_command=True)
@click.pass_context
def main(ctx):
if ctx.invoked_subcommand is None:
ctx.invoke(loader)


@main.command()
@click.argument("purl")
@click.option("--database", "db_file", default=dbpath, help="SQLite DB file location")
def lookup(purl, db_file):
"""
Looks up the vendor information for a given purl in the mismatch database.

Args:
purl (str): The package URL to lookup in the mismatch database.
db_file (str): The file path to the SQLite database file.

"""
conn = sqlite3.connect(db_file)
cursor = conn.cursor()

try:
cursor.execute("SELECT vendor FROM mismatch WHERE purl = ?", (purl,))
result = cursor.fetchall()

if result:
formatted_result = ", ".join([row[0] for row in result])
click.echo(formatted_result)
else:
click.echo("Error: No data found for the provided purl.")
except sqlite3.Error as e:
click.echo(f"Database error: {e}")
finally:
conn.close()


@main.command()
@click.option("--dir", "data_dir", default=data_dir, help="Data folder location")
@click.option("--database", "db_file", default=dbpath, help="SQLite DB file location")
@click.pass_context
def loader(ctx, data_dir, db_file):
"""
Sets up or refreshes the mismatch database using data from the specified directory.

Args:
ctx (click.Context): Click context object, automatically passed when using @click.pass_context.
data_dir (str): The directory containing the data files to be loaded into the mismatch database.
db_file (str): The file path to the SQLite database file.

"""
if run_mismatch_loader(data_dir, db_file):
click.echo("Mismatch database setup completed successfully.")
else:
click.echo("Mismatch database setup failed.")


if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"console_scripts": [
"cve-bin-tool = cve_bin_tool.cli:main",
"csv2cve = cve_bin_tool.csv2cve:main",
"mismatch = mismatch.cli:main",
],
},
)
Expand Down
Loading