Python写自动化之SVN更新
来源:互联网 发布:js调用as3方法 编辑:程序博客网 时间:2024/06/07 14:42
在远程机器上执行脚本时,为了能够保证脚本的实时性,我们一般会将脚本存放到SVN上,远程机器通过SVN的操作去更新脚本;
SVN更新脚本只需要2步就可以实现了,这个地方使用到pysvn库,看下实现
# 初始化clientself.client = pysvn.Client()self.client.set_default_username(self.username)self.client.set_default_password(util.decrypt_des(self.password))#更新代码self.client.update(self.localPath)
但是,在实际情况中,可能会出现update失败的情况,我们增加异常后,重新checkout的操作,如下
#判断更新是否成功
revision = Nonetry: revision = self.client.update(self.localPath)except: log.exception(traceback.format_exc()) revision = Noneif not os.path.exists(os.path.join(self.localPath, ".svn")): revision = None
#更新失败,则重新checkoutif revision is None or revision[0].number == -1:
.......
self.client.checkout(self.url, self.localPath)
update异常一般这样就可以解决了,如果我们在比较复杂的环境下,比如,有些机器需要通过代理访问SVN,如何做呢?
svn通过代理,使用的方式是配置文件的方式,那我们需要将代理服务器信息,以文件写入的方式写到配置文件中,然后使用配置文件初始化svn对象即可,代码如下:
useProxy = util.get_prop("proxy.enable")if useProxy != "False": self._init_servers(configDir) self.client = pysvn.Client(configDir)else: self.client = pysvn.Client() self.client.set_default_username(self.username) self.client.set_default_password(util.decrypt_des(self.password))
这样就ok了~最后附上全部的代码片段
class SVNInfo: def __init__(self, **kwargs): self.agent = agent.Agent() if "id" in kwargs: self.id = kwargs["id"] if "url" in kwargs: self.url = kwargs["url"] if "username" in kwargs: self.username = kwargs["username"] if "password" in kwargs: self.password = kwargs["password"] if "localpath" in kwargs: self.localPath = kwargs["localpath"] if "startversion" in kwargs: self.startVersion = kwargs["startversion"] if "preversion" in kwargs: self.preVersion = kwargs["preversion"] def _init_servers(self, configDir): proxyIP = util.get_prop("proxy.ip") proxyPort = util.get_prop("proxy.port") proxyUserName = util.get_prop("proxy.username") proxyPwd = util.get_prop("proxy.password") configStr = "[global]\r\n" \ "http-proxy-host = %s\r\n" \ "http-proxy-port = %s\r\n" \ "http-proxy-username = %s\r\n" \ "http-proxy-password = %s" % (proxyIP, proxyPort, proxyUserName, proxyPwd) serversFilePath = os.path.join(configDir, "servers") serverFile = codecs.open(serversFilePath, "w", "utf-8") serverFile.write(configStr) serverFile.close() def init(self): self.localPath = util.get_act_str(self.localPath, self.agent.envDict) configDir = os.path.join(tempfile.gettempdir(), "pysvn") if not os.path.exists(configDir): os.makedirs(configDir) useProxy = util.get_prop("proxy.enable") if useProxy != "False": self._init_servers(configDir) self.client = pysvn.Client(configDir) else: self.client = pysvn.Client() self.client.set_default_username(self.username) self.client.set_default_password(util.decrypt_des(self.password)) def update(self): try: self.init() revision = None try: revision = self.client.update(self.localPath) except: log.exception(traceback.format_exc()) revision = None if not os.path.exists(os.path.join(self.localPath, ".svn")): revision = None if revision is None or revision[0].number == -1: log.exception("into rename") bakName = os.path.basename(self.localPath) + "_" + util.format_time() os.popen("rename \"%s\" \"%s\"" % (self.localPath, bakName)) log.exception("into checkout") self.client.checkout(self.url, self.localPath) except: log.exception(traceback.format_exc())
0 1
- Python写自动化之SVN更新
- Python写自动化之一种截图方法
- Python写自动化之http文件下载
- Python写自动化之logging日志写入
- Python写自动化之数据库操作
- Python写自动化之Ftp上传下载
- Python写自动化之写一个Windows 服务
- Python写自动化之使用sphinx提取Python代码docstring
- 初学SVN之 用Python写SVN的钩子文件
- Python写自动化之获取文件的MD5值
- Python写自动化之构造Multipartform-data发请求
- Python写自动化之文件的压缩及解压
- Python写自动化之启动进程并获取进程输出
- Python写自动化之邮件发送(匿名)
- Python写自动化之以不同优先级启动进程
- Python写自动化之ini文件的读写
- Python写自动化之注册表的读写操作
- Python写自动化之设置Windows自动登录
- 机器学习(非传统统计方法如回归)到底在量化金融里哪些方面有应用?
- URAL 1327. Fuses
- 设置html页面不让浏览器缓存的方法
- @SuppressWarnings 注解详解
- LDAP error Code 及解决方法
- Python写自动化之SVN更新
- OpenCV.2.Computer.Vision.Application.Programming.Cookbook--Accessing pixel values
- java时间戳
- PostMessage,SendMessage,GetMessage,PeekMessage,TranslateMessage,DispatchMessage的用法集合
- Ubuntu下玩转VI--在命令行下编辑文件
- 《尚学堂CAS系统讲解视频》AVI[压缩包]
- 漂亮的有缓冲效果的Android应用启动界面
- AE数据加载
- android 例子学习