標籤:svn 更新 Proxy 伺服器
在遠程機器上執行指令碼時,為了能夠保證指令碼的即時性,我們一般會將指令碼存放到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通過代理,使用的方式是設定檔的方式,那我們需要將Proxy 伺服器資訊,以檔案寫入的方式寫到設定檔中,然後使用設定檔初始化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())
Python寫自動化之SVN更新