diff --git a/libs/fingerprint.py b/libs/fingerprint.py index 04f64e5..489af1f 100644 --- a/libs/fingerprint.py +++ b/libs/fingerprint.py @@ -21,9 +21,11 @@ class DBSchema: def __init__(self): self.conn = None self.cur = None + self.dbName = '' self.tableNames = [] self.tables = {} - self.jsonData = None + self.tablesJson = {} + # self.jsonData = None return @@ -34,7 +36,8 @@ class DBSchema: except Exception, e: print e return -2 - + + self.dbName = filein # read database schema try: self.__readDatabase() @@ -45,13 +48,34 @@ class DBSchema: return 1 - def importDBSchema(self, filein): + def importJson(self, filein): + self.tables = __importDBSchema(filein) + + + def compareDBSchema(self, filein): + self.tablesJson = __importDBSchema(filein) + result = __DBSchemaCompare() + return result + + + def __importDBSchema(self, filein) + tables = {} try: - fh = open(fileout, "r") - self.jsonData = json.loads(fh) + fh = open(filein, "r") + jsonData = json.load(fh) + dbmt = jsonData['db-metadata'] + tb = jsonData['tables'] + keys = tb.keys() + for key in keys: + newTable = TableDefinition() + newTable.importTable(key, dbmt[key], tb[key]) + tables[key] = newTable + except Exception, e: print e + return tables + def __openDB(self, filein): conn = sqlite3.connect(filein) @@ -73,26 +97,34 @@ class DBSchema: for key in keys: print "[[ TABLE: <" + key + "> ]]" tableDef = self.tables[key] - print str(tableDef.SQLstr()) + #print str(tableDef.SQLstr()) tableDef.toJSON() def writeFingerprint(self, filehandle): - ahash = {} - thash = {} - mhash = {} + ahash = {} + thash = {} + mhash = {} + dhash = {} + dmhash = {} ahash['tables'] = thash - ahash['metadata'] = mhash + ahash['file-metadata'] = mhash + ahash['db-config'] = dhash + ahash['db-metadata'] = dmhash # metadata - mhash['app-name'] = 'dbfp' - mhash['app-ver'] = '0.50' - mhash['format-ver'] = '0.90' + mhash['scanner-name'] = 'dbfp' + mhash['scanner-ver'] = '0.50' + mhash['format-ver'] = '0.90' + + # database configuration information + dhash['dn-name'] = self.dbName # tables keys = self.tables.keys() for key in keys: thash[key] = self.tables[key].fields + dmhash[key] = self.tables[key].SQLstr() json.dump(ahash, filehandle, sort_keys=True, indent=4) @@ -138,6 +170,13 @@ class TableDefinition: if newField: self.fields[newField['name']] = newField + + def importTable(self, tbName, sqlStr, fields): + self.tableName = tbName + self.sqlStr = sqlStr + self.fields = fields + + # Table Definition # # CREATE TABLE contacts (_id INTEGER PRIMARY KEY AUTOINCREMENT,name_raw_contact_id INTEGER REFERENCES raw_contacts(_id), @@ -275,6 +314,10 @@ class TableDefinition: return self.tableName + def setSQLstr(self, str): + return self.sqlStr + + def SQLstr(self): return self.sqlStr diff --git a/main.py b/main.py index b314896..d5d0634 100644 --- a/main.py +++ b/main.py @@ -8,13 +8,13 @@ from libs import fingerprint def main(): - (filein, fileout, filejson, verbose) = parseArgs() + (filein, fileout2, filejson, verbose) = parseArgs() db = fingerprint.DBSchema() - if (fileout): + if (filein): retVal = db.scanDBFile(filein) if (retVal > 0): - fh = open(fileout, "w") + fh = open(fileout2, "w") db.debugFingerprint() db.writeFingerprint(fh) fh.close() @@ -26,17 +26,25 @@ def main(): def parseArgs(): - verbose = False + print '***** ***** ***** *****' + print ' DB Fingerprint' + print '***** ***** ***** *****\n' + fileout1 = '' 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('-j', '--json', required=False) parser.add_argument('-v', '--verbose', action='store_true') args = parser.parse_args() - if (args.verbose): - verbose = args.verbose - fileout = args.file + "_" + timestr + '.json' - return (args.file, fileout, args.json, verbose) + + if (args.file is None) and (args.json is None): + parser.print_help() + exit() + + if (args.file): + fileout1 = args.file + "_" + timestr + '.json' + + return (args.file, fileout1, args.json, args.verbose) if __name__ == "__main__":