forked from LishanZhong/equant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.py
407 lines (323 loc) · 14.4 KB
/
update.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import os
import sys
import datetime
import shutil
import zipfile
import traceback
import configparser
import signal, psutil
import subprocess
import requests
from requests.adapters import HTTPAdapter
import time
ssn = requests.Session()
ssn.mount('http://', HTTPAdapter(max_retries=4))
ssn.mount('https://', HTTPAdapter(max_retries=4))
VERSION = ""
EPVERSION = ""
EQVERURL = "https://gitee.com/epolestar/equant/raw/master/VerNo.txt"
EPVERURl = "https://gitee.com/epolestar/equant/raw/master/EpVerNo.txt"
equantUrl = "https://epolestar-master-1255628687.cos.ap-beijing.myqcloud.com/"
epolestarUrl = "https://epolestar95-1255628687.cos.ap-beijing.myqcloud.com/epolestar.zip"
#urls = [equantUrl, epolestarUrl]
#tags = ["equant", "epolestar"]
EPOURLTAG = [epolestarUrl, "epolestar"]
EQUURLTAG = [equantUrl, "equant"]
zipname = ["equant.zip", "epolestar.zip"]
# 脚本所在路径
dirPath = os.getcwd()
# 工作路径(appdata所在路径)
# appPath = os.getenv("APPDATA")
if not os.path.exists(os.path.abspath(os.path.join(dirPath, "..\\equantbackup"))):
os.makedirs(os.path.join(dirPath, "..\\equantbackup"))
os.chdir(os.path.abspath(os.path.join(dirPath, "..\\equantbackup")))
workDir = os.getcwd()
styPath = os.path.join(workDir, "equant-master\\src\\strategy")
cfgPath = os.path.join(workDir, "equant-master\\src\\config")
logPath = os.path.join(workDir, "equant-master\\src\\log")
tempPath1 = os.path.join(dirPath, "src\\strategy")
tempPath2 = os.path.join(dirPath, "src\\config")
tempPath3 = os.path.join(dirPath, "src\\log")
# 版本号文件名
verName = "EPOVERSION.txt"
# 服务器上解压后需要删除的文件夹名称
equDelPath = os.path.join(workDir, "equant-master")
epoDelPath = os.path.join(workDir, "epolestar")
verDelPath = os.path.join(workDir, verName)
# 备份用户文件
def backup(directory, des):
if os.path.exists(directory):
# ret = os.system(f'xcopy "{directory}" "{des}" /y /e /i /h')
cmdstr = f'xcopy "{directory}" "{des}" /y /e /i /h /q /exclude:..\\equant\\exclude.txt'
#ret = os.system(f'xcopy "{directory}" "{des}" /y /e /i /h /q /exclude:..\\equant\\exclude.txt')
p = subprocess.Popen(cmdstr, shell=True, stdout=subprocess.PIPE)
p.wait()
if p.returncode == 0:
print("%s 备份成功!备份目录:%s." % (directory, des))
return 1
else:
print("%s 备份失败!备份目录:%s,失败信息:%s" % (directory, des, ''.join([s.decode('gbk') for s in p.stdout.readlines()])))
return 0
else:
print("Equant or Equant-master directory dosen't exist!")
return 0
# 关9.5
def killEpProcess():
if "epolestar v9.5.exe" in os.popen('tasklist /fi "IMAGENAME eq %s"' % "epolestar v9.5.exe").read():
# os.system("taskkill /f /im %s" % "epolestar v9.5.exe")
#os.system('taskkill /f /fi "IMAGENAME eq %s"' % "epolestar v9.5.exe")
cmdstr = 'taskkill /f /fi "IMAGENAME eq %s"' % "epolestar v9.5.exe"
p = subprocess.Popen(cmdstr, shell=True, stdout=subprocess.PIPE)
p.wait()
if p.returncode == 0:
print("极星客户端进程关闭成功.")
else:
print("极星客户端进程关闭失败, %s" % ''.join([s.decode('gbk') for s in p.stdout.readlines()]))
# 杀进程及子进程
def killProcesses(parent_pid, sig=signal.SIGTERM):
try:
parent = psutil.Process(parent_pid)
except psutil.NoSuchProcess:
return
children = parent.children(recursive=True)
for process in children:
process.send_signal(sig)
try:
cmdstr = 'taskkill /f /PID %d /fi "IMAGENAME eq python.exe"' % parent_pid
#os.system('taskkill /f /PID %d /fi "IMAGENAME eq python.exe"' % parent_pid)
p = subprocess.Popen(cmdstr, shell=True, stdout=subprocess.PIPE)
p.wait()
if p.returncode == 0:
print('量化终端进程关闭成功, Pid:%s.' %parent_pid)
else:
print('量化终端进程关闭失败, Pid:%s,失败信息:%s' %(parent_pid, ''.join([s.decode('gbk') for s in p.stdout.readlines()])))
except:
print("process %s doesn't exist" % parent_pid)
def requestZip(zipUrl, tag):
"""下载"""
try:
response = ssn.get(zipUrl, timeout=10)
response.raise_for_status()
with open (tag+".zip", "wb") as f:
f.write(response.content)
print("%s download successfully!"%tag)
return 1
except Exception as e:
print(tag, ":", "There is an exception %s when downloading file!" % (e))
return 0
def zipFile(filename):
"""解压"""
if not zipfile.is_zipfile(filename):
print("%s is not an .zip file" % filename)
return
z = zipfile.ZipFile(filename)
z.extractall()
print("%s unzip successfully!" % filename)
z.close()
def delzip(filename):
"""删除"""
if os.path.exists(filename):
os.remove(filename)
print("Delete %s successfully!" % filename)
def delFile(path):
"""删除文件夹"""
if os.path.exists(path):
shutil.rmtree(path, onerror=readonly_handler)
return 1
return 0
# 移动文件
def mergeFile(src, dst):
"""
:param src: 源路径
:param dst: 目标路径
:return:
"""
if not os.path.exists(src):
return
if not os.path.exists(dst):
os.makedirs(dst)
for sf in os.listdir(src):
sfPath = os.path.join(src, sf)
dfPath = os.path.join(dst, sf)
if os.path.isdir(sfPath):
if os.path.basename(sfPath) == "__pycache__":
continue
if os.path.exists(dfPath):
mergeFile(sfPath, dfPath)
else:
#print("Copy %s ===> %s"%(sfPath, dfPath))
#shutil.copyfile(sfPath, dfPath)
#os.system(f'xcopy "{sfPath}" "{dfPath}" /y /e /i /h /q')
cmdstr = f'xcopy "{sfPath}" "{dfPath}" /y /e /i /h /q'
p = subprocess.Popen(cmdstr, shell=True, stdout=subprocess.PIPE)
p.wait()
if p.returncode != 0:
print("量化终端升级出错: %s" % ''.join([s.decode('gbk') for s in p.stdout.readlines()]))
elif os.path.isfile(sfPath):
if os.path.exists(dfPath):
# shutil.copyfile(sfPath, dfPath)
#os.system(f'xcopy "{sfPath}" "{dfPath}" /y /e /i /h /q')
#print("Cover %s ===> %s" % (sfPath, dfPath))
cmdstr = f'xcopy "{sfPath}" "{dfPath}" /y /e /i /h /q'
p = subprocess.Popen(cmdstr, shell=True, stdout=subprocess.PIPE)
p.wait()
if p.returncode != 0:
print("量化终端升级出错: %s" % ''.join([s.decode('gbk') for s in p.stdout.readlines()]))
else:
# shutil.copy2(sfPath, dfPath)
shutil.copy(sfPath, dfPath)
#os.system(f"xcopy {sfPath} {dfPath} /y /e /i /h")
#print("copy %s ===> %s" % (sfPath, dfPath))
def readonly_handler(func, path, ececinfo):
os.chmod(path, 128)
func(path)
# 检查equant是否需要更新
def checkEquUpdate():
global VERSION
try:
if os.path.exists('../equant/VerNo.txt'):
with open('../equant/VerNo.txt', 'r') as f:
VERSION = f.read()
lvl = VERSION.split('.')[:-1]
lmv = '.'.join(lvl)
rsp = ssn.get(EQVERURL, timeout=10)
if rsp.status_code == 200:
rvstr = rsp.content.decode('utf-8')
rvl = rvstr.split('.')[:-1]
rmv = '.'.join(rvl)
#print("Start epolestar, version info, equant local version: %s, remote version: %s!" %(VERSION, rvstr))
if (len(lmv) == len(rmv) > 0 and rmv > lmv) or ( 0 < len(lmv) != len(rmv)):
return rmv
else:
print("量化终端(equant)不需要升级, 本地版本:%s, 最新版本:%s" %(lmv, rmv))
return ""
except Exception as e:
print("checkUpdate epolestar Error:%s" %(traceback.format_exc()))
# 检查epolestar是否需要更新
def checkEpoUpdate():
global EPVERSION
try:
# 读取epolestar文件夹中的本地版本号和服务器端版本号比对
if os.path.exists('../epolestar/update.ini'):
conf_reader = configparser.ConfigParser()
conf_reader.read('../epolestar/update.ini')
EPVERSION = conf_reader.get("version", "cur")
lmv = EPVERSION
rsp = ssn.get(EPVERURl, timeout=10)
if rsp.status_code == 200:
rvstr = rsp.content.decode('utf-8')
with open(verName, 'w') as f:
f.write(rvstr)
if os.path.exists(verName):
conf_reader = configparser.ConfigParser()
conf_reader.read(verName)
ver = conf_reader.get("version", "cur")
rmv = ver
#print("Start epolestar, version info, epolestar local version: %s, remote version: %s!" %(EPVERSION, rmv))
if (len(lmv) == len(rmv) > 0 and rmv > lmv) or ( 0 < len(lmv) != len(rmv)):
return rmv
else:
print("极星客户端(epolestar)不需要升级, 本地版本:%s, 最新版本:%s" %(lmv, rmv))
return ""
except Exception as e:
print("checkUpdate epolestar Error:%s" %(traceback.format_exc()))
def main(versionNo=None):
inp = input("------有新版本可以升级!-------\n"
"升级过程将自动关闭极星9.5客户端和量化终端;\n"
"升级过程将覆盖本地代码,升级程序会自动备份本地代码;\n"
"------请确认是否升级 (y/n) : ")
if inp == 'y' or inp == 'Y': # 确认升级
# ======================更新epolestar和equant=============================
chkEquRlt = checkEquUpdate()
if chkEquRlt:
EQUURLTAG[0] = EQUURLTAG[0] + chkEquRlt + ".0" + ".zip"
reqRlt = requestZip(EQUURLTAG[0], EQUURLTAG[1])
if reqRlt == 0: # 下载失败
print("Equant下载过程出错,请重新尝试!")
return
chkEpoRlt = checkEpoUpdate()
if chkEpoRlt:
reqRlt = requestZip(EPOURLTAG[0], EPOURLTAG[1])
if reqRlt == 0: # 下载失败
print("Epolestar下载过程出错,请重新尝试!")
return
if not any([chkEquRlt, chkEpoRlt]):
return
# ===========================解压并删除压缩包=================================
for name in zipname:
zipFile(name)
delzip(name)
# ==========================关进程============================================
killEpProcess()
if os.path.exists("..\\equant\\src\\log\\mainpid.log"):
with open("..\\equant\\src\\log\\mainpid.log", "r") as f:
pid = f.readline()
if pid:
killProcesses(int(pid))
time.sleep(2)
# ===========================备份文件==========================================
time_now = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
if chkEquRlt: # 备份equant
directory, dest = dirPath, os.path.join(workDir, time_now)
equbacRlt = backup(directory, dest)
if equbacRlt == 0: # 路径不存在
print("equant备份数据过程出错!")
return
if chkEpoRlt:
directory = os.path.abspath(os.path.join(dirPath, "..\\epolestar"))
#dest = os.path.join(workDir, time_now)
dest = os.path.abspath(os.path.join(workDir, "..\\epolestarbackup\\"+time_now))
epobacRlt = backup(directory, dest)
if epobacRlt == 0: # 路径不存在
print("equant备份数据过程出错!")
return
# ===========删除equant的代码部分,保留config, strategy, log文件夹,复制新版本=====================
if chkEquRlt and equbacRlt:
'''
for file in os.listdir(os.path.join(dirPath, "src")):
if file not in ["config", "log", "strategy"]:
path = os.path.join(dirPath, "src\\%s" % file)
if os.path.isdir(path):
delRet = delFile(path)
if delRet == 0:
print("%s 文件删除失败!" % path)
return
if os.path.isfile(path):
os.remove(path)
'''
# 合并config, strategy, log去重
# TODO:会不会存在合并出错情况
for src, dst in zip([styPath, cfgPath, logPath],
[tempPath1, tempPath2, tempPath3]):
mergeFile(src, dst)
# 将最新的代码拷贝到原目录中
for file in os.listdir(os.path.join(workDir, "equant-master")):
if file not in ["config", "log", "strategy"]:
spath = os.path.join(workDir, "equant-master\\%s" % file)
dpath = os.path.join(dirPath, "%s" % file)
if os.path.isdir(spath):
mergeFile(spath, dpath)
if os.path.isfile(spath):
shutil.copy(spath, dpath)
# os.system(f"xcopy {spath} {dpath} /y /e /i /h")
# ==================删除9.5客户端,复制新版本===============================
if chkEpoRlt and epobacRlt:
#delFile(os.path.abspath(os.path.join(dirPath, "..\\epolestar")))
src = os.path.join(workDir, "epolestar")
des = os.path.abspath(os.path.join(dirPath, "..\\epolestar"))
#os.system(f'xcopy "{src}" "{des}" /y /e /i /h /q')
cmdstr = f'xcopy "{src}" "{des}" /y /e /i /h /q'
p = subprocess.Popen(cmdstr, shell=True, stdout=subprocess.PIPE)
p.wait()
if p.returncode != 0:
print("极星客户端升级出错: %s" % ''.join([s.decode('gbk') for s in p.stdout.readlines()]))
print("更新完成!")
if os.path.exists(equDelPath):
delFile(equDelPath)
if os.path.exists(epoDelPath):
delFile(epoDelPath)
if os.path.exists(verDelPath):
os.remove(verDelPath)
if __name__ == '__main__':
# main(sys.argv[1])
main()