-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathupdate_database.py
More file actions
224 lines (191 loc) · 7.04 KB
/
update_database.py
File metadata and controls
224 lines (191 loc) · 7.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import csv
import logging
import sqlite3
from pathlib import Path
from typing import Optional
import shutil
# Configure module-level logger
logger = logging.getLogger(__name__)
class DatabaseUpdater:
"""Manages updates to tool database across CSV, SQLite, and FAISS index."""
def __init__(
self,
csv_file: str = "backend/database/tool_list_database.csv",
sql_db_file: str = "backend/database/tools.db",
faiss_dir: str = "backend/faiss_index",
) -> None:
self.csv_file = Path(csv_file)
self.sql_db_file = Path(sql_db_file)
self.faiss_dir = Path(faiss_dir)
# Ensure parent directories exist
self.csv_file.parent.mkdir(parents=True, exist_ok=True)
self.sql_db_file.parent.mkdir(parents=True, exist_ok=True)
# Initialize SQLite connection and ensure table exists
self._init_sql_table()
def _init_sql_table(self) -> None:
"""Create the 'tools' table if it doesn't exist."""
with sqlite3.connect(self.sql_db_file) as conn:
conn.execute("""
CREATE TABLE IF NOT EXISTS tools (
name TEXT NOT NULL,
description TEXT,
url TEXT
)
""")
conn.commit()
def update_csv_db(
self,
name: str,
description: str,
url: str,
check_duplicate: bool = True,
) -> bool:
"""
Append a new tool to the CSV file.
Parameters
----------
name : str
Name of the tool.
description : str
Description of the tool.
url : str
URL to the tool.
check_duplicate : bool, optional
If True, skip insertion if a tool with the same (case-insensitive)
name already exists. Default is True.
Returns
-------
bool
True if the tool was appended, False if skipped due to duplication.
"""
name_norm = name.strip()
if not name_norm:
raise ValueError("Tool name cannot be empty or whitespace-only.")
# Check for duplicates if requested
if check_duplicate and self.csv_file.exists():
with self.csv_file.open("r", encoding="utf-8", newline="") as f:
reader = csv.reader(f)
for row in reader:
if row and row[0].strip().lower() == name_norm.lower():
logger.info("Skipped duplicate tool: '%s'", name_norm)
return False
# Append new entry
with self.csv_file.open("a", encoding="utf-8", newline="") as f:
writer = csv.writer(f)
writer.writerow([name_norm, description.strip(), url.strip()])
logger.debug("Appended tool to CSV: %s", name_norm)
return True
def update_sql_db(
self,
name: str,
description: str,
url: str,
check_duplicate: bool = True,
) -> bool:
"""
Insert a new tool into the SQLite database.
Parameters
----------
name : str
Name of the tool.
description : str
Description of the tool.
url : str
URL to the tool.
check_duplicate : bool, optional
If True, skip insertion if a tool with the same (case-insensitive)
name already exists. Default is True.
Returns
-------
bool
True if inserted, False if skipped due to duplication.
"""
name_norm = name.strip()
if not name_norm:
raise ValueError("Tool name cannot be empty or whitespace-only.")
with sqlite3.connect(self.sql_db_file) as conn:
cursor = conn.cursor()
if check_duplicate:
cursor.execute(
"SELECT 1 FROM tools WHERE LOWER(name) = LOWER(?) LIMIT 1",
(name_norm,)
)
if cursor.fetchone():
logger.info("Skipped duplicate tool in SQL DB: '%s'", name_norm)
return False
cursor.execute(
"INSERT INTO tools (name, description, url) VALUES (?, ?, ?)",
(name_norm, description.strip(), url.strip())
)
conn.commit()
logger.debug("Inserted tool into SQL DB: %s", name_norm)
return True
def remove_faiss_embeddings(self) -> None:
"""Remove the FAISS index directory if it exists."""
if self.faiss_dir.exists():
shutil.rmtree(self.faiss_dir)
logger.info("Removed FAISS embeddings directory: %s", self.faiss_dir)
def update_db(
self,
name: str,
description: str,
url: str,
check_duplicate: bool = True,
invalidate_faiss: bool = True,
) -> bool:
"""
Update all database components with a new tool entry.
Parameters
----------
name : str
Tool name.
description : str
Tool description.
url : str
Tool URL.
check_duplicate : bool, optional
Whether to check for duplicates in CSV and SQL. Default is True.
invalidate_faiss : bool, optional
Whether to remove the FAISS index to force re-embedding.
Default is True.
Returns
-------
bool
True if the tool was added (not skipped), False if duplicate and skipped.
Raises
------
Exception
If any operation fails (logged and re-raised).
"""
try:
# Attempt CSV and SQL insertion
#csv_added = self.update_csv_db(name, description, url, check_duplicate)
sql_added = self.update_sql_db(name, description, url, check_duplicate)
# Both should agree on duplication status
# if csv_added != sql_added:
# logger.warning(
# "Inconsistent duplicate status between CSV and SQL for tool: %s",
# name
# )
# Invalidate FAISS index if requested
if invalidate_faiss:
self.remove_faiss_embeddings()
return sql_added # or sql_added — they should match
except Exception as e:
logger.exception("Failed to update databases for tool: %s", name)
raise
updater = DatabaseUpdater()
try:
added = updater.update_db(
name="ToolHunt",
description="ToolHunt is an advanced search engine that helps you quickly find the right cybersecurity tool from a database of over 3,000 options. Just describe what you need in plain language, and its smart, elastic search will return the best matches for security pros, pentesters, and researchers.",
url="https://github.com/cyberytti/ToolHunt",
check_duplicate=True,
invalidate_faiss=True
)
if added:
print("Tool added successfully.")
else:
print("Tool was a duplicate and skipped.")
except Exception as e:
print(f"Update failed: {e}")