|
| 1 | +#! /usr/bin/env python |
| 2 | + |
| 3 | +import sys |
| 4 | +from zipfile import ZipFile |
| 5 | + |
| 6 | +class ApkDiff: |
| 7 | + |
| 8 | + IGNORE_FILES = ["META-INF/CERT.RSA", "META-INF/CERT.SF", "META-INF/MANIFEST.MF"] |
| 9 | + |
| 10 | + def compare(self, sourceApk, destinationApk): |
| 11 | + sourceZip = ZipFile(sourceApk, 'r') |
| 12 | + destinationZip = ZipFile(destinationApk, 'r') |
| 13 | + |
| 14 | + if self.compareManifests(sourceZip, destinationZip) and self.compareEntries(sourceZip, destinationZip) == True: |
| 15 | + print "APKs match!" |
| 16 | + else: |
| 17 | + print "APKs don't match!" |
| 18 | + |
| 19 | + def compareManifests(self, sourceZip, destinationZip): |
| 20 | + sourceEntrySortedList = sorted(sourceZip.namelist()) |
| 21 | + destinationEntrySortedList = sorted(destinationZip.namelist()) |
| 22 | + |
| 23 | + for ignoreFile in self.IGNORE_FILES: |
| 24 | + while ignoreFile in sourceEntrySortedList: sourceEntrySortedList.remove(ignoreFile) |
| 25 | + while ignoreFile in destinationEntrySortedList: destinationEntrySortedList.remove(ignoreFile) |
| 26 | + |
| 27 | + if len(sourceEntrySortedList) != len(destinationEntrySortedList): |
| 28 | + print "Manifest lengths differ!" |
| 29 | + |
| 30 | + for (sourceEntryName, destinationEntryName) in zip(sourceEntrySortedList, destinationEntrySortedList): |
| 31 | + if sourceEntryName != destinationEntryName: |
| 32 | + print "Sorted manifests don't match, %s vs %s" % (sourceEntryName, destinationEntryName) |
| 33 | + return False |
| 34 | + |
| 35 | + return True |
| 36 | + |
| 37 | + def compareEntries(self, sourceZip, destinationZip): |
| 38 | + sourceInfoList = filter(lambda sourceInfo: sourceInfo.filename not in self.IGNORE_FILES, sourceZip.infolist()) |
| 39 | + destinationInfoList = filter(lambda destinationInfo: destinationInfo.filename not in self.IGNORE_FILES, destinationZip.infolist()) |
| 40 | + |
| 41 | + if len(sourceInfoList) != len(destinationInfoList): |
| 42 | + print "APK info lists of different length!" |
| 43 | + return False |
| 44 | + |
| 45 | + for sourceEntryInfo in sourceInfoList: |
| 46 | + for destinationEntryInfo in list(destinationInfoList): |
| 47 | + if sourceEntryInfo.filename == destinationEntryInfo.filename: |
| 48 | + sourceEntry = sourceZip.open(sourceEntryInfo, 'r') |
| 49 | + destinationEntry = destinationZip.open(destinationEntryInfo, 'r') |
| 50 | + |
| 51 | + if self.compareFiles(sourceEntry, destinationEntry) != True: |
| 52 | + print "APK entry %s does not match %s!" % (sourceEntryInfo.filename, destinationEntryInfo.filename) |
| 53 | + return False |
| 54 | + |
| 55 | + destinationInfoList.remove(destinationEntryInfo) |
| 56 | + break |
| 57 | + |
| 58 | + return True |
| 59 | + |
| 60 | + def compareFiles(self, sourceFile, destinationFile): |
| 61 | + sourceChunk = sourceFile.read(1024) |
| 62 | + destinationChunk = destinationFile.read(1024) |
| 63 | + |
| 64 | + while sourceChunk != "" and destinationChunk != "": |
| 65 | + if sourceChunk != destinationChunk: |
| 66 | + return False |
| 67 | + |
| 68 | + sourceChunk = sourceFile.read(1024) |
| 69 | + destinationChunk = destinationFile.read(1024) |
| 70 | + |
| 71 | + return True |
| 72 | + |
| 73 | +if __name__ == '__main__': |
| 74 | + if len(sys.argv) != 3: |
| 75 | + print "Usage: apkdiff <pathToFirstApk> <pathToSecondApk>" |
| 76 | + sys.exit(1) |
| 77 | + |
| 78 | + ApkDiff().compare(sys.argv[1], sys.argv[2]) |
0 commit comments