-
Notifications
You must be signed in to change notification settings - Fork 2
/
autobuild.py
255 lines (215 loc) · 8.26 KB
/
autobuild.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import shutil
import os,sys,string,datetime,time
import socket
import tarfile
import glob
import telnetlib
import re
import threading
from time import sleep,ctime,gmtime,strftime
class thread(threading.Thread):
def __init__(self, num, interval,bpath):
threading.Thread.__init__(self)
self.thread_num = num
self.interval = interval
self.thread_stop = False
self.bpath = bpath
def run(self):
def getdirsize(dir):
size = 0.0
for root, dirs, files in os.walk(dir):
size += sum([os.path.getsize(os.path.join(root, name)) for name in files])
return size
sleep(30)
flag = True
while(flag == True):
kitSize1 = getdirsize(self.bpath)
print self.bpath
print kitSize1
sleep(60)
kitSize2 = getdirsize(self.bpath)
print kitSize2
speed = float(kitSize2 - kitSize1)/60.0/1024
print speed
speedlog = 'The Downloading Speed is ' + str(speed) + 'kb/s'
dlog.writelog(speedlog,logPath)
if speed == 0.0:
flag = False
def stop(self):
self.thread_stop = True
class Build:
def getLastestBuild(self,path):
buildList = os.listdir(path)
#print buildList
ftime = []
for folder in buildList:
ftime.append(os.path.getctime(os.path.join(path,folder)))
ftime_temp = ftime[:]
ftime_temp.sort()
ftime_temp.reverse()
timeMax = ftime_temp[0]
timeMaxIndex = ftime.index(timeMax)
return buildList[timeMaxIndex]
def delOldestBuild(self,path,rpath):
kitList = os.listdir(path)
kitList.remove('installog.txt')
kitList.remove('installversion.txt')
kitList.remove('b1')
kitList.remove('b2')
kitList.remove('b3')
kitList.remove('b4')
kitList.remove('b5')
ftime = []
for folder in kitList:
ftime.append(os.path.getctime(os.path.join(path,folder)))
ftime_temp = ftime[:]
ftime_temp.sort()
kitDel = ftime_temp[0]
kitDelIndex = ftime.index(kitDel)
delKitName = kitList[kitDelIndex]
print os.path.join(path,delKitName)
shutil.rmtree(os.path.join(path,delKitName)) #delete the local builds
shutil.rmtree(os.path.join(rpath,delKitName)) # delete the remote buils
def download(self,folder,spath,dpath):
try:
src = os.path.join(os.path.join(os.path.join(os.path.join(spath,folder),'winx64h'),'compressed'),'bisrvr')
dst = os.path.join(os.path.join(os.path.join(os.path.join(dpath,folder),'winx64h'),'compressed'),'bisrvr')
t = thread(1,1,dst)
t.start()
dlog.writelog('Start downloading',logPath)
shutil.copytree(src,dst,524288000)
t.stop()
except:
print 'copy failed'
shutil.rmtree(os.path.join(dpath,folder))
sys.exit(0)
#return
def setversion(self,version,versionfile,verlog):
version_handler = open(versionfile,'wb')
version_handler.write(version)
version_handler.close()
version_handler = open(verlog,'a')
version_handler.write('\n'+version)
version_handler.close()
def getversion(self,versionfile):
version_handler = open(versionfile,'rb')
instversion = version_handler.readline()
version_handler.close()
return instversion
def copy2vm(self,src,dis):
srcFolder = os.listdir(src)
disFolder = os.listdir(dis)
srcFolder.remove('installog.txt')
srcFolder.remove('installversion.txt')
dlog.writelog('Start copying to VM',logPath)
if len(disFolder) == 0:
for targetFile in srcFolder:
shutil.copytree(os.path.join(src,targetFile),os.path.join(dis,targetFile),524288000)
else:
for filename in disFolder:
srcFolder.remove(filename)
targetFolder = srcFolder # list.remove() return nothing
for targetFile in targetFolder:
#print os.path.join(src,targetFile)
#print os.path.join(dis,targetFile)
shutil.copytree(os.path.join(src,targetFile),os.path.join(dis,targetFile),524288000)
return
class ZIP:
def creat_uzippath(self,agrm1):
os.makedirs(agrm1)
def extract_file(self,zippath,uzippath):
try:
print 'start unzipping'
dlog.writelog('Start unzipping',logPath)
tar = tarfile.open(zippath)
names = tar.getnames()
for name in names:
tar.extract(name,path= uzippath)
tar.close()
except:
dlog.writelog('unZip failed',logPath)
print 'unZip failed'
sys.exit(0)
return
print 'unzip completely'
dlog.writelog('unZip completely',logPath)
class HTML:
def txt2json(self,path):
targetPath="C:\\Program Files (x86)\\Apache Software Foundation\\Apache2.2\\htdocs\\installog.json"
sourPath = path
file_handler = open(sourPath,'rt')
buildList = file_handler.readlines()
buildList.reverse()
file_handler.close()
jsonlist = []
for x in buildList:
buildtmp = x.split("\n")
build = buildtmp[0]
build = build.strip()
if x == buildList[0]:
jsonele = '[' + "{" + '"id":' + str(buildList.index(x)) + ',' + '"text"' + ':' + '"' + build + '"' + "}" + ','
if x == buildList[len(buildList)-1]:
jsonele = "{" + '"id":' + str(buildList.index(x)) + ',' + '"text"' + ':' + '"' + build + '"' + "}" + ']'
if x != buildList[0] and x != buildList[len(buildList)-1]:
jsonele = "{" + '"id":' + str(buildList.index(x)) + ',' + '"text"' + ':' + '"' + build + '"' + "}" + ','
jsonlist.append(jsonele + '\r\n')
jsonname = targetPath
file_handler2 = open(jsonname, 'wb')
file_handler2.writelines(jsonlist)
file_handler2.close()
def getcmplst(self,insversion,sourcedir):
cmplstdir = sourcedir + '\\winx64h\\versions.ini'
file_handler = open(cmplstdir,'rb')
cmplst = file_handler.readlines()
file_handler.close()
targetdir= "C:\\Program Files (x86)\\Apache Software Foundation\\Apache2.2\\htdocs\\" + insversion + '.txt'
file_handler2 = open(targetdir,'wb')
file_handler2.writelines(cmplst)
file_handler2.close()
class Log:
def writelog(self,log,logpath):
path = logpath
currentime = strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
file_handler = open(path,'ab')
file_handler.writelines(currentime + ' '+ log + '\n')
file_handler.close()
if __name__ == '__main__':
spath = "path"
dpath = "C:\installbuilds"
insVer = 'installversion.txt'
insVerPath = os.path.join(dpath,insVer)
insLog = 'installog.txt'
insLogPath = os.path.join(dpath,insLog)
vmPath = "vpath"
logPath = "C:\\Aaron\\python\\downloadinglog.txt"
try:
dlog = Log()
b = Build()
bname = b.getLastestBuild(spath)
dlog.writelog(bname,logPath)
zipPath = os.path.join(os.path.join(dpath,bname),'builds')
print bname
pattern = re.compile(r'buildname')
match = pattern.match(bname)
if match:
if bname != b.getversion(insVerPath):
b.download(bname,spath,dpath)
b.setversion(bname,insVerPath,insLogPath)
else:
print 'the build exist'
sys.exit(0)
else:
print 'the build is not vaild'
sys.exit(0)
tarBuildFilePath = glob.glob(os.path.join(dpath,bname)+"/*/*/*/*.tar.gz")[0]
b.copy2vm(dpath,vmPath)
z = ZIP()
z.creat_uzippath(zipPath)
z.extract_file(tarBuildFilePath,zipPath)
h = HTML()
h.getcmplst(bname,zipPath)
h.txt2json(insLogPath)
b.delOldestBuild(dpath,vmPath)
except Exception,e:
emessage = str(e)
dlog.writelog(emessage,logPath)