-
Notifications
You must be signed in to change notification settings - Fork 0
/
md5match.py
executable file
·222 lines (173 loc) · 6.16 KB
/
md5match.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/python
import argparse
import collections
import itertools
import string
import sys
def readFileIntoLines( filename ):
f = open( filename )
lines = f.readlines()
f.close()
return lines
def isMD5Hash( token ):
token = token.lower()
if( len( token ) != 32 ):
return False
for s in token:
if( not( s.isdigit() ) ):
if( not( s.isalpha() ) ):
return False
if( s.isalpha() ):
if( not( s == 'a' or \
s == 'b' or \
s == 'c' or \
s == 'd' or \
s == 'e' or \
s == 'f' ) ):
return False
return True
# given a list of lines
# each of the form:
# <md5hash> <filename>
# (delimited by whitespace)
# returns a dictionary mapping
# MD5 hashes to lists of filenames
def linesToDict( lines ):
hashDict = {}
for line in lines:
line = line.strip()
tokens = line.split()
if( len( tokens ) > 1 ):
hash = tokens[ 0 ]
if( isMD5Hash( hash ) ):
# get the rest of the line, starting from the second token
filename = line[ line.find( tokens[ 1 ] ) : ]
if( hash not in hashDict ):
hashDict[ hash ] = []
hashDict[ hash ].append( filename )
return hashDict
Entry = collections.namedtuple( 'Entry', ['hash', 'left', 'right' ] )
def entryKey( entry ):
if( entry.left == [] ):
return entry.right[0]
elif( entry.right == [] ):
return entry.left[0]
else:
return min( entry.left[0], entry.right[0] )
def makeMatchSets( leftDict, rightDict ):
# create 3 lists:
# matches, in-left-not-in-right, and in-right-not-inleft
matches = []
inLeftNotInRight = []
inRightNotInLeft = []
# walk over all hashes in the left dictionary
# if it exists in the right dictionary, make an entry in matches
# otherwise, add it to in-left-not-in-right
for hash in leftDict:
if hash in rightDict:
matches.append( Entry( hash, leftDict[ hash ], rightDict[ hash ] ) )
else:
inLeftNotInRight.append( Entry( hash, leftDict[ hash ], [] ) )
# walk over all the hashes in the right dictionary
# test if it's not in the left dictionary and add it to the in-right-not-in-left
# dictionary
for hash in rightDict:
if not( hash in leftDict ):
inRightNotInLeft.append( Entry( hash, [], rightDict[ hash ] ) )
return ( matches, inLeftNotInRight, inRightNotInLeft )
def formatEntries( entries ):
if not entries:
return '', 0
# find the longest left and right filenames
# list of lists:
# [ ['ab', 'cde', 'f'], ['ghij', 'kl'] ]
leftFilenames = [ e.left for e in entries ]
rightFilenames = [ e.right for e in entries ]
# unwrap the list of lists into a single list
leftFilenames = list( itertools.chain( *leftFilenames ) )
rightFilenames = list( itertools.chain( *rightFilenames ) )
longestLeft = 0
if leftFilenames != []:
longestLeft = max( [ len( f ) for f in leftFilenames ] )
longestRight = 0
if rightFilenames != []:
longestRight = max( [ len( f ) for f in rightFilenames ] )
formatted = ''
lineLength = -1
i = 0
for e in entries:
h = e.hash
spaces = ' ' * len( h )
leftList = e.left
nLeft = len( leftList )
rightList = e.right
nRight = len( rightList )
isFirst = True
for k in range( max( nLeft, nRight ) ):
leftStr = ' '
rightStr = ' '
if( k < nLeft ):
leftStr = leftList[ k ]
if( k < nRight ):
rightStr = rightList[ k ]
if( isFirst ):
line = '%4d | %s | %s | %s\n' % ( i, h, leftStr.ljust( longestLeft ), rightStr.ljust( longestRight ) )
isFirst = False
else:
line = '%4d | %s | %s | %s\n' % ( i, spaces, leftStr.ljust( longestLeft ), rightStr.ljust( longestRight ) )
formatted += line
if( lineLength < 0 ):
lineLength = len( line ) - 1
i += 1
return( formatted, lineLength )
def main( argv = None ):
if argv is None:
argv = sys.argv
parser = argparse.ArgumentParser( description = 'MD5 Matcher' )
parser.add_argument( 'left' )
parser.add_argument( 'right' )
args = parser.parse_args()
# read files into lines
leftLines = readFileIntoLines( args.left )
rightLines = readFileIntoLines( args.right )
# parse both files into separate dictionaries
leftDict = linesToDict( leftLines )
rightDict = linesToDict( rightLines )
# for each hash, sort the list of files that match the hash
for h in leftDict:
leftDict[ h ].sort()
for h in rightDict:
rightDict[ h ].sort()
(matches, lnr, rnl) = makeMatchSets( leftDict, rightDict )
matches.sort( key = entryKey )
lnr.sort( key = entryKey )
rnl.sort( key = entryKey )
( formattedMatches, matchesLength ) = formatEntries( matches )
( formattedLNR, lnrLength ) = formatEntries( lnr )
( formattedRNL, rnlLength ) = formatEntries( rnl )
title = 'Matches'
if( matchesLength < len( title ) ):
matchesLength = len( title )
print( '=' * matchesLength )
print( 'Matches'.center( matchesLength ) )
print( '=' * matchesLength )
print( formattedMatches )
title = 'In %s, not in %s' % ( args.left, args.right )
if( lnrLength < len( title ) ):
lnrLength = len( title )
print( '=' * lnrLength )
print( title )
print( '=' * lnrLength )
print( formattedLNR )
title = 'In %s, not in %s' % ( args.right, args.left )
if( rnlLength < len( title ) ):
rnlLength = len( title )
print( '=' * rnlLength )
print( title )
print( '=' * rnlLength )
print( formattedRNL )
# TODO: print summary
# TODO: display duplicates
# print( '%d matches, %d only in left, %d only in right' )
if __name__ == "__main__":
sys.exit( main() )