107 lines
2.5 KiB
Python
107 lines
2.5 KiB
Python
#
|
|
#
|
|
# [ md5_all, md5_list, file_name ]
|
|
#
|
|
#
|
|
import os
|
|
import sys
|
|
import logging
|
|
import sqlite3 as sql
|
|
from libs.exceptions import FingerprintIndexWrite
|
|
from libs.fingerprint import FingerprintDB
|
|
|
|
# prefixed with "_" so that it will be listed first and visible
|
|
INDEX_FILENAME = '_index_dpfp.db'
|
|
|
|
|
|
class FingerprintIndex:
|
|
"""
|
|
Class handling an index of fingerprints for effeciently locating a fingerprint
|
|
"""
|
|
|
|
#
|
|
def __init__(self):
|
|
self.db_conn = None
|
|
return
|
|
|
|
#
|
|
def openIndex(self, fp_dir):
|
|
fq_fpidx = fp_dir + os.path.sep + INDEX_FILENAME
|
|
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
|
|
self.createIndex(fp_dir)
|
|
except:
|
|
logging.info("ERROR: issue creating the index file")
|
|
|
|
#
|
|
def createIndex(self, fp_dir):
|
|
fq_fpidx = fp_dir + os.path.sep + INDEX_FILENAME
|
|
try:
|
|
self.db_conn = sql.connect(fq_fpidx)
|
|
self.db_conn.execute('''
|
|
CREATE TABLE md5_index (
|
|
md5_all TEXT PRIMARY KEY,
|
|
md5_list TEXT,
|
|
file_name TEXT);
|
|
''')
|
|
logging.info("Successfully created index table")
|
|
finally:
|
|
if self.db_conn:
|
|
self.db_conn.close()
|
|
self.db_conn = None
|
|
raise FingerprintIndexWrite("Error creating an index file")
|
|
|
|
self.__populateIndex(fp_dir)
|
|
|
|
|
|
def __populateIndex(self, fp_dir):
|
|
#
|
|
# read each file, pull md5, add row to database
|
|
failCount = 0
|
|
finCount = 0
|
|
try:
|
|
db = FingerprintDB()
|
|
files = os.listdir(fp_dir)
|
|
for file in Files:
|
|
try:
|
|
fq_file = fp_dir + os.path.sep + file
|
|
dbht = db.importJsonIndex(fq_file)
|
|
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)))
|
|
|
|
#
|
|
def __insertRecord(self, md5_all, md5_list, filename):
|
|
try:
|
|
self.db_conn.execute(
|
|
'''
|
|
INSERT INTO md5_index VALUES(?, ?, ?)
|
|
''', md5_all, md5_list, filename)
|
|
except:
|
|
pass
|
|
|
|
#
|
|
def __checkIntegrity(self):
|
|
""" Sanity check the number of files against the index rows """
|
|
pass
|
|
|
|
#
|
|
def dirCompare(self, folder):
|
|
pass
|
|
|
|
|
|
def compareFingerprint(self, fp1, fp2):
|
|
pass
|
|
|