Python定期从SVN更新文件

发布时间:2019-08-13 07:39:58编辑:auto阅读(3007)

    如果一个测试站点要及时的从svn获取最新的文件,那么写一个定期更新程序是非常必要的,下面的代码Python的简单实现

    svnupdate.py
    import time,os,sys,svnconfig

    dist
    =svnconfig.setting['dist']
    os.chdir(svnconfig.setting[
    'svn'])

    def checkout():
        svnconfig.setting[
    'dist']=dist+time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime())
        cmd
    ='svn export %(url)s %(dist)s --username %(user)s --password %(pwd)s'%svnconfig.setting
        
    print "execute %s"%cmd
        
    #print os.popen(cmd).read()
        return os.system(cmd)

    while True:
        ret
    =checkout()
        
    if(ret==0):
            
    print 'check out success'
        
    else:
            
    print 'check out fail'
        time.sleep(svnconfig.setting[
    'interval'])

    svnconfig.py

    setting={
        
    'svn':'C:/Program Files/Subversion/bin/',#svn的程序所在路径
        'url':'http://www.xxx.com/svn/project/trunk',#svn地址
        'user':'xxx,#用户名
        'pwd':'xxx',#密码
        'dist':'D:/svn/',#目标地址
        'interval':15 #更新时间
    }

    可能是装了多个svn的缘故,直接用system('svn xx')命令执行失败,所以就先chdir到svn的目录下面,然后在执行svn命令

    svn也有一个Python程序包,也可以直接使用,没有具体使用过。
    svn以及python包的地址:
     http://subversion.tigris.org/servlets/ProjectDocumentList?folderID=91

    上面的代码主要用了 svn export ,该命令获取了一个无版本控制的副本,所以用于定时备份比较好,及时更新的话用svn checkout 命令,每次获得最新的版本,或者第一次用svn checkout以后用svn -r HEAD update也可以.

关键字