MOD: updated the open index for better exception handling, updated the json import and code, not using a getter for member variables because that is pointless, instead doing do.db_hash and db.table_hashes

This commit is contained in:
JohnE 2015-12-04 12:29:56 -08:00
parent c51cfb7bad
commit 8653b56e86
3 changed files with 54 additions and 37 deletions

View File

@ -6,6 +6,10 @@ class FingerprintWrite(Exception):
"""Error writing the fingerprint to a file"""
pass
class FingerprintIndexOpen(Exception):
"""Error opening an index file"""
pass
class FingerprintIndexWrite(Exception):
"""Error creating an index file"""
pass

View File

@ -43,17 +43,19 @@ class FingerprintDB:
def __init__(self):
self.conn = None
self.cur = None
self.tableNames = []
self.table_names = []
self.tables = {}
self.tablesJson = {}
self.dbName = ""
# hashtables of json data
self.tables_json = {}
self.db_hash = {}
self.table_hashes = {}
# fingerprint metadata
self.db_name = ""
self.app_name = ""
self.app_ver = ""
self.notes = ""
self.filein = ""
self.scanned = False
# self.jsonData = None
return
#
def scanDBFile(self, filein):
@ -66,9 +68,9 @@ class FingerprintDB:
try:
# extract file name from path+filename
self.dbName = ToolBox.parseFilenameIncExt(filein)
self.db_name = ToolBox.parseFilenameIncExt(filein)
except:
self.dbName = filein
self.db_name = filein
try:
# read database schema, parse the schema
self.__readDatabase()
@ -114,22 +116,22 @@ class FingerprintDB:
# import fingerprint from a json file
def importJson(self, filejson):
def importJson(self, file_json):
""" import fingerprint from a json file """
(self.tablesJson, dummy) = self.__importJsonDBSchema(filejson)
self.__importJsonDBSchema(file_json)
#
def importJsonIndex(self):
""" import fingerprint from a json file, return the MD5 sums """
(dummy, dbht) = self.__importJsonDBSchema(filejson)
return dbht
# def importJsonIndex(self, file_json):
# """ import fingerprint from a json file, return the MD5 sums """
# self.__importJsonDBSchema(file_json)
# return (self.
#
def compareDB(self, filejson):
if (not self.scanned):
return
""" return the percentage of the match between two fingerprints """
self.tablesJson = self.__importJsonDBSchema(filejson)
self.__importJsonDBSchema(filejson)
result = self.__DBSchemaCompare()
print "[ Percetage == {}]".format(result)
return result
@ -144,6 +146,7 @@ class FingerprintDB:
tb = jsonData['tables']
dbmt = jsonData['db-metadata']
dbht = jsonData['db-metadata-hashes']
dbmd5 = jsonData['db-metadata-md5']
all_tables = tb.keys()
for table_name in all_tables:
print "[[ Table <" + table_name + "> imported ]]"
@ -152,12 +155,13 @@ class FingerprintDB:
tables[table_name] = newTable
except Exception, e:
print "ERROR: problem loading json file: " + file_json
print e
return (tables, dbht)
logging.error("ERROR: problem loading json file: " + file_json + e)
self.tables_json = tables
self.db_hash = dbmd5
self.table_hashes = dbht
#
def __DBMD5Compare(self):
pass
@ -170,12 +174,12 @@ class FingerprintDB:
diff_num = 0
diff_total = 0
all_total = 0
for tableName in self.tablesJson.keys():
for tableName in self.tables_json.keys():
table = self.tables[tableName]
print "[[ Comparing Table: " + tableName + " ]]"
if (table):
if not (self.tablesJson[tableName].hash() == table.hash()):
logging.info("*** Hash difference 1:{}!={}".format(self.tablesJson[tableName].hash(), table.hash()))
if not (self.tables_json[tableName].hash() == table.hash()):
logging.info("*** Hash difference 1:{}!={}".format(self.tables_json[tableName].hash(), table.hash()))
(total, diff_num) = self.__CompareTable(self.tablesJson[tableName], table)
all_total += total
diff_total += diff_num
@ -238,7 +242,7 @@ class FingerprintDB:
for row in self.cur.execute(self.sqlmaster):
newTable = TableSchema()
newTable.loadTable(row[0], row[1])
self.tableNames.append(newTable.name())
self.table_names.append(newTable.name())
self.tables[newTable.name()] = newTable
return
@ -279,7 +283,7 @@ class FingerprintDB:
mhash['format-ver'] = self.format_ver
mhash['scanner-ver'] = self.scanner_ver
mhash['scanner-name'] = 'dbfp'
mhash['dn-name'] = self.dbName
mhash['dn-name'] = self.db_name
mhash['app-name'] = self.app_name
mhash['app-ver'] = self.app_ver
mhash['notes'] = self.notes

View File

@ -30,15 +30,16 @@ class FingerprintIndex:
try:
if (os.path.isfile(fq_fpidx)):
self.db_conn = sql.connect(fq_fpidx)
logging.info("DB Open SUCCESSFUL")
except:
self.db_conn = None
logging.info("No index file found, creating index now...")
try:
# create the Index file, also populate it with entrieds from the fingerprint
logging.info("DB Open SUCCESSFUL")
else:
logging.info("No index file found, creating index now...")
self.createIndex(fp_dir)
except:
logging.info("ERROR: issue creating the index file")
except:
raise FingerprintIndexOpen("Error opening/creating an index file")
finally:
if self.db_conn:
self.db_conn.close()
self.db_conn = None
#
def createIndex(self, fp_dir):
@ -52,11 +53,12 @@ class FingerprintIndex:
file_name TEXT);
''')
logging.info("Successfully created index table")
except:
raise FingerprintIndexWrite("Error creating an index file")
finally:
if self.db_conn:
self.db_conn.close()
self.db_conn = None
raise FingerprintIndexWrite("Error creating an index file")
self.db_conn = None
self.__populateIndex(fp_dir)
@ -69,17 +71,24 @@ class FingerprintIndex:
try:
db = FingerprintDB()
files = os.listdir(fp_dir)
for file in Files:
# print ("Populating DB, files=={}".format(files))
for file in files:
try:
fq_file = fp_dir + os.path.sep + file
dbht = db.importJsonIndex(fq_file)
md5_all = __createMD5Index(dbht)
print ("importJson file=={}".format(fq_file))
db.importJson(fq_file)
print("db_hash=={}".format(db.db_hash))
print("table_hashes={}".format(db.table_hashes))
#md5_all = __createMD5Index(dbht)
finCount = finCount+1
except:
failCount = failCount+1
except:
logging.info("Completed populating the index. Completed: {} Failed: {} ".format(str(finCount), str(failCount)))
pass
logging.info("Completed populating the index. Completed: {} Failed: {} ".format(str(finCount), str(failCount)))
#
def __insertRecord(self, md5_all, md5_list, filename):