Advertisement
pipsqueaker117

My Program

Mar 19th, 2013
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.57 KB | None | 0 0
  1. '''
  2. Created on Mar 18, 2013
  3.  
  4. @author: pipsqueaker
  5. '''
  6. import os, shutil, time
  7. from datetime import datetime
  8.  
  9. class mainClass():
  10.    
  11.     def __init__(self):
  12.         self.srcDir = []
  13.         self.dst = []
  14.         self.iteration = 1
  15.         self.fileHash = {}
  16.    
  17.     def initializeDirs(self):
  18.         #Initialize Paths from the setup files
  19.         if os.path.exists("srcDir") == False:
  20.             print("The srcDir file does not exist; Creating Now...")
  21.             self.srcDir = input("Please input source directory \n")
  22.             self.newSource = open("srcDir", "w")
  23.             self.newSource.write(self.srcDir)
  24.             self.newSource.close()
  25.        
  26.         if os.path.exists("dstDirs") == False:
  27.             print("The dstFirs file does not exits; Creating Now...")
  28.             print("Input a directory to sync to. Or just type xit to exit")
  29.            
  30.             self.newDst = open("dstDirs", "w")
  31.             while True:
  32.                 self.IN = input()
  33.                 if os.name == 'nt': #Windows
  34.                     self.IN.replace("/", "\\")
  35.                 else:
  36.                     self.IN.replace("\\", "/")
  37.                    
  38.                 if self.IN != "xit":
  39.                     self.newDst.write(self.IN)
  40.                     self.dst.append(self.IN)
  41.                 else:
  42.                     self.newDst.close()
  43.                     break
  44.            
  45.         self.srcDir = open("srcDir", "r")
  46.         self.srcDir = self.srcDir.readline()
  47.        
  48.         self.dstDirs = open("dstDirs", "r")
  49.         for line in self.dstDirs:
  50.             self.dst.append(line)
  51.  
  52.     def fileHashes(self):
  53.         self.fileHash = {}  
  54.         for file in os.listdir(self.srcDir):
  55.             self.fileHash[file] = os.path.getmtime(self.srcDir+file)
  56.    
  57.     def loopForever(self):
  58.         print("Filesync Version 1.0 by pipsqueaker \n")
  59.         while True:
  60.             print("Iteration ", self.iteration, " @ ", datetime.now()) #APPROVE
  61.            
  62.             for destination in self.dst:
  63.                 for checkFile in os.listdir(destination):
  64.                     if not os.path.exists(self.srcDir+checkFile):
  65.                         os.remove(destination+checkFile)
  66.                         print(checkFile, " removed from ", destination, " @", datetime.now())
  67.                         self.fileHashes()
  68.                    
  69.                 for file in os.listdir(self.srcDir):
  70.                    
  71.                     if os.path.exists(destination+file):
  72.                         try:
  73.                             if os.path.getmtime(self.srcDir+file) != self.fileHash[file]:
  74.                                     try:
  75.                                         shutil.copy2((self.srcDir+file), destination)
  76.                                         print(file," was updated to ",destination," @",datetime.now())
  77.                                     except:
  78.                                         pass
  79.                            
  80.                         except KeyError:
  81.                             continue
  82.                     else:
  83.                         try:
  84.                             shutil.copy2((self.srcDir+file), destination)
  85.                             print(file, " was copied to ", destination, " @", datetime.now())
  86.                             self.fileHashes()
  87.                         except:
  88.                             pass
  89.             self.iteration += 1
  90.             time.sleep(10)
  91.    
  92.     def main(self):
  93.         self.initializeDirs()
  94.         self.fileHashes()
  95.         self.loopForever()
  96.        
  97. n = mainClass()
  98. n.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement