FIN: import JSON schema

This commit is contained in:
JohnE 2015-06-19 17:12:09 -07:00
parent 0eeb77862e
commit 80d9fbacd1
2 changed files with 72 additions and 21 deletions

View File

@ -21,9 +21,11 @@ class DBSchema:
def __init__(self): def __init__(self):
self.conn = None self.conn = None
self.cur = None self.cur = None
self.dbName = ''
self.tableNames = [] self.tableNames = []
self.tables = {} self.tables = {}
self.jsonData = None self.tablesJson = {}
# self.jsonData = None
return return
@ -34,7 +36,8 @@ class DBSchema:
except Exception, e: except Exception, e:
print e print e
return -2 return -2
self.dbName = filein
# read database schema # read database schema
try: try:
self.__readDatabase() self.__readDatabase()
@ -45,13 +48,34 @@ class DBSchema:
return 1 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: try:
fh = open(fileout, "r") fh = open(filein, "r")
self.jsonData = json.loads(fh) 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: except Exception, e:
print e print e
return tables
def __openDB(self, filein): def __openDB(self, filein):
conn = sqlite3.connect(filein) conn = sqlite3.connect(filein)
@ -73,26 +97,34 @@ class DBSchema:
for key in keys: for key in keys:
print "[[ TABLE: <" + key + "> ]]" print "[[ TABLE: <" + key + "> ]]"
tableDef = self.tables[key] tableDef = self.tables[key]
print str(tableDef.SQLstr()) #print str(tableDef.SQLstr())
tableDef.toJSON() tableDef.toJSON()
def writeFingerprint(self, filehandle): def writeFingerprint(self, filehandle):
ahash = {} ahash = {}
thash = {} thash = {}
mhash = {} mhash = {}
dhash = {}
dmhash = {}
ahash['tables'] = thash ahash['tables'] = thash
ahash['metadata'] = mhash ahash['file-metadata'] = mhash
ahash['db-config'] = dhash
ahash['db-metadata'] = dmhash
# metadata # metadata
mhash['app-name'] = 'dbfp' mhash['scanner-name'] = 'dbfp'
mhash['app-ver'] = '0.50' mhash['scanner-ver'] = '0.50'
mhash['format-ver'] = '0.90' mhash['format-ver'] = '0.90'
# database configuration information
dhash['dn-name'] = self.dbName
# tables # tables
keys = self.tables.keys() keys = self.tables.keys()
for key in keys: for key in keys:
thash[key] = self.tables[key].fields thash[key] = self.tables[key].fields
dmhash[key] = self.tables[key].SQLstr()
json.dump(ahash, filehandle, sort_keys=True, indent=4) json.dump(ahash, filehandle, sort_keys=True, indent=4)
@ -138,6 +170,13 @@ class TableDefinition:
if newField: if newField:
self.fields[newField['name']] = newField self.fields[newField['name']] = newField
def importTable(self, tbName, sqlStr, fields):
self.tableName = tbName
self.sqlStr = sqlStr
self.fields = fields
# Table Definition # Table Definition
# #
# CREATE TABLE contacts (_id INTEGER PRIMARY KEY AUTOINCREMENT,name_raw_contact_id INTEGER REFERENCES raw_contacts(_id), # 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 return self.tableName
def setSQLstr(self, str):
return self.sqlStr
def SQLstr(self): def SQLstr(self):
return self.sqlStr return self.sqlStr

24
main.py
View File

@ -8,13 +8,13 @@ from libs import fingerprint
def main(): def main():
(filein, fileout, filejson, verbose) = parseArgs() (filein, fileout2, filejson, verbose) = parseArgs()
db = fingerprint.DBSchema() db = fingerprint.DBSchema()
if (fileout): if (filein):
retVal = db.scanDBFile(filein) retVal = db.scanDBFile(filein)
if (retVal > 0): if (retVal > 0):
fh = open(fileout, "w") fh = open(fileout2, "w")
db.debugFingerprint() db.debugFingerprint()
db.writeFingerprint(fh) db.writeFingerprint(fh)
fh.close() fh.close()
@ -26,17 +26,25 @@ def main():
def parseArgs(): def parseArgs():
verbose = False print '***** ***** ***** *****'
print ' DB Fingerprint'
print '***** ***** ***** *****\n'
fileout1 = ''
timestr = time.strftime('%Y-%m-%d_%H%M%S', time.localtime(time.time())) 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 = argparse.ArgumentParser(description='Fingerprint a sqlite database based on its schema')
parser.add_argument('-f', '--file', required=False) parser.add_argument('-f', '--file', required=False)
parser.add_argument('-j', '--json', required=False) parser.add_argument('-j', '--json', required=False)
parser.add_argument('-v', '--verbose', action='store_true') parser.add_argument('-v', '--verbose', action='store_true')
args = parser.parse_args() args = parser.parse_args()
if (args.verbose):
verbose = args.verbose if (args.file is None) and (args.json is None):
fileout = args.file + "_" + timestr + '.json' parser.print_help()
return (args.file, fileout, args.json, verbose) exit()
if (args.file):
fileout1 = args.file + "_" + timestr + '.json'
return (args.file, fileout1, args.json, args.verbose)
if __name__ == "__main__": if __name__ == "__main__":