import os,sys,re,shutil
import win32api,win32file,time,threading from ConfigParser import ConfigParser from win32con import *
MARKFILE = '_.m'
#删除目录时没通知 删除文件时通知目录 #安装pywin
http://sourceforge.net/projects/pywin32/files/pywin32/Build%20214/pywin32-214.win32-py2.6.exe/download
class worker(threading.Thread): def __init__(self, config): #print config config['shadowRoot'] = '.sr' self.mkIfNotExit(config['shadowRoot']) self.config = config self.regex = re.compile(config['filter']) self.synList(self.config['from']) threading.Thread.__init__(self, name = \"thread1\") def mkIfNotExit(self,path,isfile=0): if (not os.path.exists(path)): if (not isfile): os.mkdir(path) else: #print path
try:
shutil.copyfile(MARKFILE ,path) except:
pass #shutil.copy('_.m',path) return 1 else: return 0 def updateMtime(self,df,mtime): os.utime(df, (mtime,mtime))
def synFolder(self,path): if(self.config['protocol'] == 'FILE'): if (not os.path.exists(path)): print 'mkdir:',path os.mkdir(path) def synFile(self,source,to): if(self.config['protocol'] == 'FILE'): print 'upload:',to #目录不存在 创建 shutil.copyfile(source ,to) def uploadFile(self,source,relatpath): if (self.regex.search(relatpath)): return 1 to = os.path.join(self.config['to'],relatpath) if (os.path.isdir(source)): self.synFolder(to) else: self.synFile(source,to) #print 'source:',source,'to:',to def checkStat(self,path): '''检测''' relatpath = self.getRelatePath(path) if (self.regex.search(relatpath)): return 1 data = os.path.join(self.config['shadowRoot'],relatpath self.config['sec'].decode('utf-8')) if (len(relatpath) < 1): self.config['shadowRoot'] = data result = 0 if (self.mkIfNotExit(data,os.path.isfile(path))):
or
'''新创建 同步''' self.uploadFile(path,relatpath) elif (os.path.getmtime(path)> os.path.getmtime(data)): '''修改过 同步 修改记录时间''' self.uploadFile(path,relatpath) self.updateMtime(data,os.path.getmtime(path)) else: result = 1 #print relatpath,os.path.getmtime(path),data,result #print relatpath,os.path.getmtime(path),data return result
def synList(self,dirName):
'''对比目录''' #print dirName try: if (self.checkStat(dirName)): return 1 else: ls = os.listdir(dirName) for l in ls: temp=os.path.join(dirName,l) if(os.path.isdir(temp)): self.synList(temp) else: self.checkStat(temp) except: print u'错误:',dirName
def getRelatePath(self,path): basePath = self.config['from'] relatpath = path[len(basePath)+1::] return relatpath
def run(self): dirName = self.config['from'] buffer = int(self.config['buffer']) hdir = win32file.CreateFile( dirName, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, None, OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS, 0 ) #print hdir while(1): time.sleep(.5) results = win32file.ReadDirectoryChangesW ( hdir, buffer, 100, FILE_NOTIFY_CHANGE_LAST_WRITE, None, None ) for action, file in results: full_filename = os.path.join (dirName, file) relatpath = self.getRelatePath(full_filename) print relatpath,'changed' self.uploadFile(full_filename,relatpath) #TODO syn delete operation #to = os.path.join(self.config['to'],relatpath) if(os.path.isdir(full_filename)): pass else: pass
def watch_dir(): cf = ConfigParser() cf.read(\"syn.conf\") watch_workers = [] for sec in cf.sections(): print 'section:',sec.decode('utf-8') config = {} config['sec'] = sec for opt in cf.options(sec):
config[opt] = cf.get(sec, opt).strip() watch_workers.append(worker(config)) for work in watch_workers: print work work.start()
if __name__==\"__main__\": watch_dir() #config = {} #config['from'] = 'e:\\\\as\\hello' #watch_worker = worker(config) #watch_worker.start()
说明:将以上文件保存为syn.py
以下是配置文件(syn.conf): [section]
from = e:\\\\ path to = Z:\\\\path protocol = FILE buffer = 1000
filter=svn|cvs|CVS|configure|metadata|\\.buildpath|\\.settings|\\.classpath|\\.cvsignore|\\.mymetadata|\\.myumldata|\\.project|.tomcatplugin|font|smarty_cache|syslogs|\\.swp|doc
说明:
1. 第一行设置一个段section,下面几行是关于这个段的配置信息,from从本地目录同步
到to服务器目录,filter是过滤,你可以过滤任何你不想同步到服务器的文件或文件夹,过滤器已|分割。
2. 一个.conf配置文件可以设置多个段。段名section不要重名。
3. 将syn.py和syn.conf保存在同级目录中,在编辑本地文件之前先运行syn.py,当保存本
地文件时,会自动同步服务器文件。
4. 运行环境说明:本地需安装有python和pywin,pywin的版本要对应python的版本。
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- huatuowenda.com 版权所有 湘ICP备2023022495号-1
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务