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
#!/usr/bin/env python
# -*- coding: utf_8 -*-
import os
import re
import shutil
import copy
import time
import types
import json
import logging
import traceback
from urllib import parse
from datetime import datetime
from pony.orm import *
from app import signalManager, config
from model import fullStackDB
from model.annex import Annex
from model.apps import Apps
from model.user import User
from model.app_logs import AppLogs
from model.build_logs import BuildLogs
from utils import sql_filter, ThreadMaker
from utils.epk import EpkApp
logger = logging.getLogger("AppsManager")
@ThreadMaker
def build_application(user, uuid):
signalManager.actionAddBuildLog.emit(user, uuid, isMove=False)
class AppsManager(object):
def __init__(self):
super(AppsManager, self).__init__()
def add(self, user, data):
with db_session:
editor = User.get(id=user)
if not editor:
return False, "current user is not exists"
# result = Apps.select(app_name=data.get("app_name"), is_delete=False).count()
# if result < 1:
# return False, "app_name has been exists."
data.update({
'app_icon': data["app_icon"].replace(config.get("UPLOAD_PATH"), ""),
'create_by': editor,
'create_at': datetime.now(),
'update_by': editor,
'update_at': datetime.now(),
})
app_files = []
epk_path = ""
if data.get("fileList"):
app_files = data.get("fileList")
data.pop("fileList")
epk_path = data.get("epk_path")
data.pop("epk_path")
app = Apps(**data)
commit()
# 在EPK目录下生成JSON文件
with open(os.sep.join([os.path.dirname(epk_path), "epk.json"]), "w") as f:
json.dump(app.to_dict(exclude=["uuid", "create_at", "update_at", "delete_at"]), f)
for a in app_files:
Annex(app=app, title=os.path.basename(a), path=a, size=os.path.getsize(a), create_by=editor, create_at=datetime.now(), update_by=editor, update_at=datetime.now())
flush()
commit()
app_info = {}
params = { 'appName': app.app_name, 'appDir': epk_path, 'appVersion': app.app_version, 'output': os.path.dirname(epk_path) }
if editor.role == "administrator" or editor.role == "community":
params['algorithm'] ="h"
epk = EpkApp(**params)
app_info = epk.pack()
epk_filename = os.sep.join([os.path.dirname(epk_path).replace(config.get("UPLOAD_PATH"), ""), "{}.epk".format(app.app_name)]).replace('\\', '/')
if app_info:
app_info['md5'] = str(app_info['md5'])
result = BuildLogs(app=app, app_path=epk_filename, app_info=app_info, create_by=editor, create_at=datetime.now(), update_by=editor, update_at=datetime.now())
commit()
AppLogs(app_name=app.app_name, app_path=epk_filename, app_version=data.get("app_version"), app_info=app_info, create_by=editor, create_at=datetime.now())
commit()
return result, "add app {}".format("success" if result else "fail")
def delete(self, user, uuid):
with db_session:
editor = User.get(id=user)
if not editor:
return False, "current user is not exists"
result = Apps.get(uuid=uuid)
if result:
result.delete()
return result, "delete app {}.".format("success" if result else "fail")
def get(self, user, data):
if not data.get("uuid"):
return False, "app uuid can not be null"
with db_session:
editor = User.get(id=user)
if not editor:
return False, "current user is not exists"
# 根据app查询应用,获取应用有哪些文件
# 按格式创建文件夹,将这些文件移动到这个文件夹
# 将这些零散文件进行打包
# 更新数据库对应文件的路径
app = Apps.get(uuid=data.get("uuid"))
if not app:
return None, "app not found"
source_files = Annex.select().filter(app=app)
if not source_files:
return None, "apps file not found"
dtNowString = datetime.now().strftime("%Y%m%d%H%M%S")
dirname = "{}-{}-{}-{}".format(app.app_name, app.app_version, app.category, dtNowString)
upload_dir = os.sep.join([config.get("UPLOAD_PATH"), config.get("EPK_DIR")])
target_dir = os.sep.join([upload_dir, dirname])
dest_dir = os.sep.join([target_dir, "src"])
if not os.path.exists(dest_dir):
os.makedirs(dest_dir)
app_files = []
for sf in source_files:
target_file = sf.path
if not os.path.exists(sf.path):
target_file = os.sep.join([config.get("UPLOAD_PATH"), sf.path])
filename = os.path.basename(target_file)
name, suffix = os.path.splitext(filename)
name = re.sub(r"_\d{14}$", "", name)
dst_file = os.path.normpath(os.sep.join([dest_dir, name + suffix]))
shutil.copy(os.path.normpath(target_file), dst_file)
app_files.append([sf.id, dst_file])
with open(os.sep.join([target_dir, "epk.json"]), "w") as f:
json.dump(app.to_dict(exclude=["uuid", "create_at", "update_at", "delete_at"]), f)
# 打包成EPK文件
app_info = {}
params = { 'appName': app.app_name, 'appDir': dest_dir, 'appVersion': app.app_version, 'output': target_dir }
if editor.role == "administrator" or editor.role == "community":
params['algorithm'] = "h"
epk = EpkApp(**params)
app_info = epk.pack()
app_info['md5'] = str(app_info['md5'])
# 更新数据库对应文件路径
# 将文件拷贝过去后,需要重新更新数据库文件记录
epk_path = os.sep.join([target_dir.replace(config.get("UPLOAD_PATH"), ""), "{}.epk".format(app.app_name)]).replace('\\', '/')
build = BuildLogs.get(app=app)
if build:
build.set(app_path=epk_path, app_info=app_info, update_by=editor, update_at=datetime.now())
commit()
# 新增一条AppLogs
AppLogs(app_name=app.app_name, app_path=epk_path, app_version=app.app_version, app_info=app_info, create_by=editor, create_at=datetime.now())
commit()
return { 'app_name': app.app_name, 'app_path': epk_path }, "rebuild app {}.".format("success" if app_info else "fail")
def getList(self, user, data):
if not data or len(data) <= 0:
return False, 0, "parameters can not be null."
temp = copy.deepcopy(data)
if 'pagenum' in temp:
temp.pop('pagenum')
if 'pagesize' in temp:
temp.pop('pagesize')
if 'scope_type' in temp:
temp.pop('scope_type')
with db_session:
editor = User.get(id=user)
if not editor:
return False, "current user is not exists"
if editor.role == "administrator":
temp.update({"is_delete": False})
else:
temp.update({ "create_by": editor, "is_delete": False })
if "scope_type" in data and data.get("scope_type") == "list":
result = Apps.select().where(**temp).order_by(desc(Apps.create_at))
temp = []
for item in result:
temp.append(item.to_dict(only=["uuid", "app_name"]))
return temp, len(temp), "get app {}.".format("success" if temp else "fail")
result = Apps.select().where(**temp).order_by(Apps.sort).sort_by(desc(Apps.create_at)).page(data.get("pagenum", 1), pagesize=data.get("pagesize", 10))
count = Apps.select().where(**temp).count()
if result and len(result):
temp = []
for item in result:
t = item.to_dict(with_collections=True, related_objects=True, exclude=["app_annex", "app_download", "is_delete", "delete_by", "delete_at"])
t.update({
"app_build_log": item.app_build_log.to_dict(exclude=["is_delete", "delete_by", "delete_at", "create_by", "update_by"]) if item.app_build_log else None,
"create_by": item.create_by.to_dict(only=["uuid", "username"]),
"update_by": item.update_by.to_dict(only=["uuid", "username"]),
"create_at": item.create_at.strftime("%Y-%m-%d %H:%M:%S") if item.create_at else None,
"update_at": item.update_at.strftime("%Y-%m-%d %H:%M:%S") if item.update_at else None,
})
temp.append(t)
result = temp
return result, count, "get app {}.".format("success" if result else "no data")
def update(self, user, uuid, data):
# 当参数为空时,直接返回错误
if len(data) <= 0 or (len(data.keys()) == 1 and "id" in data):
return False, "app can not be null."
with db_session:
editor = User.get(id=user)
if not editor:
return False, "current user is not exists"
result = Apps.get(uuid=uuid)
if not result:
return False, "app not found"
if data.get("app_files"):
app_files = data.get("app_files")
for a in app_files:
Annex(app=result, title=a.get("filename"), path=a.get("filepath"), size=a.get("filesize"), create_by=editor, create_at=datetime.now(), update_by=editor, update_at=datetime.now())
flush()
commit()
data.pop("app_files")
if data.get("app_icon"):
condition = {
'update_by': editor,
'update_at': datetime.now()
}
if data.get("app_icon").get("filename"):
condition.update({"title": data.get("app_icon").get("filename")})
if data.get("app_icon").get("filepath"):
condition.update({"path": data.get("app_icon").get("filepath")})
if data.get("app_icon").get("filesize"):
condition.update({"size": data.get("app_icon").get("filesize")})
result.app_icon.set(**condition)
commit()
data.pop("app_icon")
result.set(update_at=datetime.now(), update_by=editor, **data)
commit()
build_application(user, str(result.uuid))
return result, "update app {}.".format("success" if result else "fail")
def build(self, files, data):
with db_session:
user = User.get(uuid=data['access_key'])
if not user:
return False, "user does not exists"
if data.get("access_key"): data.pop("access_key")
data.update({
'create_by': user,
'create_at': datetime.now(),
'update_by': user,
'update_at': datetime.now(),
})
app = Apps(**data)
commit()
dir_format = "{}-{}-{}".format(app.app_name, app.app_version, datetime.now().strftime("%Y%m%d%H%M%S"))
upload_dir = os.sep.join([config.get("UPLOAD_PATH"), "evueapps"])
target_dir = os.sep.join([upload_dir, user.account, dir_format])
dest_dir = os.sep.join([target_dir, "src"])
if not os.path.exists(dest_dir):
os.makedirs(dest_dir)
for target_file in files:
filename = os.path.basename(target_file)
name, suffix = os.path.splitext(filename)
name = re.sub(r"_\d{14}$", "", name)
dst_file = os.path.normpath(os.sep.join([dest_dir, name + suffix]))
shutil.copy(os.path.normpath(target_file), dst_file)
Annex(app=app, title=filename, path=dst_file.replace(config.get("UPLOAD_PATH"), ""), size=os.path.getsize(dst_file), create_by=user, create_at=datetime.now(), update_by=user, update_at=datetime.now())
flush()
commit()
# 打包成EPK文件
app_info = {}
params = { 'appName': app.app_name, 'appDir': dest_dir, 'appVersion': app.app_version, 'output': target_dir }
if user.role == "administrator" or user.role == "community":
params['algorithm'] = "h"
epk = EpkApp(**params)
app_info = epk.pack()
app_info['md5'] = str(app_info['md5'])
# 更新数据库对应文件路径
# 将文件拷贝过去后,需要重新更新数据库文件记录
epk_path = os.sep.join([target_dir.replace(config.get("UPLOAD_PATH"), ""), "{}.epk".format(app.app_name)]).replace('\\', '/')
build = BuildLogs.get(app=app)
if build:
build.set(app_path=epk_path, app_info=app_info, update_by=user, update_at=datetime.now())
commit()
else:
BuildLogs(app=app, app_path=epk_path, app_info=app_info, create_by=user, create_at=datetime.now(), update_by=user, update_at=datetime.now())
commit()
# 新增一条AppLogs
AppLogs(app_name=app.app_name, app_path=epk_path, app_version=app.app_version, app_info=app_info, create_by=user, create_at=datetime.now())
commit()
with open(os.sep.join([target_dir, "epk.json"]), "w") as f:
json.dump(app.to_dict(exclude=["uuid", "create_at", "update_at", "delete_at"]), f)
return { 'app_name': app.app_name, 'app_file': "{}.epk".format(app.app_name), 'app_url': parse.urljoin(config['UPLOAD_SERVER'], epk_path) }, "application build {}.".format("success" if app_info else "fail")
appsManager = AppsManager()