| 1 |
ninoborges |
8 |
"""
|
| 2 |
|
|
|
| 3 |
|
|
iTunesDirSync.py
|
| 4 |
|
|
|
| 5 |
|
|
Created by:
|
| 6 |
|
|
Emanuel Borges
|
| 7 |
|
|
03.30.209
|
| 8 |
|
|
|
| 9 |
|
|
|
| 10 |
|
|
"""
|
| 11 |
|
|
|
| 12 |
|
|
import os, NinoGenTools
|
| 13 |
|
|
|
| 14 |
|
|
class DirDictionary:
|
| 15 |
|
|
def __init__ (self, reportType = 'Simple'):
|
| 16 |
|
|
self.reportType = reportType
|
| 17 |
|
|
if reportType == 'Complex':
|
| 18 |
|
|
self.hasher = NinoGenTools.HashFileContents('md5')
|
| 19 |
|
|
|
| 20 |
|
|
def CreateDictionary(self, topLevelDir):
|
| 21 |
|
|
self.matrix = {}
|
| 22 |
|
|
self.hashMatrix = {}
|
| 23 |
|
|
for contents in os.walk(topLevelDir):
|
| 24 |
|
|
(currentDir, dirNames, listOfFiles) = contents
|
| 25 |
|
|
for file in listOfFiles:
|
| 26 |
|
|
if self.reportType == 'Complex':
|
| 27 |
|
|
hashVal = self.hasher.HashFile(os.path.join(currentDir, file))
|
| 28 |
|
|
self.hashMatrix[os.path.join(currentDir, file)] = hashVal
|
| 29 |
|
|
if file in self.matrix.keys():
|
| 30 |
|
|
self.matrix[file].append(os.path.join(currentDir, file))
|
| 31 |
|
|
else:
|
| 32 |
|
|
self.matrix[file] = [os.path.join(currentDir, file)]
|
| 33 |
|
|
def DuplicateReport(self):
|
| 34 |
|
|
dupCount = NinoGenTools.Counter()
|
| 35 |
|
|
|
| 36 |
|
|
for i in self.matrix.keys():
|
| 37 |
|
|
instanceCount = len(self.matrix[i])
|
| 38 |
|
|
## Query these "Dups" against the hash table and remove them if they're not.
|
| 39 |
|
|
|
| 40 |
|
|
if instanceCount > 1:
|
| 41 |
|
|
dupCount.inc()
|
| 42 |
|
|
print "-"*25
|
| 43 |
|
|
print "You have %d versions of %s" %(instanceCount, i)
|
| 44 |
|
|
for dir in self.matrix[i]:
|
| 45 |
|
|
print dir
|
| 46 |
|
|
print "-"*25
|
| 47 |
|
|
print
|
| 48 |
|
|
print "*"*25
|
| 49 |
|
|
print "You have a total of %d duplicates" %dupCount.count
|
| 50 |
|
|
|
| 51 |
|
|
|
| 52 |
|
|
class DirDictionaryComparisonTool:
|
| 53 |
|
|
def __init__(self):
|
| 54 |
|
|
"""Tools for comparing two DirDictionary objects. Dont forget that they're objects
|
| 55 |
|
|
not just dicts"""
|
| 56 |
|
|
pass
|
| 57 |
|
|
def FindDifferences(self,localComputer, otherComputer):
|
| 58 |
|
|
"""(localComputer DirDictionary, otherComputer DirDictionary)
|
| 59 |
|
|
Finds the differences between 2 DirDictionaries"""
|
| 60 |
|
|
localComputerMissingCount = NinoGenTools.Counter()
|
| 61 |
|
|
otherComputerMissingCount = NinoGenTools.Counter()
|
| 62 |
|
|
localMissingFileList =[]
|
| 63 |
|
|
otherMissingFileList =[]
|
| 64 |
|
|
for i in localComputer.matrix.keys():
|
| 65 |
|
|
if i in otherComputer.matrix.keys():
|
| 66 |
|
|
pass
|
| 67 |
|
|
else:
|
| 68 |
|
|
otherMissingFileList.append(i)
|
| 69 |
|
|
for x in otherComputer.matrix.keys():
|
| 70 |
|
|
if x in localComputer.matrix.keys():
|
| 71 |
|
|
pass
|
| 72 |
|
|
else:
|
| 73 |
|
|
localMissingFileList.append(x)
|
| 74 |
|
|
|
| 75 |
|
|
print "*"*25
|
| 76 |
|
|
print "You are missing %d files, that the other computer has."%len(localMissingFileList)
|
| 77 |
|
|
print
|
| 78 |
|
|
raw_input("Press any key to see a list:")
|
| 79 |
|
|
print "-"*25
|
| 80 |
|
|
for fileName in localMissingFileList:
|
| 81 |
|
|
for absFilePath in otherComputer.matrix[fileName]:
|
| 82 |
|
|
print absFilePath
|
| 83 |
|
|
print "-"*25
|
| 84 |
|
|
print "*"*25
|
| 85 |
|
|
print "The other computer is missing %d files, that you have."%len(otherMissingFileList)
|
| 86 |
|
|
print
|
| 87 |
|
|
raw_input("Press any key to see a list:")
|
| 88 |
|
|
print "-"*25
|
| 89 |
|
|
for fileName in otherMissingFileList:
|
| 90 |
|
|
for absFilePath in localComputer.matrix[fileName]:
|
| 91 |
|
|
print absFilePath
|
| 92 |
|
|
print "-"*25
|
| 93 |
|
|
|