|
| 1 | +import argparse |
| 2 | +import os |
| 3 | +import sqlite3 |
| 4 | + |
| 5 | +from cve_bin_tool.cvedb import DBNAME, DISK_LOCATION_DEFAULT |
| 6 | +from cve_bin_tool.mismatch_loader import run_mismatch_loader |
| 7 | + |
| 8 | +parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 9 | +data_dir = os.path.join(parent_dir, "data") |
| 10 | +dbpath = DISK_LOCATION_DEFAULT / DBNAME |
| 11 | + |
| 12 | + |
| 13 | +def lookup(purl, db_file): |
| 14 | + """ |
| 15 | + Looks up the vendor information for a given purl in the mismatch database. |
| 16 | +
|
| 17 | + Args: |
| 18 | + purl (str): The package URL to lookup in the mismatch database. |
| 19 | + db_file (str): The file path to the SQLite database file. |
| 20 | +
|
| 21 | + """ |
| 22 | + conn = sqlite3.connect(db_file) |
| 23 | + cursor = conn.cursor() |
| 24 | + |
| 25 | + try: |
| 26 | + cursor.execute("SELECT vendor FROM mismatch WHERE purl = ?", (purl,)) |
| 27 | + result = cursor.fetchall() |
| 28 | + |
| 29 | + if result: |
| 30 | + formatted_result = ", ".join([row[0] for row in result]) |
| 31 | + print(formatted_result) |
| 32 | + else: |
| 33 | + print("Error: No data found for the provided purl.") |
| 34 | + except sqlite3.Error as e: |
| 35 | + print(f"Database error: {e}") |
| 36 | + finally: |
| 37 | + conn.close() |
| 38 | + |
| 39 | + |
| 40 | +def loader(data_dir, db_file): |
| 41 | + """ |
| 42 | + Sets up or refreshes the mismatch database using data from the specified directory. |
| 43 | +
|
| 44 | + Args: |
| 45 | + data_dir (str): The directory containing the data files to be loaded into the mismatch database. |
| 46 | + db_file (str): The file path to the SQLite database file. |
| 47 | +
|
| 48 | + """ |
| 49 | + if run_mismatch_loader(data_dir, db_file): |
| 50 | + print("Mismatch database setup completed successfully.") |
| 51 | + else: |
| 52 | + print("Mismatch database setup failed.") |
| 53 | + |
| 54 | + |
| 55 | +def main(): |
| 56 | + parser = argparse.ArgumentParser(description="Mismatch Database Management Tool") |
| 57 | + subparsers = parser.add_subparsers(dest="command") |
| 58 | + |
| 59 | + # Subparser for the lookup command |
| 60 | + lookup_parser = subparsers.add_parser( |
| 61 | + "lookup", help="Look up vendor information for a given purl" |
| 62 | + ) |
| 63 | + lookup_parser.add_argument( |
| 64 | + "purl", type=str, help="The package URL to lookup in the mismatch database" |
| 65 | + ) |
| 66 | + lookup_parser.add_argument( |
| 67 | + "--database", dest="db_file", default=dbpath, help="SQLite DB file location" |
| 68 | + ) |
| 69 | + |
| 70 | + # Subparser for the loader command |
| 71 | + loader_parser = subparsers.add_parser( |
| 72 | + "loader", help="Set up or refresh the mismatch database" |
| 73 | + ) |
| 74 | + loader_parser.add_argument( |
| 75 | + "--dir", dest="data_dir", default=data_dir, help="Data folder location" |
| 76 | + ) |
| 77 | + loader_parser.add_argument( |
| 78 | + "--database", dest="db_file", default=dbpath, help="SQLite DB file location" |
| 79 | + ) |
| 80 | + |
| 81 | + args = parser.parse_args() |
| 82 | + |
| 83 | + if args.command == "lookup": |
| 84 | + lookup(args.purl, args.db_file) |
| 85 | + elif args.command == "loader": |
| 86 | + loader(args.data_dir, args.db_file) |
| 87 | + else: |
| 88 | + loader(data_dir, dbpath) |
| 89 | + |
| 90 | + |
| 91 | +if __name__ == "__main__": |
| 92 | + main() |
0 commit comments