资源描述:
《python自动同步脚本》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、#!/usr/bin/envpython#coding:utf-8importos,sys,re,shutilimportwin32api,win32file,time,threadingfromConfigParserimportConfigParserfromwin32conimport*MARKFILE='_.m'#删除目录时没通知删除文件时通知目录#安装pywinhttp://sourceforge.net/projects/pywin32/files/pywin32/Build%20214/pywin32-214.win32-py2.6.exe/
2、downloadclassworker(threading.Thread):def__init__(self,config):#printconfigconfig['shadowRoot']='.sr'self.mkIfNotExit(config['shadowRoot'])self.config=configself.regex=re.compile(config['filter'])self.synList(self.config['from'])threading.Thread.__init__(self,name="thread1")defmkI
3、fNotExit(self,path,isfile=0):if(notos.path.exists(path)):if(notisfile):os.mkdir(path)else:#printpathtry:shutil.copyfile(MARKFILE,path)except:pass#shutil.copy('_.m',path)return1else:return0defupdateMtime(self,df,mtime):os.utime(df,(mtime,mtime))defsynFolder(self,path):if(self.confi
4、g['protocol']=='FILE'):if(notos.path.exists(path)):print'mkdir:',pathos.mkdir(path)defsynFile(self,source,to):if(self.config['protocol']=='FILE'):print'upload:',to#目录不存在创建shutil.copyfile(source,to)defuploadFile(self,source,relatpath):if(self.regex.search(relatpath)):return1to=os.p
5、ath.join(self.config['to'],relatpath)if(os.path.isdir(source)):self.synFolder(to)else:self.synFile(source,to)#print'source:',source,'to:',todefcheckStat(self,path):'''检测'''relatpath=self.getRelatePath(path)if(self.regex.search(relatpath)):return1data=os.path.join(self.config['shad
6、owRoot'],relatpathorself.config['sec'].decode('utf-8'))if(len(relatpath)<1):self.config['shadowRoot']=dataresult=0if(self.mkIfNotExit(data,os.path.isfile(path))):'''新创建同步'''self.uploadFile(path,relatpath)elif(os.path.getmtime(path)>os.path.getmtime(data)):'''修改过同步修改记录时间'''self.upl
7、oadFile(path,relatpath)self.updateMtime(data,os.path.getmtime(path))else:result=1#printrelatpath,os.path.getmtime(path),data,result#printrelatpath,os.path.getmtime(path),datareturnresultdefsynList(self,dirName):'''对比目录'''#printdirNametry:if(self.checkStat(dirName)):return1else:ls=
8、os.listdir(dirName)forlinls:temp=