Skip to content

Commit d2fa085

Browse files
authored
feat: convert mismatch utility into a standalone entity (#4300)
Signed-off-by: Meet Soni <[email protected]>
1 parent 7abee04 commit d2fa085

File tree

3 files changed

+118
-11
lines changed

3 files changed

+118
-11
lines changed

cve_bin_tool/mismatch_loader.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
ON CONFLICT DO NOTHING;
2323
"""
2424

25+
db_path = DISK_LOCATION_DEFAULT / DBNAME
26+
parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
27+
data_dir = os.path.join(parent_dir, "data")
28+
2529

2630
def setup_sqlite(data_dir: str, db_file: str) -> bool:
2731
"""
@@ -72,10 +76,6 @@ def setup_args():
7276
Setup command line arguments
7377
"""
7478

75-
db_path = DISK_LOCATION_DEFAULT / DBNAME
76-
parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
77-
data_dir = os.path.join(parent_dir, "data")
78-
7979
parser = argparse.ArgumentParser(description="mismatch loader")
8080
parser.add_argument(
8181
"--dir",
@@ -95,18 +95,32 @@ def setup_args():
9595
return args
9696

9797

98+
def run_mismatch_loader(dir=data_dir, db_file=db_path):
99+
"""
100+
Runs the mismatch loader to populate the SQLite database with mismatch relationships.
101+
102+
Args:
103+
dir (str): The directory containing the data files to be processed.
104+
db_file (str): The file path to the SQLite database file.
105+
106+
Returns:
107+
bool: True if the database setup was successful, False otherwise.
108+
"""
109+
if not os.path.exists(dir) or not os.path.isdir(dir):
110+
LOGGER.error(
111+
f"Specified data directory does not exist or is not a folder: {dir}"
112+
)
113+
return False
114+
return setup_sqlite(dir, db_file)
115+
116+
98117
def main():
99118
"""
100-
Run the SQLite loader utility
119+
Run the mismatch loader utility
101120
"""
102121
args = setup_args()
103122

104-
if not os.path.exists(args.dir) or not os.path.isdir(args.dir):
105-
LOGGER.error(
106-
f"Specified data directory does not exist or is not a folder: {args.dir}"
107-
)
108-
exit(1)
109-
if not setup_sqlite(args.dir, args.database):
123+
if not run_mismatch_loader(args.dir, args.database):
110124
exit(1)
111125
exit(0)
112126

mismatch/cli.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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()

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
"console_scripts": [
7373
"cve-bin-tool = cve_bin_tool.cli:main",
7474
"csv2cve = cve_bin_tool.csv2cve:main",
75+
"mismatch = mismatch.cli:main",
7576
],
7677
},
7778
)

0 commit comments

Comments
 (0)