WIP: md5 create index

This commit is contained in:
JohnE 2015-11-16 15:57:28 -08:00
parent 2d6ad6ae56
commit 2417001be2
4 changed files with 66 additions and 22 deletions

10
dbfp.py
View File

@ -21,20 +21,20 @@ def main():
# #
def compareFingerprintDir(filein, fpdir): def compareFingerprintDir(filein, fpdir):
db = fingerprint.DBSchema() db = fingerprint.FingerprintDB()
fp = FingerprintIndex() fp = FingerprintIndex()
fp.createIndex() fp.openIndex()
# #
def compareFingerprint(filein, filejson): def compareFingerprint(filein, filejson):
db = fingerprint.DBSchema() db = fingerprint.FingerprintDB()
db.scanDBFile(filein) db.scanDBFile(filein)
percent = db.compareDB(filejson) percent = db.compareDB(filejson)
print "Percent match: {}".format(str(percent)) print "Percent match: {}".format(str(percent))
# #
def createFingerprint(filein, verbose, app_name, app_ver, notes): def createFingerprint(filein, verbose, app_name, app_ver, notes):
db = fingerprint.DBSchema() db = fingerprint.FingerprintDB()
retVal = db.scanDBFile(filein) retVal = db.scanDBFile(filein)
if (retVal > 0): if (retVal > 0):
if verbose: if verbose:
@ -76,7 +76,7 @@ def __createFingerprint(dir_name):
except: except:
# not finding a databases folder is normal, not all apps use sqlite # not finding a databases folder is normal, not all apps use sqlite
return return
db = fingerprint.DBSchema() db = fingerprint.FingerprintDB()
fdir = FP_BASE_DIR + os.path.sep + dir_name fdir = FP_BASE_DIR + os.path.sep + dir_name
if (not mkdir(fdir)): if (not mkdir(fdir)):
logging.error('Error creating directory "{}"'.format(fdir)) logging.error('Error creating directory "{}"'.format(fdir))

View File

@ -3,6 +3,9 @@
# #
class FingerprintWrite(Exception): class FingerprintWrite(Exception):
"""Problem writing the fingerprint to a file""" """Error writing the fingerprint to a file"""
pass pass
class FingerprintIndexWrite(Exception):
"""Error creating an index file"""
pass

View File

@ -25,7 +25,7 @@ delimeter = "|"
# sql text # sql text
# ); # );
# #
class DBSchema: class FingerprintDB:
""" """
This class represents a complete database schema This class represents a complete database schema
Helper functions: Helper functions:
@ -92,7 +92,7 @@ class DBSchema:
finally: finally:
fh.close() fh.close()
except Exception, ex: except Exception, ex:
print ex logging.error(ex)
raise FingerprintWrite("Problem writing the fingerprint to a file") raise FingerprintWrite("Problem writing the fingerprint to a file")
# #
@ -139,7 +139,7 @@ class DBSchema:
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 ]]"
newTable = TableDefinition() newTable = TableSchema()
newTable.importTable(table_name, tb[table_name], dbmt[table_name], dbht[table_name]) newTable.importTable(table_name, tb[table_name], dbmt[table_name], dbht[table_name])
tables[table_name] = newTable tables[table_name] = newTable
@ -228,7 +228,7 @@ class DBSchema:
# sqlmaster = "SELECT name, sql FROM sqlite_master WHERE type='table'" # sqlmaster = "SELECT name, sql FROM sqlite_master WHERE type='table'"
def __readDatabase(self): def __readDatabase(self):
for row in self.cur.execute(self.sqlmaster): for row in self.cur.execute(self.sqlmaster):
newTable = TableDefinition() newTable = TableSchema()
newTable.loadTable(row[0], row[1]) newTable.loadTable(row[0], row[1])
self.tableNames.append(newTable.name()) self.tableNames.append(newTable.name())
self.tables[newTable.name()] = newTable self.tables[newTable.name()] = newTable
@ -308,7 +308,7 @@ class DBSchema:
# #
# #
# #
class TableDefinition: class TableSchema:
""" """
This class represents the definition of database table This class represents the definition of database table
""" """

View File

@ -3,27 +3,43 @@
# [ md5_all, md5_list, file_name ] # [ md5_all, md5_list, file_name ]
# #
# #
import os
import sys
import logging import logging
import sqlite3 as sql import sqlite3 as sql
from libs import fingerprint from libs.fingerprint import FingerprintDB
# prefixed with "_" so that it will be listed first and visible
INDEX_FILENAME = '_index_dpfp.db'
class FingerprintIndex: class FingerprintIndex:
""" """
Create and use an index to Class handling an index of fingerprints for effeciently locating a fingerprint
""" """
# #
def __init__(self): def __init__(self):
self.db_conn = None
return return
def openIndex(self): def openIndex(self, fpdir):
pass fq_fpidx = fpdir + os.path.sep + INDEX_FILENAME
def createIndex(self):
try: try:
conn = sql.connect('test123.db') if (os.path.isfile(fq_fpidx)):
conn.execute(''' self.db_conn = sql.connect(fq_fpidx)
except:
self.db_conn = None
logging.info("No index file found, creating index now...")
# create the Index file, also populate it with entrieds from the fingerprint
self.creatIndex(fpdir)
#
def createIndex(self, fpdir):
fq_fpidx = fpdir + os.path.sep + INDEX_FILENAME
try:
self.db_conn = sql.connect(fq_fpidx)
self.db_conn.execute('''
CREATE TABLE md5_index ( CREATE TABLE md5_index (
md5_all TEXT PRIMARY KEY, md5_all TEXT PRIMARY KEY,
md5_list TEXT, md5_list TEXT,
@ -33,10 +49,35 @@ class FingerprintIndex:
except: except:
logging.error("Error creating index table") logging.error("Error creating index table")
finally: finally:
if conn: if self.db_conn:
conn.close() self.db_conn.close()
self.db_conn = None
raise FingerprintIndexWrite("Error creating an index file")
def checkIntegrity(self): self.__populateIndex(fpdir)
def __populateIndex(self, fpdir):
#
# read each file, pull md5, add row to database
failCount = 0
finCount = 0
try:
db = FingerprintDB()
files = os.listdir(fpdir)
for file in Files:
try:
db.scanDBFile(fpdir)
finCount = finCount+1
except:
failCount = failCount+1
except:
logging.info("Completed populating the index. Completed: {} Failed: {} ".format(str(finCount), str(failCount)))
pass
#
def __checkIntegrity(self):
""" Sanity check the number of files against the index rows """ """ Sanity check the number of files against the index rows """
pass pass