由于本人所在的公司的svn创建分支的工具比较繁琐,故而决定自己用python自己写一个svn的打分支工具,废话不多说,直接上代码
import os
import time
from xml.etree import ElementTree
import subprocess
import modifyVersion
class createBranch:
copyBaseUrl = "svn co "
branchVersion = ""
baseDir =''
def __init__(self):
self.read_xml(open('project.xml',encoding='utf-8').read())
#读取xml文件这个xml文件是你自己配置好代码地址和保存分支的地址
def read_xml(self, text):
root = ElementTree.fromstring(text)
lst_node = root.getiterator("urlBean")
for node in lst_node:
createTargetBaseDir = node.attrib['dirname']
self.baseDir = createTargetBaseDir
brachname = node.attrib['brachname']
urls = node.getchildren()
for url in urls:
projects = url.getchildren()
for project in projects:
branchProjectName = project.text+"-"+brachname+self.branchVersion
createBranchCommand = url.attrib['value'].replace('project', project.text) +branchProjectName
if not os.path.exists(createTargetBaseDir):
os.makedirs(createTargetBaseDir)
self.createBranch(createTargetBaseDir,branchProjectName,createBranchCommand)
def createBranch(self, baseDir, projectName,createBranchCommand):
copyBranchCommand = self.copyBaseUrl + createBranchCommand.split(' ')[-1]
try:
svnList = subprocess.Popen('svn list '+createBranchCommand.split(' ')[-1], stdin=subprocess.PIPE,stderr=subprocess.PIPE,stdout=subprocess.PIPE, shell=True)
data =svnList.stdout.readlines()
if len(data) == 0:
os.system(createBranchCommand)
os.system('cd ' + baseDir + " && " + copyBranchCommand)
elif len(data) > 0:
print("该分支已经存在无需创建直接下载即可:" + copyBranchCommand + " \n")
os.system('cd ' + baseDir + " && " + copyBranchCommand)
except Exception as e:
pass
if __name__ =='__main__':
branch = createBranch()
project.xml:
<urlBean dirname="保存代码的地址" brachname="创建分支的名称" dirdesc="可以忽略">
<url value="svn cp -m '' svn主干地址 svn分支地址">
<project>hpWeb</project>
<project>hp2Web</project>
</url>
</urlBean>