svn 钩子( python版本 hook) commit后复制到webserver

#!/usr/bin/python
#coding=utf-8
'''文件提交的时候如果注释里有一行是指定的ip 则复制到对应的远程服务器(只复制到单台) '''

import sys,os
serverip = ["228"]
serverConfig = {"traderii":{"path":"/usr/local/www/TraderII"},"crm":{"path":"/usr/local/www/crm"}}
serverFullIP = {"228":"192.168.1.228"}
REV = ''
PROJ = ''
PROJNAME = ''

def command_output(cmd):
    " Capture a command's standard output. "
    import commands
    a,b = commands.getstatusoutput(cmd)
    return b

def main():
    "main job"
    global REV,PROJ,PROJNAME
    PROJ = sys.argv[1]
    REV = sys.argv[2]
    PROJNAME = PROJ[PROJ.rindex('/')+1:]
    f.write("%s /n" % (REV))
    f.write("%s /n" % (PROJ))
    
    
    changed = command_output("svnlook changed -r %s %s" % (REV,PROJ))
    loginfo = command_output("svnlook info -r %s %s" % (REV,PROJ))
    chgfiles = getModifiedFiles(changed)
    synFile(chgfiles,loginfo)    

    f.write("author: %s /n" % (loginfo))
    f.write(changed)

def synFile(chgfiles,info):
    '''上传文件'''
    if (len(serverip) == 0):
        return 0
    if (PROJNAME not in serverConfig):
        return 0
    info = info.split("/n")
    info = info[3:]
    
    toupload = []
    for i in info:
        i = i.strip()
        if (i in serverip):
            toupload.append(i)

    if (len(toupload) == 0):
        return 0
    uploadf = 0
    for f in chgfiles:
        act = chgfiles[f]
        if ('U' == act or 'A' == act):
            uploadf = folder = uploadFile(f)
    
    if (uploadf):
        uploadpath = serverConfig[PROJNAME]['path'] 
        for server in toupload:
            uploadip = serverFullIP[server]
            cmd = "scp -r  %s/* root@%s:%s" % (uploadf,uploadip,uploadpath)
            command_output(cmd)
            #print cmd 
        cmd = "rm -rf %s" % (uploadf)
        command_output(cmd)
        #print cmd

def uploadFile(file):
    '''从svn导出 放置在临时文件夹'''
    #print file
    tmpfolder = "/root/svn/%s" % (REV)
    tmpfile = file[file.rindex("/")+1:]
    if (not os.path.isdir(tmpfolder)):
        os.mkdir(tmpfolder)
    tmpfile = "%s/%s" % (tmpfolder,tmpfile)
    cmd = "svn export -r %s svn://127.0.0.1/%s/%s %s" % (REV,PROJNAME,file,tmpfile)
    res = command_output(cmd)

    datafolder = "/root/svnt/%s" % (REV)
    desfolder = "%s/%s" % (datafolder,file[file.index("/")+1:file.rindex('/')])
    desfile = "%s/%s" % (desfolder,file[file.rindex("/")+1:])

    if (not os.path.isdir(desfolder)):
        cmd = "mkdir -p %s" % (desfolder)
        command_output(cmd)

    
    cmd = "svn export -r %s svn://127.0.0.1/%s/%s %s" % (REV,PROJNAME,file,desfile)
    command_output(cmd)

    #cmd = "scp -r %s [email protected]:/root/tmp" % (tocopy_dir)
    if (res):
        return datafolder    
    else:
        return false




def getModifiedFiles(logtxt):
    '''修改被修改文件的动作'''
    files = logtxt.split("/n")
    ret = {}
    for f in files:
        act = f[0:1]
        f = f[1:].strip()
        ret[f] = act
    return ret 

if __name__ == "__main__":
    #import sys
    f=file('/usr/local/www/argv.txt','w')
    r = main()
    f.close()
    sys.exit(r)

你可能感兴趣的:(SVN,python,command,cmd,hook,output)