Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Created on Mar 18, 2013
- @author: pipsqueaker
- '''
- import os, shutil, time
- from datetime import datetime
- class mainClass():
- def __init__(self):
- self.srcDir = []
- self.dst = []
- self.iteration = 1
- self.fileHash = {}
- def initializeDirs(self):
- #Initialize Paths from the setup files
- if os.path.exists("srcDir") == False:
- print("The srcDir file does not exist; Creating Now...")
- self.srcDir = input("Please input source directory \n")
- self.newSource = open("srcDir", "w")
- self.newSource.write(self.srcDir)
- self.newSource.close()
- if os.path.exists("dstDirs") == False:
- print("The dstFirs file does not exits; Creating Now...")
- print("Input a directory to sync to. Or just type xit to exit")
- self.newDst = open("dstDirs", "w")
- while True:
- self.IN = input()
- if os.name == 'nt': #Windows
- self.IN.replace("/", "\\")
- else:
- self.IN.replace("\\", "/")
- if self.IN != "xit":
- self.newDst.write(self.IN)
- self.dst.append(self.IN)
- else:
- self.newDst.close()
- break
- self.srcDir = open("srcDir", "r")
- self.srcDir = self.srcDir.readline()
- self.dstDirs = open("dstDirs", "r")
- for line in self.dstDirs:
- self.dst.append(line)
- def fileHashes(self):
- self.fileHash = {}
- for file in os.listdir(self.srcDir):
- self.fileHash[file] = os.path.getmtime(self.srcDir+file)
- def loopForever(self):
- print("Filesync Version 1.0 by pipsqueaker \n")
- while True:
- print("Iteration ", self.iteration, " @ ", datetime.now()) #APPROVE
- for destination in self.dst:
- for checkFile in os.listdir(destination):
- if not os.path.exists(self.srcDir+checkFile):
- os.remove(destination+checkFile)
- print(checkFile, " removed from ", destination, " @", datetime.now())
- self.fileHashes()
- for file in os.listdir(self.srcDir):
- if os.path.exists(destination+file):
- try:
- if os.path.getmtime(self.srcDir+file) != self.fileHash[file]:
- try:
- shutil.copy2((self.srcDir+file), destination)
- print(file," was updated to ",destination," @",datetime.now())
- except:
- pass
- except KeyError:
- continue
- else:
- try:
- shutil.copy2((self.srcDir+file), destination)
- print(file, " was copied to ", destination, " @", datetime.now())
- self.fileHashes()
- except:
- pass
- self.iteration += 1
- time.sleep(10)
- def main(self):
- self.initializeDirs()
- self.fileHashes()
- self.loopForever()
- n = mainClass()
- n.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement