diff --git a/dbfp.py b/dbfp.py index 1fb6d1b..b553ed8 100644 --- a/dbfp.py +++ b/dbfp.py @@ -3,10 +3,8 @@ # import os import argparse -import time import logging from libs import fingerprint -from libs import toolbox from libs import android from subprocess import Popen, PIPE, check_call @@ -30,11 +28,10 @@ def compareFingerprint(filein, filejson): db.compareDB(filejson) # -def createFingerprint(filein, fileout, verbose, app_name, app_ver, notes): +def createFingerprint(filein, verbose, app_name, app_ver, notes): db = fingerprint.DBSchema() retVal = db.scanDBFile(filein) if (retVal > 0): - fh = open(fileout, "w") if verbose: db.debugFingerprint() if app_name: @@ -43,8 +40,7 @@ def createFingerprint(filein, fileout, verbose, app_name, app_ver, notes): db.setAppVer(app_ver) if notes: db.setNotes(notes) - db.writeFingerprint(fh) - fh.close() + db.writeFingerprint() else: print db.getErrorString(retVal) @@ -107,8 +103,6 @@ def parseArgs(): print '***** ***** ***** *****' print ' DB Fingerprint' print '***** ***** ***** *****\n' - fileout = '' - timestr = time.strftime('%Y-%m-%d_%H%M%S', time.localtime(time.time())) parser = argparse.ArgumentParser(description='Fingerprint a sqlite database based on its schema') parser.add_argument('-f', '--file', required=False) parser.add_argument('-fd', '--fpdir', required=False, help="path to dirctory of fingerprint files") @@ -122,13 +116,11 @@ def parseArgs(): args = parser.parse_args() if (args.file): - filename = toolbox.ToolBox.parseFilename(args.file) - fileout = filename + "_" + timestr + '.json' - createFingerprint(args.file, fileout, args.verbose, args.app_name, args.app_version, args.notes) + createFingerprint(args.file, args.verbose, args.app_name, args.app_version, args.notes) elif (args.file and args.fpdir): compareFingerprintDir(args.file, args.fpdir) - elif (args.fp): - compareFingerprint(args.file, args.fp) + elif (args.file and args.fingerprint): + compareFingerprint(args.file, args.fingerprint) elif (args.pull): fingerprintDir() else: diff --git a/libs/exceptions.py b/libs/exceptions.py new file mode 100644 index 0000000..a678ffa --- /dev/null +++ b/libs/exceptions.py @@ -0,0 +1,8 @@ +# +# +# + +class FingerprintWrite(Exception): + """Problem writing the fingerprint to a file""" + pass + diff --git a/libs/fingerprint.py b/libs/fingerprint.py index a953644..f9c5426 100644 --- a/libs/fingerprint.py +++ b/libs/fingerprint.py @@ -7,6 +7,7 @@ import sqlite3 import hashlib import time from libs import toolbox +from libs.exceptions import FingerprintWrite delimeter = "|" @@ -39,62 +40,94 @@ class DBSchema: def __init__(self): self.conn = None self.cur = None - self.dbName = '' self.tableNames = [] self.tables = {} self.tablesJson = {} + self.dbName = "" self.app_name = "" self.app_ver = "" self.notes = "" + self.filein = "" + self.scanned = False # self.jsonData = None return # def scanDBFile(self, filein): - # try to open sqlite file + """ read the database, populate the data into the class """ try: (self.conn, self.cur) = self.__openDB(filein) - except Exception, e: - print e + except Exception, ex: + print ex return -2 - # extract file name from path+filename + try: + # extract file name from path+filename self.dbName = toolbox.ToolBox.parseFilenameIncExt(filein) except: self.dbName = filein - # read database schema try: + # read database schema, parse the schema self.__readDatabase() - except Exception, e: - print e + except Exception, ex: + print ex return -3 + # flag is used to determine if the class has data + self.scanned = True + self.filein = filein return 1 # - def writeFingerprint(self, filein): - self.DBSchema() - retVal = self.scanDBFile(filein) - if (retVal > 0): + def writeFingerprint(self): + if (not self.scanned): + return + + try: + fileout = toolbox.ToolBox.getTimestampFilename(self.filein) fh = open(fileout, "w") - self.__writeFingerprint(fh) - fh.close() - else: - print self.getErrorString(retVal) + try: + self.__writeFingerprint(fh) + finally: + fh.close() + except Exception, ex: + print ex + raise FingerprintWrite("Problem writing the fingerprint to a file") + + # + def writeFingerprintFile(self, fileout): + if (not self.scanned): + return + + try: + fh = open(fileout, "w") + try: + self.__writeFingerprint(fh) + finally: + fh.close() + except Exception, ex: + print ex + raise FingerprintWrite("Problem writing the fingerprint to a file") + # import fingerprint from a json file def importJson(self, filejson): + """ import fingerprint from a json file """ self.tablesJson = self.__importJsonDBSchema(filejson) # def compareDB(self, filejson): + if (not self.scanned): + return + """ return the percentage of the match between two fingerprints """ self.tablesJson = self.__importJsonDBSchema(filejson) result = self.__DBSchemaCompare() print "[ Percetage == {}]".format(result) return result - # import fingerprint from a json file + # def __importJsonDBSchema(self, file_json): + """ import fingerprint from a json file """ tables = {} try: fh = open(file_json, "r") diff --git a/libs/fingerprint_comp.py b/libs/fingerprint_comp.py index 07151c9..188bdc7 100644 --- a/libs/fingerprint_comp.py +++ b/libs/fingerprint_comp.py @@ -5,19 +5,19 @@ from libs import fingerprint class FingerprintRunner: - """ - Compare Finger Prints from a directory - """ + """ + Compare Finger Prints from a directory + """ # - def __init__(self): - return + def __init__(self): + return - # - def dirCompare(self, folder): - return + # + def dirCompare(self, folder): + return - def compareFingerprint(self, fp1, fp2): - pass + def compareFingerprint(self, fp1, fp2): + pass