Skip to content

Commit 341173f

Browse files
committed
Add retry on database operations
1 parent 60768ee commit 341173f

File tree

1 file changed

+66
-15
lines changed

1 file changed

+66
-15
lines changed

srum_dump2.py

+66-15
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import urllib.request
2323
import subprocess
2424
import ctypes
25+
import time
2526

2627

2728
def BinarySIDtoStringSID(sid_str):
@@ -155,7 +156,11 @@ def load_srumid_lookups(database):
155156
id_lookup = {}
156157
#Note columns 0 = Type, 1 = Index, 2 = Value
157158
lookup_table = database.get_table_by_name('SruDbIdMapTable')
158-
column_lookup = dict([(x.name,index) for index,x in enumerate(lookup_table.columns)])
159+
column_lookup = dict([(x.name,index) for index,x in enumerate(lookup_table.columns)])
160+
num_lookups = ese_table_record_count(lookup_table)
161+
if not num_lookups:
162+
print(f"\nUnexpectedly. The number of records in the lookup table is zero.")
163+
return ""
159164
for rec_entry_num in range(lookup_table.number_of_records):
160165
bin_blob = smart_retrieve(lookup_table,rec_entry_num, column_lookup['IdBlob'])
161166
if smart_retrieve(lookup_table,rec_entry_num, column_lookup['IdType'])==3:
@@ -314,23 +319,72 @@ def format_output(val, eachformat, eachstyle, xls_sheet):
314319
new_cell.value = re.sub(r'[\000-\010]|[\013-\014]|[\016-\037]|[\x00-\x1f\x7f-\x9f]|[\uffff]',"",val)
315320
return new_cell
316321

322+
def ese_table_guid_to_name(ese_table):
323+
if ese_table.name in template_tables:
324+
tname,tfields = template_tables.get(ese_table.name)
325+
else:
326+
tname = ese_table.get_name()
327+
return tname
328+
329+
def ese_table_get_record(ese_table, row_num):
330+
retry = 5
331+
if row_num >= ese_table_record_count(ese_table):
332+
return None
333+
while retry:
334+
try:
335+
ese_row = ese_table.get_record(row_num)
336+
except Exception as e:
337+
retry -= 1
338+
time.sleep(0.1)
339+
error = e
340+
else:
341+
break
342+
else:
343+
tname = ese_table_guid_to_name(ese_table)
344+
print("Skipping corrupt row {0} in the {1} table. Because {2}".format(row_num, tname, str(error)))
345+
ese_row = None
346+
return ese_row
347+
348+
def ese_table_record_count(ese_table):
349+
retry = 5
350+
while retry:
351+
try:
352+
total_recs = ese_table.get_number_of_records()
353+
except:
354+
retry -= 1
355+
time.sleep(0.1)
356+
else:
357+
break
358+
else:
359+
tname = ese_table_guid_to_name(x)
360+
print(f"Table {tname} has an invalid number of records. {str(total_recs)}")
361+
total_recs = 0
362+
return total_recs
363+
317364

318365
def process_srum(ese_db, target_wb ):
319366
"""Process all the tables and columns in the ESE database"""
320-
total_recs = sum([x.number_of_records for x in ese_db.tables if x.name not in skip_tables])
367+
total_recs = 0
368+
for each_table in ese_db.tables:
369+
if each_table.name in skip_tables:
370+
continue
371+
total_recs += ese_table_record_count(each_table)
372+
321373
if not options.quiet:
322374
print("Processing {} records across {} tables".format(total_recs,ese_db.number_of_tables-len(skip_tables)))
323375
for table_num in range(ese_db.number_of_tables):
324376
ese_table = ese_db.get_table(table_num)
325377
if ese_table.name in skip_tables:
326378
continue
327-
if ese_table.name in template_tables:
328-
tname,tfields = template_tables.get(ese_table.name)
329-
else:
330-
tname = ese_table.name[1:15]
379+
380+
tname = ese_table_guid_to_name(ese_table)
381+
num_recs = ese_table_record_count(ese_table)
382+
if not num_recs:
383+
print(f"\nSkipping table with zero of records. {tname}")
384+
continue
331385

332386
if not options.quiet:
333-
print("\nNow dumping table {} containing {} rows".format(tname, ese_table.number_of_records))
387+
print("\nNow dumping table {} containing {} rows".format(tname, num_recs or "Unknown"))
334388
print("While you wait, did you know ...\n {} \n".format(next(ads)))
335389

336390
xls_sheet = target_wb.create_sheet(title=tname)
@@ -353,17 +407,14 @@ def process_srum(ese_db, target_wb ):
353407
header_row.append(WriteOnlyCell(xls_sheet, value=eachcol.name))
354408
xls_sheet.append(header_row)
355409

356-
for row_num in range(ese_table.number_of_records):
357-
try:
358-
ese_row = ese_table.get_record(row_num)
359-
except Exception as e:
360-
print("Skipping corrupt row {0} in the {1} table. Because {2}".format(row_num, ese_table.name, str(e)))
361-
continue
410+
for row_num in range(num_recs):
411+
412+
ese_row = ese_table_get_record(ese_table,row_num)
362413
if ese_row == None:
363414
continue
364415

365416
if not options.quiet and row_num % 500 == 0:
366-
print("\r|{0:-<50}| {1:3.2f}%".format("X"*( 50 * row_num//ese_table.number_of_records), 100*row_num/ese_table.number_of_records ),end="")
417+
print("\r|{0:-<50}| {1:3.2f}%".format("X"*( 50 * row_num//num_recs), 100*row_num/num_recs ),end="")
367418

368419
#The row is retrieved now use the template to figure out which ones you want and format them
369420
xls_row = []
@@ -519,7 +570,7 @@ def extract_live_file():
519570
[sg.OK(), sg.Cancel()]]
520571

521572
# Create the Window
522-
window = sg.Window('SRUM_DUMP 2.5', layout)
573+
window = sg.Window('SRUM_DUMP 2.6', layout)
523574

524575
while True:
525576
event, values = window.Read()

0 commit comments

Comments
 (0)