forked from mr-kelly/svncommander
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
174 lines (137 loc) · 4.34 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#coding=utf-8
"""
SVN工具集, python指令调用svn系统命令
主要其实就是字符串处理
可跨平台使用哦
by Kelly ([email protected])
"""
import os
import subprocess
import tempfile
import argparse
# SCRIPTPATH = os.path.dirname(os.path.realpath(__file__))
# os.chdir(SCRIPTPATH)
svnDirPath = "https://cosmosbox/svn/cosmosbox/trunk/cb-product/AssetBundles/"
cmdSvnDiffVersion = """svn diff -r %s:%s """ + svnDirPath
cmdGetHeadVersionNum = "svn info -r HEAD "
cmdCheckout = "svn co %s -r %s %s" # url / version / targetPath
cmdSvnList = "svn list -v -R"
cmdChangeList = "svn status"
def shellCall(cmd):
print "shellCall: '%s'" % cmd
subprocess.call(cmd, shell=True);
def call(cmd):
print "call: '%s'" % cmd
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
return p.stdout.read()
class SvnCommander:
def __init__(self, workPath, svnExePath='svn'):
self.svnExePath = os.path.realpath(svnExePath)
self.workPath = os.path.realpath(workPath)
def getLines(self, strs):
"""
svn指令都是 xxx: vvvv格式, 输入xxx获取vvv, key不唯一时返回list
>>> len(testCommander.getLines('abcdefg\\nasdaqwew\\n1235\\n'))
3
"""
retList = [s.strip() for s in strs.split('\n') if s]
return retList
def parseSvnResult(self, strs):
"""
>>> result = testCommander.parseSvnResult('abcd : ok!\\ndefg: not ok!')
>>> result['abcd']
'ok!'
>>> result['defg']
'not ok!'
"""
lines = self.getLines(strs)
ret = {}
for line in lines:
lineArr = line.split(':', 1)
if len(lineArr) >= 2:
key = lineArr[0].strip()
value = lineArr[1].strip()
ret[key] = value
return ret
def getInfo(self, strs, key):
result = self.parseSvnResult(strs)
if (result[key]):
return result[key]
return None
def merge(self, url):
"""
合并,传入url,与本地工作目录合并(不允许URL之间合并,太危险)
"""
shellCall('%s merge %s@HEAD %s' % (self.svnExePath, url, self.workPath))
def update(self):
shellCall('%s update' % (self.svnExePath))
def switch(self, url):
shellCall('%s switch %s %s' % (self.svnExePath, url, self.workPath))
def getRepoUrl(self):
"""
>>> 'http' in testCommander.getRepoUrl()
True
"""
_cmd = self.svnExePath + ' info -r HEAD ' + self.workPath
result = call(_cmd)
return self.getInfo(result, 'URL')
def getDiffFiles(self, fromVersion, toVersion):
"""
获取版本变动的文件列表
"""
result = call(cmdSvnDiffVersion % (fromVersion, toVersion))
return self.parseSvnResult(result)["Index"]
def getHeadVersionNum(self):
"""
获取HEAD版本号
>>> testCommander.getHeadVersionNum().isdigit()
True
"""
result = call(cmdGetHeadVersionNum + self.workPath)
return self.parseSvnResult(result)["Revision"]
def GetTmpSvnDir(self, version):
"""获取指定版本的临时文件夹"""
tempPath = tempfile.gettempdir()
versionDir = '_'.join(svnDirPath.split('/')[-3:])
tempPath = os.path.join(tempPath, "CosmosSVN", "%s_%s" %(versionDir, version))
return tempPath
def CheckoutToTmpDir(self, version, tempPath):
""" 临时文件夹,checkout指定版本 """
print call(cmdCheckout % (svnDirPath, version, tempPath))
def GetSvnList(self, strPath):
""" 获取指定目录所有svn内文件及其信息"""
os.chdir(strPath)
return GetLines(call(cmdSvnList))
def GetChangedList(self, strPath):
""" 獲取指定目錄下面所有改動的文件 """
os.chdir(strPath)
lines = GetLines(call(cmdChangeList))
retList = []
for line in lines:
if line[0] in ['?', 'M']:
fRPath = line.replace('?', '').replace('M', '').strip()
retList.append(fRPath)
return
def GetDelList(self, strPath):
"""
獲取指定目錄下面所有刪除或missing的文件
"""
os.chdir(strPath)
lines = GetLines(call(cmdChangeList))
retList = []
for line in lines:
if line[0] in ['!', 'D']:
fRPath = line.replace('!', '').replace('D', '').strip()
retList.append(fRPath)
return retList
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Svn Helper! TODO command line!")
parser.add_argument("path")
parser.add_argument("--svn_exe", default="svn.exe")
parser.add_argument("--test", default=False)
args = parser.parse_args()
if args.test:
import doctest
doctest.testmod(extraglobs={'testCommander':SvnCommander(args.path, args.svn_exe)})
else:
print('do nothing! maybe you want to --test True ??')