This repository has been archived by the owner on Jul 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileComparator.py
executable file
·65 lines (54 loc) · 1.9 KB
/
fileComparator.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
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
# Given two files, open them, read in the lines and put them in respective
# maps, and then compare and output lines with differences
# TODO: if line is longer than some threshold, break it up into chunks
def comparator(fp1, fp2, pfxLen):
# read in fp1 into in-memory hash
lineCount = 1
dictLineNum = {}
dictLineVal = {}
for line in fp1:
key = line[:pfxLen]
dictLineNum[key] = lineCount
if key in dictLineVal:
print "WARNING: collision at ", lineCount, ": replacing [", dictLineVal[key], "] with [", line, "]"
dictLineVal[key] = line
lineCount = lineCount + 1
# read in fp2 and look
lineCount = 1
for line in fp2:
key = line[:pfxLen]
line1 = dictLineVal[key]
line1Num = dictLineNum[key]
if line != line1:
print "ORG: [", line1, "] at [",line1Num, "]\n"
print "NEW: [", line, "] at [",lineCount, "]\n"
lineCount = lineCount + 1
# check for input args
if len(sys.argv) != 4:
print "usage: ", sys.argv[0], " <prefix size> <file1> <file2>"
exit(-1)
#print sys.argv
fp1 = None
fp2 = None
try:
fp1 = open(sys.argv[2], 'r')
fp2 = open(sys.argv[3], 'r')
pfx = int(sys.argv[1])
comparator(fp1, fp2, pfx)
except Exception as e:
print "Exception ", e
exit(-1)
else:
fp1.close()
fp2.close()