-
Notifications
You must be signed in to change notification settings - Fork 0
/
Compare_FileMaker_Databases.py
155 lines (154 loc) · 5.73 KB
/
Compare_FileMaker_Databases.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#001
# - added support for Mongo record fields in filemaker
# -
# -
from fmkr import FM
from pymongo import Connection, DESCENDING, ASCENDING
import pprint
import sys
from datetime import *
class FMDB_Compare():
def __init__(self, username, password, host="127.0.0.1", port=80, protocol="http" ):
self.f = FM(host, port, protocol)
self.f.set_db_password(username, password)
def to_unicode(self, bad_string):
if(bad_string == None):
return None
if(self.isstr( bad_string)):
strcleaned = ''.join([x for x in bad_string if ord(x) < 128])
return strcleaned.strip().encode('utf-8')
elif(self.isunicode( bad_string)):
strcleaned = ''.join([x for x in bad_string if ord(x) < 128])
return strcleaned.strip().encode('utf-8')
elif(self.islist( bad_string)):
listcount = 0
newstring = ""
for item in bad_string:
newstring = newstring + self.to_unicode(item)
listcount = listcount + 1
newstring2 = self.to_unicode( newstring )
if newstring2 == None or newstring2 == "":
return None
return newstring2
elif( self.isint( bad_string ) ):
bad_string = str(bad_string)
return self.to_unicode( bad_string )
else:
print "bad_string is type: "
print type(bad_string)
def SearchAndCompareAll(self, db1name, db1layout, db2name, db2layout, ignoreList=[], maxrecords=500):
print "searching db1:"
db1 = self.FindAllRecords(db1name, db1layout, maxrecords)
print "searching db2:"
db2 = self.FindAllRecords(db2name, db2layout, maxrecords)
self.CompareDatabases(db2, db1, ignoreList)
#If Record Id's don't match, echo & exit!
def CompareRecordIDs(self, rid1, rid2, autoexit=True):
if rid1 != rid2:
print ""
print "----------------------------------------------"
print ""
print "Record ID's DONT MATCH!"
print ""
print rid1 + " - " + rid2
print ""
print "----------------------------------------------"
print ""
if(autoexit):
exit
def CompareDatabases(self, db1, db2, ignoreList=[], limit=500):
"""
Compares 2 databases that are loaded into memory.
Inputs:
Database1
Database2
List of fields to ignore
Maximum number of records to parse
Returns:
Nothing.
"""
print "Comparing Databases:"
print "Ignoring fields: "
print ignoreList
count = 0
for row in db1:
if count > limit:
break
rowbuffer = ""
isDiff = False
for key, value in row.iteritems():
if self.to_unicode(key) == "RECORDID":
rowRecordID = self.to_unicode(value)
self.CompareRecordIDs(rowRecordID, self.to_unicode(db2[count]["RECORDID"]))
newvalue = self.to_unicode(value)
newvalue2 = self.to_unicode(db2[count][key])
if(self.to_unicode( key ) not in ignoreList):
rowbuffer2 = self.isDif(newvalue, newvalue2, key)
if(rowbuffer2):
if(isDiff == False):
isDiff = True
rowbuffer += rowbuffer2 + "\n"
if(isDiff):
print " "
print " ================================== "
print "RecordID: " + rowRecordID
print " ================================== "
print rowbuffer
print " ================================== "
print " "
count = count + 1
print count
def FindAllRecords(self, database, layout, maxrecords=100):
self.f.set_db_data(database, layout, maxret=maxrecords)
r = self.f.fm_find_all()
return r.resultset
def IterateColumns(self,columns):
for columnn in columns:
yield column
def isDif(self, fielda, fieldb, key):
if(fielda is not None):
if(fieldb is not None):
if(fielda == fieldb):
return False
else:
return "Value Difference - " + key + ": " + fielda + " <> " + fieldb
else:
return "Value Difference - " + key + ": " + fielda + " <> None"
else:
if(fielda is None and fieldb is None):
return False
else:
return "Value Difference - " + key + ": None <> " + fieldb
def to_fm_datetime(self, dt):
if dt != None:
return datetime.strftime(dt, '%m/%d/%Y %H:%M:%S')
else:
return None
def isint(self, stri):
if type(stri) == type(int()):
return True
else:
return False
def isstr(self, stri):
if type(stri) == type(str()):
return True
else:
return False
def ischr(self, chr):
if type(chr) == type(chr()):
return True;
else:
return False;
def islist(self, lis):
if type(lis) == type(list()):
return True
else:
return False
def isunicode(self, uni):
if type(uni) == type(unicode()):
return True
else:
return False
if __name__ == '__main__':
a = FMDB_Compare("username", "password", "127.0.0.1", 80, "http")
a.SearchAndCompareAll("Database1", "LayoutOnDatabase1", "Database2", "LayoutOnDatabase2")