When executing the script on the remote machine, in order to guarantee the real-time script, we usually put the script on the SVN, the remote machine through the SVN operation to update the script;
The SVN update script can be implemented in just 2 steps, and this place uses the PYSVN library to see the implementation
# Initialize 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)
However, in the actual situation, there may be an update failure condition, after we increase the exception, re-checkout the operation, as follows
#判断更新是否成功
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
#更新失败, re-checkoutif revision is None or revision[0].number = =-1:
.......
Self.client.checkout (Self.url, Self.localpath)
Update exception generally this can be solved, if we are in a more complex environment, for example, some machines need to access SVN through a proxy, how to do it?
SVN through the proxy, the way is the configuration file, then we need to send the proxy server information as a file write to the configuration file, and then use the configuration file to initialize the SVN object, the code is as follows:
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))
This will be OK ~ Finally attach all the code snippets
Class Svninfo:def __init__ (Self, **kwargs): self.agent = Agent. Agent () If "id" in kwargs:self.id = kwargs["id"] if "url" in Kwargs:self.url = Kwarg s["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 Kwar Gs:self.preVersion = kwargs["Preversion"] def _init_servers (self, configdir): Proxyip = Util.get_pro P ("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_st R (Self.localpath, self.agent.envDict) Configdir = Os.path.join (Tempfile.gettempdir (), "PYSVN") if not Os.pat H.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 (s Elf.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")): R Evision = 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 writes the SVN update for automation