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""" """Error writing the fingerprint to a file"""
pass pass
class FingerprintIndexOpen(Exception):
"""Error opening an index file"""
pass
class FingerprintIndexWrite(Exception): class FingerprintIndexWrite(Exception):
"""Error creating an index file""" """Error creating an index file"""
pass pass

View File

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

View File

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