FIN: import JSON schema
This commit is contained in:
parent
0eeb77862e
commit
80d9fbacd1
|
@ -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
|
||||
|
||||
|
||||
|
@ -35,6 +37,7 @@ class DBSchema:
|
|||
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,7 +97,7 @@ class DBSchema:
|
|||
for key in keys:
|
||||
print "[[ TABLE: <" + key + "> ]]"
|
||||
tableDef = self.tables[key]
|
||||
print str(tableDef.SQLstr())
|
||||
#print str(tableDef.SQLstr())
|
||||
tableDef.toJSON()
|
||||
|
||||
|
||||
|
@ -81,18 +105,26 @@ class DBSchema:
|
|||
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['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
|
||||
|
||||
|
|
24
main.py
24
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__":
|
||||
|
|
Loading…
Reference in New Issue