自动拉取git分支代码,去掉pom.xml文件snap的版本

161 阅读1分钟
# -*- coding: utf-8 -*-

#自动拉取git分支代码,去掉pom.xml文件snap的版本

import os, time,sys
import  shutil
import  commands

class PomDeal(object):

    def __init__(self,git_url,branch):
        self.git_url = git_url
        self.branch = branch
        self.artifactId = "fenqifu-common"
        self.path = './repo'
        self.file_name = './repo/pom.xml'
	self.mvn = '/usr/bin/mvn'
    def clear_repo(self):
        print os.getcwd()
        result = os.path.isdir(self.path)
        if result:
            print "#1. 判断目录路径存在,如果存在执行清空目录操作"
            shutil.rmtree(self.path)
        else:
            print "目录不存在"
    def git_clone(self):
        print "#2. 执行clone代码操作,目录为./repo/下"
        os.system('git  clone -b %s %s  %s' % (self.branch,self.git_url,self.path))

    def file_modify(self):
        global  new_version
        com = "grep  -A 2 '<artifactId>%s</artifactId>' %s |grep version|awk -F '>' '{print $2}'|awk -F '<' '{print $1}'" % (self.artifactId,self.file_name)
        (status, old_version) = commands.getstatusoutput(com)
        new_version = old_version.split('-')[0]
	print new_version
        com1 = 'sed -i "s/%s/%s/g" %s' % (old_version,new_version,self.file_name)
        (status,output) = commands.getstatusoutput(com1)
        if status == 0:
            print "#3. 执行修改pom.xml文件,修改%s =======> %s" % (old_version,new_version)
        else:
            exit(10)

#commit修改记录
    def save_commit(self):
        CommitSave = 'cd %s && git add ./ && git commit -m "修改pom.xml文件version"'% (self.path)
        retu = os.system(CommitSave)
        if retu == 0:
            print "#4. commit修改记录"
#编译打包
    def mvn_compile(self):
        mvn_comm = 'cd %s && %s clean install -Dmaven.test.skip=True' % (self.path,self.mvn)
        print "#5. 执行编译"
        # (status,output) = commands.getstatusoutput(mvn_comm)
        os.system(mvn_comm)
        # print  output
#打tag
    def tag_compile(self):
        tag_comm = 'cd %s && git tag v%s' % (self.path,new_version)
        print "#6. 执行git tag"
        os.system(tag_comm)
    def build(self):
        self.clear_repo()
        self.git_clone()
        self.file_modify()
        self.save_commit()
        self.mvn_compile()
        self.tag_compile()



if __name__ == '__main__':
    shili = PomDeal(str(sys.argv[1]),str(sys.argv[2]))
    shili.build()


更多精彩关注公众号“51运维com”