/
filediff.py
executable file
·91 lines (69 loc) · 2.33 KB
/
filediff.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
#!/usr/bin/env python
import argparse
import os
import sys
import logging
import schemadiff
import dsn
from warnings import filterwarnings
def read_schema_from_file(file):
fh = open(file, 'r')
schema = fh.read()
schema = string.replace(schema, '%DB_COLLATION_CREATE_TABLE_COMMON%', '')
fh.close()
return schema
if __name__ == '__main__':
FORMAT = "%(asctime)-15s %(funcName)s %(message)s"
logging.basicConfig(format=FORMAT, level=logging.DEBUG)
parser = argparse.ArgumentParser()
parser.add_argument("--validate", help="verify that the changes work",
action="store_true")
parser.add_argument("--dmlfile", help="file to write dml statements")
parser.add_argument("file1", help="input file")
parser.add_argument("file2", help="input file")
args = parser.parse_args()
file1 = args.file1
file2 = args.file2
validate = False
if args.validate:
validate = True
dmlfile = args.dmlfile
if not os.path.exists(file1):
print '%s does not exist' % file1
sys.exit(1)
if not os.path.exists(file2):
print '%s does not exist' % file2
sys.exit(1)
if not os.path.isfile(file1):
print '%s is not a regular file' % file1
sys.exit(1)
if not os.path.isfile(file2):
print '%s is not a regular file' % file2
sys.exit(1)
if os.path.samefile(file1, file2):
print '%s and %s are the same file' % (file1, file2)
sys.exit(1)
path1 = os.path.abspath(file1)
path2 = os.path.abspath(file2)
# # need 2 hash objects in order to use them independently
# # for each path
#
# hash1 = hashlib.md5()
# hash2 = hash1.copy()
#
# hash1.update(path1)
# hash2.update(path2)
db1 = "%s" % (os.path.splitext(file1)[0])
db2 = "%s" % (os.path.splitext(file2)[0])
if db1 == db2:
print 'not dealing with same-name schema files right now'
sys.exit(1)
# finally. let's get to work.
schema1 = read_schema_from_file(file1)
schema2 = read_schema_from_file(file2)
conn = dsn.getConnection()
cursor = conn.cursor()
schemadiff.diff_schemas(cursor, schema1, schema2, db1, db2,
dmlfile=dmlfile, validate=validate)
cursor.close()
conn.close()