Advertisement
Guest User

Untitled

a guest
Mar 7th, 2014
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.04 KB | None | 0 0
  1.  
  2.  
  3. #main data structure
  4.    # {directory:{file:[modified_date, md5hash]}}
  5.  
  6.  
  7. import os
  8. import hashlib
  9. import shutil
  10.  
  11. class snapshot():
  12.     def __init__(self):
  13.         self.directories = {}
  14. #add the list to the files dict with the key directories
  15. #iterate through folders and subfolders to build a collection of all files    
  16.     def add_dir(self, directory):
  17.         self.directories[directory] = {}
  18.         for path, subdirs,  files in os.walk(directory):
  19.             for filename in files:
  20.                 s = open(os.path.join(path,filename), 'rb',)
  21.                 contents = s.read()
  22.                 s.close()
  23.                 #s = s.encode('utf_8')
  24.                 self.directories[directory][os.path.join(path,filename)] = [os.path.getmtime(os.path.join(path,filename)), hashlib.md5(contents)]
  25.        
  26.        
  27.      
  28. #returns two dicts, one of files to copy from self to othersnap, another with files
  29. #that need to be copied from othersnap to the self directory
  30. #returns {file:[prefix, mtime, md5]
  31.     def comp(self, othersnap):
  32.         fromself = {}
  33.         toself = {}
  34.         set1 = {}
  35.         for i in self.directories.keys():
  36.             currentset = self.directories[i]
  37.             prefix = i
  38.             for file in currentset.keys():
  39.                 set1[file[len(prefix):]] = [prefix] + currentset[file] #strip the directory prefix
  40.         set2 = {}    
  41.         for j in othersnap.directories.keys():
  42.             currentset = othersnap.directories[j]
  43.             prefix = j
  44.             for file in currentset.keys():
  45.                 set2[file[len(prefix):]] = [prefix] + currentset[file]
  46.                
  47.         for file in set1.keys():
  48.             if file in set2:
  49.                 if set2[file][2] != set1[file][2]:
  50.                     #md5 sums do not match!
  51.                     if set1[file][1] > set2[file][1]:  #compare mtimes
  52.                         fromself[file] = set1[file]        
  53.                     elif set1[file][1] < set2[file][1]:
  54.                         toself[file] =  set2[file]
  55.                     else:
  56.                         pass
  57.                         #code something to freak out about the files having the same mtime
  58.                         #but have different md5sums
  59.             else:
  60.                 #file doesn't exist in othersnap
  61.                 fromself[file] = set1[file]
  62.                
  63.             #At this point we have checked for overlap in set1 and set2, and everything in set 1 that isn't in set 2 will be returned
  64.         for file in set2.keys():
  65.             if file in set1:
  66.                 pass
  67.             else:
  68.                 toself[file] = set2[file]
  69.         return fromself, toself
  70.      
  71.      
  72. def sync(snap1, snap2):
  73.     fromsnap1, tosnap1 = snap1.comp(snap2)    
  74.     for file in fromsnap1.keys():
  75.         targetdir = list(snap2.directories.keys())[0]
  76.         #check if its a sub directory
  77.         path = make_sub_dirs(file, targetdir)
  78.         if path!='':
  79.             shutil.copy2(fromsnap1[file][0]+file, path)
  80.         else:
  81.             shutil.copy2(fromsnap1[file][0]+file, targetdir)
  82.    
  83.     for file in tosnap1.keys():
  84.         targetdir = list(snap1.directories.keys())[0]
  85.         #shutil.copy2(tosnap1[file][0]+file, targetdir)
  86.         #cp tosnap1[file][0] + file to targetdir(snap1)
  87.         path = make_sub_dirs(file, targetdir)
  88.         if path != '':
  89.             shutil.copy2(tosnap1[file][0]+file, path)
  90.         else:
  91.             shutil.copy2(tosnap1[file][0]+file, targetdir)
  92.                  
  93. #checks if all directories in path exist, if not create them and return path
  94. def make_sub_dirs(file, targetdir):
  95.     dirs = file.split(os.path.sep)[1:-1]
  96.     path = ''
  97.     if len(dirs) > 0:
  98.         path = targetdir   #was a sub directory. Iteratively create all sub directories
  99.         for direct in dirs:
  100.             if os.path.isdir(path + os.path.sep + direct):
  101.                 path = path + os.path.sep + direct
  102.             else:
  103.                 os.mkdir(path + os.path.sep + direct)
  104.                 path = path + os.path.sep + direct
  105.     return path
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement