Python写自动化之SVN更新

发布时间:2019-09-21 10:44:57编辑:auto阅读(1811)

    在远程机器上执行脚本时,为了能够保证脚本的实时性,我们一般会将脚本存放到SVN上,远程机器通过SVN的操作去更新脚本;

    SVN更新脚本只需要2步就可以实现了,这个地方使用到pysvn库,看下实现

    # 初始化client
    self.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 = 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
    
    
    #更新失败,则重新checkout
    if 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())




关键字