python自动同步脚本.doc

上传人:M****1 文档编号:558807865 上传时间:2023-09-27 格式:DOC 页数:5 大小:41.50KB
返回 下载 相关 举报
python自动同步脚本.doc_第1页
第1页 / 共5页
python自动同步脚本.doc_第2页
第2页 / 共5页
python自动同步脚本.doc_第3页
第3页 / 共5页
python自动同步脚本.doc_第4页
第4页 / 共5页
python自动同步脚本.doc_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《python自动同步脚本.doc》由会员分享,可在线阅读,更多相关《python自动同步脚本.doc(5页珍藏版)》请在金锄头文库上搜索。

1、#!/usr/bin/env python #coding:utf-8 import os,sys,re,shutilimport win32api,win32file,time,threadingfrom ConfigParser import ConfigParserfrom win32con import *MARKFILE = _.m#删除目录时没通知 删除文件时通知目录#安装pywinhttp:/ worker(threading.Thread):def _init_(self, config):#print configconfigshadowRoot = .srself.mkIf

2、NotExit(configshadowRoot)self.config = configself.regex = pile(configfilter)self.synList(self.configfrom)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) e

3、xcept: pass#shutil.copy(_.m,path)return 1else:return 0def updateMtime(self,df,mtime):os.utime(df, (mtime,mtime)def synFolder(self,path):if(self.configprotocol = FILE):if (not os.path.exists(path):print mkdir:,pathos.mkdir(path)def synFile(self,source,to):if(self.configprotocol = FILE):print upload:,

4、to#目录不存在 创建shutil.copyfile(source ,to)def uploadFile(self,source,relatpath):if (self.regex.search(relatpath):return 1to = os.path.join(self.configto,relatpath)if (os.path.isdir(source):self.synFolder(to)else:self.synFile(source,to)#print source:,source,to:,todef checkStat(self,path):检测relatpath = se

5、lf.getRelatePath(path)if (self.regex.search(relatpath):return 1data = os.path.join(self.configshadowRoot,relatpath or self.configsec.decode(utf-8)if (len(relatpath) os.path.getmtime(data):修改过 同步 修改记录时间self.uploadFile(path,relatpath)self.updateMtime(data,os.path.getmtime(path)else:result = 1#print re

6、latpath,os.path.getmtime(path),data,result#print relatpath,os.path.getmtime(path),datareturn resultdef synList(self,dirName):对比目录#print dirNametry:if (self.checkStat(dirName):return 1else:ls = os.listdir(dirName)for l in ls:temp=os.path.join(dirName,l) if(os.path.isdir(temp):self.synList(temp)else:s

7、elf.checkStat(temp)except:print u错误:,dirNamedef getRelatePath(self,path):basePath = self.configfromrelatpath = pathlen(basePath)+1:return relatpathdef run(self):dirName = self.configfrombuffer = int(self.configbuffer)hdir = win32file.CreateFile(dirName,GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE|F

8、ILE_SHARE_DELETE,None,OPEN_EXISTING,FILE_FLAG_BACKUP_SEMANTICS,0)#print hdirwhile(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.getRe

9、latePath(full_filename)print relatpath,changedself.uploadFile(full_filename,relatpath)#TODO syn delete operation#to = os.path.join(self.configto,relatpath)if(os.path.isdir(full_filename):passelse:passdef watch_dir():cf = ConfigParser() cf.read(syn.conf) watch_workers = for sec in cf.sections():print

10、 section:,sec.decode(utf-8)config = configsec = secfor opt in cf.options(sec): configopt = cf.get(sec, opt).strip()watch_workers.append(worker(config)for work in watch_workers:print workwork.start()if _name_=_main_:watch_dir()#config = #configfrom = e:ashello#watch_worker = worker(config)#watch_work

11、er.start()说明:将以上文件保存为syn.py以下是配置文件(syn.conf):sectionfrom = e: pathto = Z:pathprotocol = FILEbuffer = 1000filter=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的版本。

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 社会民生

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号