-
Notifications
You must be signed in to change notification settings - Fork 481
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: convert mismatch utility into a standalone entity (#4300)
Signed-off-by: Meet Soni <meetsoni3017@gmail.com>
- Loading branch information
Showing
3 changed files
with
118 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import argparse | ||
import os | ||
import sqlite3 | ||
|
||
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 | ||
|
||
|
||
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]) | ||
print(formatted_result) | ||
else: | ||
print("Error: No data found for the provided purl.") | ||
except sqlite3.Error as e: | ||
print(f"Database error: {e}") | ||
finally: | ||
conn.close() | ||
|
||
|
||
def loader(data_dir, db_file): | ||
""" | ||
Sets up or refreshes the mismatch database using data from the specified directory. | ||
Args: | ||
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): | ||
print("Mismatch database setup completed successfully.") | ||
else: | ||
print("Mismatch database setup failed.") | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser(description="Mismatch Database Management Tool") | ||
subparsers = parser.add_subparsers(dest="command") | ||
|
||
# Subparser for the lookup command | ||
lookup_parser = subparsers.add_parser( | ||
"lookup", help="Look up vendor information for a given purl" | ||
) | ||
lookup_parser.add_argument( | ||
"purl", type=str, help="The package URL to lookup in the mismatch database" | ||
) | ||
lookup_parser.add_argument( | ||
"--database", dest="db_file", default=dbpath, help="SQLite DB file location" | ||
) | ||
|
||
# Subparser for the loader command | ||
loader_parser = subparsers.add_parser( | ||
"loader", help="Set up or refresh the mismatch database" | ||
) | ||
loader_parser.add_argument( | ||
"--dir", dest="data_dir", default=data_dir, help="Data folder location" | ||
) | ||
loader_parser.add_argument( | ||
"--database", dest="db_file", default=dbpath, help="SQLite DB file location" | ||
) | ||
|
||
args = parser.parse_args() | ||
|
||
if args.command == "lookup": | ||
lookup(args.purl, args.db_file) | ||
elif args.command == "loader": | ||
loader(args.data_dir, args.db_file) | ||
else: | ||
loader(data_dir, dbpath) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters