1 #!/usr/bin/env python
2 #coding=utf-8
3
4 #启动环境:linux,安装了python,svn客户端,并且具备python部分依赖包,xlrd,shutil,如果没有网上搜索安装
5 #启动方式:将文件MvSvnLibToNew.py和MvSvnLibToNew.xls放到机器的某个目录
6 #然后nohup python MvSvnLibToNew.py type > 1.log& 启动,type=0-只检测配置,不上传,type=1-既检测配置,又上传
7 #配置文件MvSvnLibToNew.xls,有7列配置项,分别为
8
9 #配置文件参数说明如下
10 #old_url:老库的待迁移的svn地址
11 #old_local_path:下载到本地的目录
12 #old_username:老库的svn用户名
13 #old_password:老库的svn密码
14 #new_url:新库的上传路径
15 #new_username:新库的svn用户名
16 #new_password:新库的svn密码
17
18 #有如下检测功能
19 #检测old_url是否存在,用户名密码是否正确
20 #检测new_url是否存在,用户名密码是否正确
21 #检测new_url路径配置是否跟old_new的子目录一样
22
23 import os
24 import xlrd
25 import sys
26 import shutil
27
28 #python 3.8不需要
29 #reload(sys)
30 #sys.setdefaultencoding('utf-8')
31
32 def execCmd(cmd):
33 r = os.popen(cmd)
34 text = r.read()
35 r.close()
36 return text
37
38 #checkout文件到本地
39 def checkOutOldLib(old_url, old_local_path, old_username, old_password):
40 command_str = "svn co " + old_url + " " + old_local_path + " --username " + old_username + " --password " + old_password + " --non-interactive"
41 print(command_str)
42 text = execCmd(command_str)
43 print(text)
44
45 #判断远程目录是否存在
46 def judgeRemoteDirExist(old_url, old_username, old_password, new_url, new_username, new_password):
47 #判断老库是否存在
48 command_str = "svn ls " + old_url + " --username " + old_username + " --password " + old_password
49 print(command_str)
50 text = execCmd(command_str)
51 #print(text)
52 if len(text) == 0 :
53 print("ERROR:old_url[%s]不存在!" % (old_url))
54 return(False)
55
56 #首先判断新老连接配置最后的目录是否,比如old:xxxxx/develop,new:yyyyyy/develop,则会上传失败
57 listDataOld = old_url.split("/")
58 listDataNew = new_url.split("/")
59 #如果配置一样,则需要检测新库目录是否存在
60 if listDataOld[len(listDataOld)-1] == listDataNew[len(listDataNew)-1] :
61 print("ERROR old_url[%s] 和 new_url[%s] 子目录配置一样,配置错误,请重新配置!" % (old_url, new_url))
62 return(False)
63 else :
64 command_str = "svn ls " + new_url + " --username " + new_username + " --password " + new_password
65 print(command_str)
66 text = execCmd(command_str)
67 #print(text)
68 if len(text) == 0 :
69 return(True)
70
71 #检查目录是否存在
72 #print(listDataOld[len(listDataOld)-1])
73 nPos = text.find(listDataOld[len(listDataOld)-1])
74 #print(nPos)
75 #如果存在
76 if nPos >= 0 :
77 print("ERROR new_url: [%s] 已存在 [%s],导入失败,请确保新库导入为新的空目录! " % (new_url, listDataOld[len(listDataOld)-1]))
78 return(False)
79 else :
80 return(True)
81
82 #全量检测
83 def allCheckConfig(sheetobj):
84 iSuccess = 0
85 iFailed = 0
86 iBool = True
87 nrows = sheetobj.nrows
88 ncols = sheetobj.ncols
89 print("*********************************************************************************************")
90 #先做一个全量检测
91 for i in range(1, nrows):
92 for j in range(ncols):
93 if j == 0:
94 old_url = sheetobj.row_values(i)[j]
95 print("old_url: %s" % (sheetobj.row_values(i)[j]))
96 elif j == 1:
97 old_local_path = sheetobj.row_values(i)[j]
98 print("old_local_path: %s" % (sheetobj.row_values(i)[j]))
99 elif j == 2:
100 old_username = sheetobj.row_values(i)[j]
101 print("old_username: %s" % (sheetobj.row_values(i)[j]))
102 elif j == 3:
103 old_password = sheetobj.row_values(i)[j]
104 print("old_password: %s" % (sheetobj.row_values(i)[j]))
105 elif j == 4:
106 new_url = sheetobj.row_values(i)[j]
107 print("new_url: %s" % (sheetobj.row_values(i)[j]))
108 elif j == 5:
109 new_username = sheetobj.row_values(i)[j]
110 print("new_username: %s" % (sheetobj.row_values(i)[j]))
111 elif j == 6:
112 new_password = sheetobj.row_values(i)[j]
113 print("new_password: %s" % (sheetobj.row_values(i)[j]))
114 print(" ")
115 listData = old_url.split("/")
116 old_local_path = old_local_path + "/" + listData[len(listData)-1]
117
118 if judgeRemoteDirExist(old_url, old_username, old_password, new_url, new_username, new_password) == False :
119 print("配置异常!")
120 iFailed += 1
121 iBool = False
122 else :
123 print("配置正常!")
124 iSuccess += 1
125 print("*********************************************************************************************")
126
127 print("总共检测%s个,其中配置正常%s个,配置异常%s个" % (nrows-1, iSuccess, iFailed))
128 print("*********************************************************************************************")
129
130 return(iBool)
131
132 #全量导入新库
133 def allImportSvn(sheetobj):
134 nrows = sheetobj.nrows
135 ncols = sheetobj.ncols
136
137 #全量导入
138 for i in range(1, nrows):
139 for j in range(ncols):
140 if j == 0:
141 old_url = sheetobj.row_values(i)[j]
142 print("old_url: %s" % (sheetobj.row_values(i)[j]))
143 elif j == 1:
144 old_local_path = sheetobj.row_values(i)[j]
145 print("old_local_path: %s" % (sheetobj.row_values(i)[j]))
146 elif j == 2:
147 old_username = sheetobj.row_values(i)[j]
148 print("old_username: %s" % (sheetobj.row_values(i)[j]))
149 elif j == 3:
150 old_password = sheetobj.row_values(i)[j]
151 print("old_password: %s" % (sheetobj.row_values(i)[j]))
152 elif j == 4:
153 new_url = sheetobj.row_values(i)[j]
154 print("new_url: %s" % (sheetobj.row_values(i)[j]))
155 elif j == 5:
156 new_username = sheetobj.row_values(i)[j]
157 print("new_username: %s" % (sheetobj.row_values(i)[j]))
158 elif j == 6:
159 new_password = sheetobj.row_values(i)[j]
160 print("new_password: %s" % (sheetobj.row_values(i)[j]))
161 listData = old_url.split("/")
162 old_local_path = old_local_path + "/" + listData[len(listData)-1]
163
164 if judgeRemoteDirExist(old_url, old_username, old_password, new_url, new_username, new_password) == True :
165 new_url = new_url + "/" + listData[len(listData)-1]
166 checkOutOldLib(old_url, old_local_path, old_username, old_password)
167 importNewLib(new_url, old_local_path, new_username, new_password)
168 shutil.rmtree(old_local_path, ignore_errors=True)
169 print("*********************************************************************************************")
170
171
172 #上传本地文件到新库
173 def importNewLib(new_url, old_local_path, new_username, new_password):
174 command_str = "svn import " + old_local_path + " " + new_url + " -m 老库迁移至新库 --username " + new_username + " --password " + new_password
175 print(command_str)
176 text = execCmd(command_str)
177 print(text)
178
179 #读取配置的xls配置文件
180 def readExcelData(excel_path):
181 bookobj = xlrd.open_workbook(excel_path,encoding_override="utf-8")
182 sheetobj = bookobj.sheet_by_index(0)
183 return sheetobj
184
185
186 if __name__ == "__main__":
187 excel_path = "./MvSvnLibToNew.xls"
188 sheetobj = readExcelData(excel_path)
189
190 if len(sys.argv) != 2 :
191 print("start: \tpython MvSvnLibToNew.py type")
192 print("param:")
193 print("\ttype:")
194 print("\t0-只检测配置,不上传")
195 print("\t1-既检测配置,又上传")
196 exit(1)
197
198
199 #如果带了参数,type=0,只检测,不上传,type为其他,既检测又上传
200 #如果不带参数,既检测又上传
201 if len(sys.argv) == 2 :
202 type = sys.argv[1]
203 #只检测,不上传
204 if type == '0' :
205 if allCheckConfig(sheetobj) == False :
206 exit(1)
207 exit(1)
208 elif type == '1' :
209 if allCheckConfig(sheetobj) == False :
210 exit(1)
211 allImportSvn(sheetobj)