-
Notifications
You must be signed in to change notification settings - Fork 37
/
db_ops.py
420 lines (378 loc) · 19 KB
/
db_ops.py
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#!/usr/bin/python3
from sqlalchemy import create_engine
from sqlalchemy import func
from sqlalchemy import MetaData # to get table data
from sqlalchemy import Table
from sqlalchemy.sql import select # to select data
from sqlalchemy.sql import text # for text query
from sqlalchemy.exc import IntegrityError
from sqlalchemy.sql.expression import join
from sqlalchemy import desc
from sqlalchemy.sql import and_, or_, not_
import conf
import datetime
import configparser
import json
import os
class DbOps:
"""Class that contains database queries and inserts for the program."""
def __init__(self, username, password, host, database):
self.user = username
self.pw = password
self.host = host
self.db = database
def create_conn(self):
"""Connect to database."""
usr = self.user
pw = self.pw
host = self.host
database = self.db
db_connect_string = 'mysql+pymysql://' + usr + ':' + pw + '@' + host + '/' + database
engine = create_engine(db_connect_string)
conn = engine.connect()
return conn, engine
def get_table(self, table_name, engine):
"""Get table metadata."""
meta = MetaData(engine)
meta.reflect()
table = Table(table_name, meta, autoload=True)
return table
def test_str_length(self):
print("testing")
def get_recent_hash(self, r_id):
"""Get the hash of the most recent commit."""
cnxn, enxn = self.create_conn()
result = ''
tbl = self.get_table('repo_info', enxn)
stmt = select([tbl.c.repo_latest_commit]).where(tbl.c.repo_id == r_id)
try:
res = cnxn.execute(stmt)
except:
res = 'no commit in db'
for row in res:
result = row[0]
res.close()
return result
def update_repo_info(self, in_list, commit_hash):
"""Update repo_info table with repository metadata."""
cnxn, enxn = self.create_conn()
ins = self.get_table('repo_info', enxn).insert()
upd_table = self.get_table('repo_info', enxn)
if in_list[4]:
repo_d = self.truncate(in_list[4], 4096)
else:
repo_d = 'n/a'
try:
ins.execute(repo_id=in_list[7],
repo_owner_id=in_list[8],
repo_user=in_list[5],
repo_name=in_list[6],
repo_full_name=in_list[3],
repo_updated__ts=in_list[9],
repo_size=in_list[1],
repo_cloned=in_list[10],
repo_description=repo_d,
repo_latest_commit=commit_hash)
except IntegrityError as e:
timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
upd_table.update().values(repo_last_checked=timestamp,
repo_latest_comit=commit_hash,
repo_cloned=in_list[10]).where(upd_table.c.repo_id == in_list[7])
def update_repo_search_results(self, in_list_repo, in_list_matches, search_type):
"""Update repo_search_results table with keyword/regex matches."""
cnxn, enxn = self.create_conn()
ins = self.get_table('repo_search_results', enxn).insert()
upd = self.get_table('repo_search_results', enxn).update()
added = 0
added_list = []
for item in in_list_matches:
if search_type == 'git_history':
matching_line = item['Match Line'][1:]
else:
matching_line = item['Match Line']
try:
ins.execute(match_repo_id=in_list_repo[7],
match_type=self.truncate(item['Match Type'], 256),
match_string=self.truncate(item['Match Value'], 256),
match_location=self.truncate(item['Match Path'], 2048),
match_line=self.truncate(matching_line, 8192),
match_item_entropy=item['Match Entropy'],
match_line_hash=item['Match Line Hash'],
match_update_type=item['Match Add or Del'],
match_commit_hash=item['Match Commit Hash'],
match_commit_author=item['Match Commit Author'],
match_commit_time=item['Match Commit Time'],
match_commit_message=self.truncate(item['Match Commit Message'], 1024),
match_original_query=item['Match Original Query'])
added += 1
added_dict = {"Repo Name": in_list_repo[6], "Repo Author": in_list_repo[5],
"Query": item['Match Original Query'], "Type": self.truncate(item['Match Type'], 32),
"Path": self.r_truncate(item['Match Path'], 128),
"Line": self.truncate(matching_line, 256),
"Item Entropy": item['Match Entropy'], "Add or Del": item['Match Add or Del'],
"Commit Author": item['Match Commit Author'], "Commit Time": item['Match Commit Time']}
added_list.append(added_dict)
except Exception as e: # IntegrityError:
pass
return added, added_list
def search_for_repo(self):
# search for a repo (repo_info)
print("testing")
def search_for_repo_match(self):
# search for a repo match
print("testing")
def display_repos(self, post_dict):
"""Query and display repo info based on filtering items in web app."""
# display list of repos, based on some constraint (time)
cnxn, enxn = self.create_conn()
r_info = self.get_table('repo_info', enxn)
if post_dict:
if post_dict['r_user'] == '':
post_dict['r_user'] = '%'
if post_dict['r_name'] == '':
post_dict['r_name'] == '%'
if post_dict['r_cloned'] == 'Any':
post_dict['r_cloned'] = '%'
if post_dict['r_desc'] == '':
post_dict['r_desc'] = '%'
if post_dict['r_checked'] == '':
post_dict['r_checked'] = '%'
r_u = post_dict['r_user']
r_n = post_dict['r_name']
r_c = post_dict['r_cloned']
r_d = post_dict['r_desc']
r_chk = post_dict['r_checked']
if r_c == '%':
stmt = select(
[r_info.c.repo_id, r_info.c.repo_owner_id, r_info.c.repo_user,
r_info.c.repo_name, r_info.c.repo_size, r_info.c.repo_cloned,
r_info.c.repo_description, r_info.c.repo_last_checked,
r_info.c.repo_latest_commit]).where(
and_(
r_info.c.repo_user.like(f'%{r_u}%'),
r_info.c.repo_name.like(f'%{r_n}%'),
r_info.c.repo_cloned.like(f'%{r_c}%'),
r_info.c.repo_description.like(f'%{r_d}%'),
# r_info.c.repo_last_checked.like(f'%{r_chk}')
)
).order_by(desc(r_info.c.repo_last_checked)).limit(
post_dict['num_res'])
else:
stmt = select(
[r_info.c.repo_id, r_info.c.repo_owner_id, r_info.c.repo_user,
r_info.c.repo_name, r_info.c.repo_size, r_info.c.repo_cloned,
r_info.c.repo_description, r_info.c.repo_last_checked,
r_info.c.repo_latest_commit]).where(
and_(
r_info.c.repo_user.like(f'%{r_u}%'),
r_info.c.repo_name.like(f'%{r_n}%'),
r_info.c.repo_cloned == r_c,
r_info.c.repo_description.like(f'%{r_d}%'),
# r_info.c.repo_last_checked.like(f'%{r_chk}')
)
).order_by(desc(r_info.c.repo_last_checked)).limit(
post_dict['num_res'])
if not post_dict:
def_results = {'num_res': 100}
post_dict.update(def_results)
stmt = select(
[r_info.c.repo_id, r_info.c.repo_owner_id, r_info.c.repo_user,
r_info.c.repo_name, r_info.c.repo_size, r_info.c.repo_cloned,
r_info.c.repo_description, r_info.c.repo_last_checked,
r_info.c.repo_latest_commit]).order_by(desc(r_info.c.repo_last_checked)).limit(post_dict['num_res'])
res = cnxn.execute(stmt)
nr = res.rowcount
return res, nr
def get_count(self, table):
cnxn, enxn = self.create_conn()
res = cnxn.execute(f"select count(*) from {table}").scalar() # stmt).scalar()
return res
def display_repos_api(self, post_dict={}):
"""API Query to get repo info."""
# display list of repos, based on some constraint (time)
cnxn, enxn = self.create_conn()
r_info = self.get_table('repo_info', enxn)
if post_dict:
# handle filters and bring back appropriate results
# Post Dict Example:
# {"num_res":33, "page":2, "repo_user": "johndoe", "repo_name": "secretrepo",
# "repo_cloned": ["cloned", "not_cloned"], "repo_desc": "config"}
page = post_dict['page']
if page > 1:
offset = (page - 1) * 100
elif page == 1:
offset = 0
if post_dict["repo_cloned"] != '%':
stmt = select(
[r_info.c.repo_id, r_info.c.repo_owner_id, r_info.c.repo_user,
r_info.c.repo_name, r_info.c.repo_full_name, r_info.c.repo_updated_ts,
r_info.c.repo_size, r_info.c.repo_cloned,
r_info.c.repo_description, r_info.c.repo_last_checked,
r_info.c.repo_latest_commit]).where(
and_(
r_info.c.repo_user.like(f'%{post_dict["repo_user"]}%'),
r_info.c.repo_name.like(f'%{post_dict["repo_name"]}%'),
r_info.c.repo_cloned == post_dict["repo_cloned"],
r_info.c.repo_description.like(f'%{post_dict["repo_desc"]}%'),
# r_info.c.repo_last_checked.like(f'%{r_chk}')
)
).order_by(desc(r_info.c.repo_last_checked)).limit(
post_dict['num_res']).offset(offset)
else:
stmt = select(
[r_info.c.repo_id, r_info.c.repo_owner_id, r_info.c.repo_user,
r_info.c.repo_name, r_info.c.repo_full_name, r_info.c.repo_updated_ts,
r_info.c.repo_size, r_info.c.repo_cloned,
r_info.c.repo_description, r_info.c.repo_last_checked,
r_info.c.repo_latest_commit]).where(
and_(
r_info.c.repo_user.like(f'%{post_dict["repo_user"]}%'),
r_info.c.repo_name.like(f'%{post_dict["repo_name"]}%'),
r_info.c.repo_cloned.like(f'%{post_dict["repo_cloned"]}%'),
r_info.c.repo_description.like(f'%{post_dict["repo_desc"]}%'),
# r_info.c.repo_last_checked.like(f'%{r_chk}')
)
).order_by(desc(r_info.c.repo_last_checked)).limit(
post_dict['num_res']).offset(offset)
if not post_dict:
page = 1
stmt = select(
[r_info.c.repo_id, r_info.c.repo_owner_id, r_info.c.repo_user,
r_info.c.repo_name, r_info.c.repo_full_name, r_info.c.repo_updated_ts,
r_info.c.repo_size, r_info.c.repo_cloned,
r_info.c.repo_description, r_info.c.repo_last_checked,
r_info.c.repo_latest_commit]).order_by(desc(r_info.c.repo_last_checked)).limit(100)
res = cnxn.execute(stmt)
nq = res.rowcount #get the count of results for search
return res, page, nq
def display_match_results(self, num_results, post_dict={}):
"""Query and display repo search results based on filtering items in web app."""
cnxn, enxn = self.create_conn()
r_info = self.get_table('repo_info', enxn)
r_res = self.get_table('repo_search_results', enxn)
join_obj = r_res.join(r_info, r_info.c.repo_id == r_res.c.match_repo_id)
if post_dict:
if post_dict['r_user'] == '':
post_dict['r_user'] = '%'
if post_dict['r_name'] == '':
post_dict['r_name'] = '%'
if post_dict['m_string'] == '':
post_dict['m_string'] = '%'
if post_dict['m_line'] == '':
post_dict['m_line'] = '%'
if post_dict['m_location'] == '':
post_dict['m_location'] = '%'
if post_dict['m_type'] == 'Any':
post_dict['m_type'] = '%'
if post_dict['m_type'] == '':
post_dict['m_type'] = '%'
m_s = post_dict['m_string']
m_ln = post_dict['m_line']
m_l = post_dict['m_location']
m_n = post_dict['r_name']
m_u = post_dict['r_user']
m_t = post_dict['m_type']
stmt = select(
[r_info.c.repo_user, r_info.c.repo_name, r_res.c.match_inserted, r_res.c.match_type,
r_res.c.match_string, r_res.c.match_line, r_res.c.match_location, r_res.c.match_update_type,
r_res.c.match_commit_message,
r_info.c.repo_description]).select_from(join_obj).where(
and_(
r_info.c.repo_user.like(f'%{m_u}%'),
r_info.c.repo_name.like(f'%{m_n}%'),
r_res.c.match_type.like(f'%{m_t}%'),
r_res.c.match_string.like(f'%{m_s}%'),
r_res.c.match_line.like(f'%{m_ln}%'),
r_res.c.match_location.like(f'%{m_l}%')
)
).order_by(desc(r_res.c.match_inserted)).limit(
post_dict['num_res'])
if not post_dict:
def_results = {'num_res': 100}
post_dict.update(def_results)
stmt = select(
[r_info.c.repo_user, r_info.c.repo_name, r_res.c.match_inserted, r_res.c.match_type,
r_res.c.match_string, r_res.c.match_line, r_res.c.match_location, r_res.c.match_update_type,
r_res.c.match_commit_message,
r_info.c.repo_description]).select_from(join_obj).order_by(desc(r_res.c.match_inserted)).limit(
post_dict['num_res'])
res = cnxn.execute(stmt)
nr = res.rowcount
return res, nr, stmt
def display_results_api(self, post_dict={}):
"""API Query to get results info."""
cnxn, enxn = self.create_conn()
r_res = self.get_table('repo_search_results', enxn)
if post_dict:
print(post_dict)
# Post Dict Example:
# {"num_res":33, "page": 2, "match_type": "Password", "match_string": "secret",
# "match_location": "config", "match_line": "jdbc", "match_update_type": ["+","-"],
# "match_author": "John Doe", "match_message": "config"}
page = post_dict['page']
if page > 1:
offset = (page - 1) * 100
elif page == 1:
offset = 0
count_stmt = res_stmt = select(
[r_res.c.match_master_id, r_res.c.match_repo_id, r_res.c.match_inserted, r_res.c.match_type,
r_res.c.match_string, r_res.c.match_location, r_res.c.match_line, r_res.c.match_item_entropy,
r_res.c.match_line_hash, r_res.c.match_update_type, r_res.c.match_commit_hash,
r_res.c.match_commit_author,
r_res.c.match_commit_time, r_res.c.match_commit_message]).where(
and_(
r_res.c.match_type.like(f'%{post_dict["match_type"]}%'),
r_res.c.match_string.like(f'%{post_dict["match_string"]}%'),
r_res.c.match_location.like(f'%{post_dict["match_location"]}%'),
r_res.c.match_line.like(f'%{post_dict["match_line"]}%'),
r_res.c.match_update_type.like(f'%{post_dict["match_update_type"]}%'),
r_res.c.match_commit_author.like(f'%{post_dict["match_author"]}%'),
r_res.c.match_commit_message.like(f'%{post_dict["match_message"]}%')
)
).order_by(desc(r_res.c.match_master_id))
res_stmt = select(
[r_res.c.match_master_id, r_res.c.match_repo_id, r_res.c.match_inserted, r_res.c.match_type,
r_res.c.match_string, r_res.c.match_location, r_res.c.match_line, r_res.c.match_item_entropy,
r_res.c.match_line_hash, r_res.c.match_update_type, r_res.c.match_commit_hash,
r_res.c.match_commit_author,
r_res.c.match_commit_time, r_res.c.match_commit_message]).where(
and_(
r_res.c.match_type.like(f'%{post_dict["match_type"]}%'),
r_res.c.match_string.like(f'%{post_dict["match_string"]}%'),
r_res.c.match_location.like(f'%{post_dict["match_location"]}%'),
r_res.c.match_line.like(f'%{post_dict["match_line"]}%'),
r_res.c.match_update_type.like(f'%{post_dict["match_update_type"]}%'),
r_res.c.match_commit_author.like(f'%{post_dict["match_author"]}%'),
r_res.c.match_commit_message.like(f'%{post_dict["match_message"]}%')
)
).order_by(desc(r_res.c.match_master_id)).limit(
post_dict['num_res']).offset(offset)
cnt = cnxn.execute(count_stmt)
nq = cnt.rowcount # get the count of results for search
if not post_dict:
page = 1
res_stmt = select(
[r_res.c.match_master_id, r_res.c.match_repo_id, r_res.c.match_inserted, r_res.c.match_type,
r_res.c.match_string, r_res.c.match_location, r_res.c.match_line, r_res.c.match_item_entropy,
r_res.c.match_line_hash, r_res.c.match_update_type, r_res.c.match_commit_hash, r_res.c.match_commit_author,
r_res.c.match_commit_time, r_res.c.match_commit_message]).order_by(desc(r_res.c.match_master_id)).limit(100)
nq = self.get_count(r_res)
res = cnxn.execute(res_stmt)
return res, page, nq
def truncate(self, in_line, max_len):
"""Truncate line."""
if in_line is not None:
if len(in_line) > max_len:
return in_line[:max_len - 1]
else:
return in_line
def r_truncate(self, in_line, max_len):
"""Truncate line - from the left side."""
if in_line is not None:
if len(in_line) > max_len:
return in_line[max_len - 1:]
else:
return in_line
if __name__ == "__main__":
x = DbOps()
x.display_match_results()