-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
267 lines (203 loc) · 7.22 KB
/
server.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
import json
import os
import sys
from datetime import datetime, date, timedelta
from threading import Timer
import time
import shutil
import pymysql
from flask import Flask
from flask import render_template, make_response
app = Flask(__name__, static_folder='', static_url_path='')
app.root_path = os.path.dirname(os.path.abspath(__file__))
HOST = "0.0.0.0"
PORT = 9094
INFO = "Test Report on " + HOST + ":" + str(PORT)
UPDATE_SHELL = "----"
HTML_RUN_SHELL = 'pytest --html="Reports/report-`date +%Y-%m-%d`.html"'
XML_RUN_SHELL = 'pytest --junitxml="Reports/report-`date +%Y-%m-%d`.xml"'
RUN_SHELL = 'pytest --html="Reports/report-`date +%Y-%m-%d`.html" --junitxml="Reports/report-`date +%Y-%m-%d`.xml"'
MYSQL_HOST = "192.168.8.13"
MYSQL_DB_NAME = "xxxxxxx.com"
MYSQL_USER_NAME = "123123123"
MYSQL_USER_PWD = "xxxxxxx.com123"
SMS_SQL = "select * from tablexxx order by id desc limit 25"
class CJsonEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime):
return obj.strftime('%Y-%m-%d %H:%M:%S')
elif isinstance(obj, date):
return obj.strftime("%Y-%m-%d")
else:
return json.JSONEncoder.default(self, obj)
# 经过测试自更新重启是有效的,python编译过程慢一个过程
# 要重启2次,注: linux/max下重启是替换进程本身,pid不变.
# 不支持idea调试和参数太多的情况.
# 支持nohub python server.py
def restart_program():
python = sys.executable
os.execl(python, python, *sys.argv)
@app.route("/")
def home():
return render_template("index.html")
CACHE = {
"time": datetime.now(),
"reports": [],
"suites": []
}
def get_report_file_pix():
return "Reports/report-" + date.today().strftime("%Y-%m-%d")
def get_report_file_full():
return "Reports/report-" + datetime.now().strftime("%Y%m%d%H%M%S")
def run_pytest_by_type(shell=XML_RUN_SHELL):
import subprocess
out = ""
process = subprocess.Popen(shell, stdout=subprocess.PIPE, shell=True)
for line in iter(process.stdout.readline, ''):
if line == b'':
break
out += str(line)
return out
def run_pytest_as_xml():
"""
获取测试报告列表,生成xml格式的报告
"""
out = run_pytest_by_type(XML_RUN_SHELL)
shutil.copy(get_report_file_pix() + ".xml", get_report_file_full() + ".xml")
return out
def run_pytest_as_html():
"""
获取测试报告列表,生成html格式的报告 pytest --html="Reports/report-`date +%Y-%m-%d`.html"
"""
out = run_pytest_by_type(HTML_RUN_SHELL)
shutil.copy(get_report_file_pix() + ".html", get_report_file_full() + ".html")
return out
def run_pytest_by_file(file):
out = run_pytest_by_type("pytest " + file)
return out
def run_pytest():
"""
获取测试报告列表,生成多份报告.
"""
out = run_pytest_by_type(RUN_SHELL)
shutil.copy(get_report_file_pix() + ".html", get_report_file_full() + ".html")
shutil.copy(get_report_file_pix() + ".xml", get_report_file_full() + ".xml")
return out
def auto_pull_git():
os.system(UPDATE_SHELL)
return True
def walk_reports():
if CACHE['reports']:
return CACHE['reports']
cpath = os.path.dirname(os.path.abspath(__file__)) + "/Reports"
dir = os.scandir(cpath)
file_list = []
for root, dirs, files in dir:
for filepath in files:
new_path = root.replace(cpath, "")
file_list.append("" + os.path.join(new_path, filepath))
# file_list = os.listdir(os.path.dirname(os.path.abspath(__file__)) + "/Reports")
response = make_response(json.dumps(file_list))
response.headers["Content-type"] = "application/json"
return response
def walk_suites():
if CACHE['reports']:
return CACHE['reports']
cpath = os.path.dirname(os.path.abspath(__file__)) + "/Suites"
dir = os.scandir(cpath)
file_list = []
for root, dirs, files in dir:
for filepath in files:
new_path = root.replace(cpath, "")
file_list.append("Suites/" + os.path.join(new_path, filepath))
# file_list = os.listdir(os.path.dirname(os.path.abspath(__file__)) + "/Reports")
response = make_response(json.dumps(file_list))
response.headers["Content-type"] = "application/json"
return response
@app.route('/report/list')
def report_list(name=None):
# 应该使用walk()方法
# https://www.python.org/dev/peps/pep-0471/
if CACHE['reports']:
return CACHE['reports']
cpath = os.path.dirname(os.path.abspath(__file__)) + "/Reports"
dir = os.scandir(cpath)
file_list = []
for obj in dir:
if not obj.is_dir():
file_list.append(obj.name)
response = make_response(json.dumps(file_list))
response.headers["Content-type"] = "application/json"
return response
@app.route('/suites/list')
def suite_list(name=None):
"""
获取文件列表按路径直接取出来
"""
time.sleep(0.1)
cpath = os.path.dirname(os.path.abspath(__file__)) + "/Suites"
dir = os.walk(cpath)
file_list = []
for root, dirs, files in dir:
for filepath in files:
new_path = root.replace(cpath, "")
file_list.append("Suites/" + os.path.join(new_path, filepath))
file_list_new = filter(filter_file_type, file_list)
filter_list = [item for item in file_list_new]
return make_response(json.dumps(filter_list))
@app.route('/server/stop')
def server_stop():
restart_program()
raise RuntimeError('Server will stop.')
@app.route('/server/update')
def server_update():
auto_pull_git()
restart_program()
@app.route('/server/trun')
def run_all_test_case():
return make_response(run_pytest())
@app.route('/server/runfile')
def run_test_by_file():
from flask import request
args = request.args
file = args['file']
return make_response(run_pytest_by_file(file))
@app.route('/server/trunhtml')
def run_all_test_case_html():
return make_response(run_pytest_as_html())
@app.route('/sms/list')
def get_sms_last_list():
db = pymysql.connect(MYSQL_HOST, MYSQL_USER_NAME, MYSQL_USER_PWD, MYSQL_DB_NAME,charset="utf8")
cursor = db.cursor(pymysql.cursors.DictCursor)
cursor.execute(SMS_SQL)
map_data = cursor.fetchall()
db.close()
return make_response(json.dumps(map_data, cls=CJsonEncoder))
@app.route('/server/treport')
def get_today_test_report():
"""
获取当天的测试报告,如果没有就运行一次.
获取的报告为xml格式的.
:return:
"""
file_name = "Reports/report-" + date.today().strftime("%Y-%m-%d") + ".xml"
run_pytest()
datas = open(file_name).read()
return make_response(datas)
def filter_file_type(name):
"""
过滤文件类型,有些类型的文件不需要显示.
"""
if name.endswith(".py") and not name.endswith("__init__.py"):
# or name.endswith(".png") or name.endswith(".md"):
return True
else:
return False
# 开启服务 wsgi标准
# 指定端口 pywsgi.WSGIServer((HOST, PORT), app, log=None).serve_forever()
if __name__ == '__main__':
print(INFO)
app.config['TEMPLATES_AUTO_RELOAD'] = True
# app.config['DEBUG'] = True
app.run(port=PORT, threaded=True, host=HOST)
# flask 如果不使用多线程,会同步阻塞,(chrome快速请求时出现!)