mdserver-web/class/core/plugin_api.py

500 lines
17 KiB
Python
Raw Normal View History

2018-11-21 05:18:17 -05:00
# coding: utf-8
import psutil
import time
import os
import public
import re
import json
2018-12-06 02:41:57 -05:00
import threading
import multiprocessing
class pa_thread(threading.Thread):
def __init__(self, func, args, name=''):
threading.Thread.__init__(self)
self.name = name
self.func = func
self.args = args
self.result = self.func(*self.args)
def getResult(self):
try:
return self.result
except Exception:
return None
2018-11-21 05:18:17 -05:00
2018-11-27 10:29:28 -05:00
class plugin_api:
2018-11-21 05:18:17 -05:00
__tasks = None
2018-11-28 06:11:55 -05:00
__plugin_dir = 'plugins'
__type = 'data/json/type.json'
2018-11-27 10:29:28 -05:00
__index = 'data/json/index.json'
2018-11-21 05:18:17 -05:00
setupPath = None
def __init__(self):
self.setupPath = 'server'
2018-11-28 06:11:55 -05:00
# self.__plugin_dir = public.getRunDir() + '/plugins'
2018-11-21 05:18:17 -05:00
# 进程是否存在
def processExists(self, pname, exe=None):
try:
if not self.pids:
self.pids = psutil.pids()
for pid in self.pids:
try:
p = psutil.Process(pid)
if p.name() == pname:
if not exe:
return True
else:
if p.exe() == exe:
return True
except:
pass
return False
except:
return True
# 检查是否正在安装
def checkSetupTask(self, sName):
if not self.__tasks:
self.__tasks = public.M('tasks').where(
"status!=?", ('1',)).field('status,name').select()
2018-12-01 04:20:10 -05:00
2018-11-21 05:18:17 -05:00
if sName.find('php-') != -1:
tmp = sName.split('-')
sName = tmp[0]
version = tmp[1]
isTask = '1'
for task in self.__tasks:
tmpt = public.getStrBetween('[', ']', task['name'])
if not tmpt:
continue
tmp1 = tmpt.split('-')
name1 = tmp1[0].lower()
if sName == 'php':
if name1 == sName and tmp1[1] == version:
isTask = task['status']
else:
if name1 == 'pure':
name1 = 'pure-ftpd'
if name1 == sName:
isTask = task['status']
return isTask
def checkStatus(self, info):
2018-12-05 07:15:00 -05:00
2018-12-06 02:41:57 -05:00
if not info['setup']:
return False
2018-12-05 07:15:00 -05:00
data = self.run(info['name'], 'status', info['setup_version'])
if data[0] == 'start':
return True
return False
2018-11-21 05:18:17 -05:00
2018-12-06 02:41:57 -05:00
def checkStatusProcess(self, info, i, return_dict):
if not info['setup']:
return_dict[i] = False
return
data = self.run(info['name'], 'status', info['setup_version'])
if data[0] == 'start':
return_dict[i] = True
else:
return_dict[i] = False
2018-11-29 05:19:18 -05:00
def checkDisplayIndex(self, name, version):
if not os.path.exists(self.__index):
public.writeFile(self.__index, '[]')
indexList = json.loads(public.readFile(self.__index))
if type(version) == list:
for index in range(len(version)):
vname = name + '-' + version[index]
if vname in indexList:
return True
else:
vname = name + '-' + version
if vname in indexList:
return True
return False
def getVersion(self, path):
2018-11-30 03:58:50 -05:00
version_f = path + '/version.pl'
if os.path.exists(version_f):
return public.readFile(version_f).strip()
return ''
2018-11-29 05:19:18 -05:00
2018-11-21 05:18:17 -05:00
# 构造本地插件信息
def getPluginInfo(self, info):
2018-11-28 06:11:55 -05:00
checks = ''
2018-11-29 05:19:18 -05:00
path = ''
coexist = False
2018-11-28 06:11:55 -05:00
if info["checks"][0:1] == '/':
2018-11-21 05:18:17 -05:00
checks = info["checks"]
else:
2018-11-28 06:11:55 -05:00
checks = public.getRootDir() + '/' + info['checks']
2018-11-21 05:18:17 -05:00
2018-11-29 05:19:18 -05:00
if info.has_key('path'):
path = info['path']
if path[0:1] != '/':
path = public.getRootDir() + '/' + path
if info.has_key('coexist') and info['coexist']:
coexist = True
2018-11-21 05:18:17 -05:00
pluginInfo = {
"id": 10000,
"pid": info['pid'],
"type": 1000,
"name": info['name'],
"title": info['title'],
"ps": info['ps'],
"dependnet": "",
"mutex": "",
2018-11-29 05:19:18 -05:00
"path": path,
2018-11-21 05:18:17 -05:00
"install_checks": checks,
"uninsatll_checks": checks,
2018-11-29 05:19:18 -05:00
"coexist": coexist,
2018-11-21 05:18:17 -05:00
"versions": info['versions'],
# "updates": info['updates'],
2018-11-29 05:19:18 -05:00
"display": False,
2018-11-21 05:18:17 -05:00
"setup": False,
2018-11-29 05:19:18 -05:00
"setup_version": "",
2018-11-21 05:18:17 -05:00
"status": False,
}
pluginInfo['task'] = self.checkSetupTask(pluginInfo['name'])
2018-11-28 06:11:55 -05:00
if checks.find('VERSION') > -1:
pluginInfo['install_checks'] = checks.replace(
'VERSION', info['versions'])
2018-11-29 05:19:18 -05:00
if path.find('VERSION') > -1:
pluginInfo['path'] = path.replace(
'VERSION', info['versions'])
pluginInfo['display'] = self.checkDisplayIndex(
info['name'], pluginInfo['versions'])
2018-11-21 05:18:17 -05:00
pluginInfo['setup'] = os.path.exists(pluginInfo['install_checks'])
2018-11-30 03:58:50 -05:00
if coexist and pluginInfo['setup']:
pluginInfo['setup_version'] = info['versions']
else:
pluginInfo['setup_version'] = self.getVersion(
2018-12-05 01:00:31 -05:00
pluginInfo['install_checks'])
2018-12-06 02:41:57 -05:00
# pluginInfo['status'] = self.checkStatus(pluginInfo)
2018-11-21 05:18:17 -05:00
return pluginInfo
2018-11-29 05:19:18 -05:00
def makeCoexist(self, data):
plugins_info = []
for index in range(len(data['versions'])):
tmp = data.copy()
tmp['title'] = tmp['title'] + \
'-' + data['versions'][index]
tmp['versions'] = data['versions'][index]
pg = self.getPluginInfo(tmp)
plugins_info.append(pg)
return plugins_info
2018-11-29 13:03:05 -05:00
def makeList(self, data, sType='0'):
2018-11-29 05:19:18 -05:00
plugins_info = []
if (data['pid'] == sType):
if type(data['versions']) == list and data.has_key('coexist') and data['coexist']:
tmp_data = self.makeCoexist(data)
for index in range(len(tmp_data)):
plugins_info.append(tmp_data[index])
else:
pg = self.getPluginInfo(data)
plugins_info.append(pg)
return plugins_info
if sType == '0':
if type(data['versions']) == list and data.has_key('coexist') and data['coexist']:
tmp_data = self.makeCoexist(data)
for index in range(len(tmp_data)):
plugins_info.append(tmp_data[index])
else:
pg = self.getPluginInfo(data)
plugins_info.append(pg)
2018-12-01 04:20:10 -05:00
# print plugins_info, data
2018-11-29 05:19:18 -05:00
return plugins_info
2018-11-28 06:11:55 -05:00
def getAllList(self, sType='0'):
2018-11-21 05:18:17 -05:00
plugins_info = []
2018-12-06 02:41:57 -05:00
for dirinfo in os.listdir(self.__plugin_dir):
if dirinfo[0:1] == '.':
continue
path = self.__plugin_dir + '/' + dirinfo
if os.path.isdir(path):
json_file = path + '/info.json'
if os.path.exists(json_file):
try:
data = json.loads(public.readFile(json_file))
tmp_data = self.makeList(data, sType)
for index in range(len(tmp_data)):
plugins_info.append(tmp_data[index])
except Exception, e:
print e
return plugins_info
2018-11-28 06:11:55 -05:00
2018-12-06 02:41:57 -05:00
def getAllListPage(self, sType='0', page=1, pageSize=10):
plugins_info = []
2018-11-21 05:18:17 -05:00
for dirinfo in os.listdir(self.__plugin_dir):
2018-11-28 06:11:55 -05:00
if dirinfo[0:1] == '.':
continue
2018-11-21 05:18:17 -05:00
path = self.__plugin_dir + '/' + dirinfo
if os.path.isdir(path):
2018-11-28 06:11:55 -05:00
json_file = path + '/info.json'
if os.path.exists(json_file):
2018-11-21 05:18:17 -05:00
try:
2018-11-28 06:11:55 -05:00
data = json.loads(public.readFile(json_file))
2018-11-29 05:19:18 -05:00
tmp_data = self.makeList(data, sType)
for index in range(len(tmp_data)):
plugins_info.append(tmp_data[index])
2018-11-25 10:30:19 -05:00
except Exception, e:
2018-11-30 03:58:50 -05:00
print e
2018-12-06 02:41:57 -05:00
manager = multiprocessing.Manager()
return_dict = manager.dict()
jobs = []
ntmp_list = range(len(plugins_info))
for i in ntmp_list:
p = multiprocessing.Process(
target=self.checkStatusProcess, args=(plugins_info[i], i, return_dict))
jobs.append(p)
p.start()
for proc in jobs:
proc.join()
returnData = return_dict.values()
for i in ntmp_list:
plugins_info[i]['status'] = returnData[i]
return plugins_info
def makeListThread(self, data, sType='0'):
plugins_info = []
if (data['pid'] == sType):
if type(data['versions']) == list and data.has_key('coexist') and data['coexist']:
tmp_data = self.makeCoexist(data)
for index in range(len(tmp_data)):
plugins_info.append(tmp_data[index])
else:
pg = self.getPluginInfo(data)
plugins_info.append(pg)
return plugins_info
if sType == '0':
if type(data['versions']) == list and data.has_key('coexist') and data['coexist']:
tmp_data = self.makeCoexist(data)
for index in range(len(tmp_data)):
plugins_info.append(tmp_data[index])
else:
pg = self.getPluginInfo(data)
plugins_info.append(pg)
# print plugins_info, data
return plugins_info
def getAllListThread(self, sType='0'):
plugins_info = []
tmp_list = []
threads = []
for dirinfo in os.listdir(self.__plugin_dir):
if dirinfo[0:1] == '.':
continue
path = self.__plugin_dir + '/' + dirinfo
if os.path.isdir(path):
json_file = path + '/info.json'
if os.path.exists(json_file):
data = json.loads(public.readFile(json_file))
if sType == '0':
tmp_list.append(data)
if (data['pid'] == sType):
tmp_list.append(data)
ntmp_list = range(len(tmp_list))
for i in ntmp_list:
t = pa_thread(self.makeListThread, (tmp_list[i], sType))
threads.append(t)
for i in ntmp_list:
threads[i].start()
for i in ntmp_list:
threads[i].join()
for i in ntmp_list:
t = threads[i].getResult()
for index in range(len(t)):
plugins_info.append(t[index])
return plugins_info
def makeListProcess(self, data, sType, i, return_dict):
plugins_info = []
if (data['pid'] == sType):
if type(data['versions']) == list and data.has_key('coexist') and data['coexist']:
tmp_data = self.makeCoexist(data)
for index in range(len(tmp_data)):
plugins_info.append(tmp_data[index])
else:
pg = self.getPluginInfo(data)
plugins_info.append(pg)
# return plugins_info
if sType == '0':
if type(data['versions']) == list and data.has_key('coexist') and data['coexist']:
tmp_data = self.makeCoexist(data)
for index in range(len(tmp_data)):
plugins_info.append(tmp_data[index])
else:
pg = self.getPluginInfo(data)
plugins_info.append(pg)
return_dict[i] = plugins_info
# return plugins_info
def getAllListProcess(self, sType='0'):
plugins_info = []
tmp_list = []
manager = multiprocessing.Manager()
return_dict = manager.dict()
jobs = []
for dirinfo in os.listdir(self.__plugin_dir):
if dirinfo[0:1] == '.':
continue
path = self.__plugin_dir + '/' + dirinfo
if os.path.isdir(path):
json_file = path + '/info.json'
if os.path.exists(json_file):
data = json.loads(public.readFile(json_file))
if sType == '0':
tmp_list.append(data)
if (data['pid'] == sType):
tmp_list.append(data)
ntmp_list = range(len(tmp_list))
for i in ntmp_list:
p = multiprocessing.Process(
target=self.makeListProcess, args=(tmp_list[i], sType, i, return_dict))
jobs.append(p)
p.start()
for proc in jobs:
proc.join()
returnData = return_dict.values()
for i in ntmp_list:
for index in range(len(returnData[i])):
plugins_info.append(returnData[i][index])
2018-11-28 06:11:55 -05:00
return plugins_info
def getPluginList(self, sType, sPage=1, sPageSize=15):
ret = {}
ret['type'] = json.loads(public.readFile(self.__type))
2018-12-06 02:41:57 -05:00
# plugins_info = self.getAllListThread(sType)
# plugins_info = self.getAllListProcess(sType)
plugins_info = self.getAllListPage(sType)
2018-11-21 05:18:17 -05:00
args = {}
args['count'] = len(plugins_info)
args['p1'] = sPage
ret['data'] = plugins_info
ret['list'] = public.getPage(args)
return ret
2018-11-28 06:11:55 -05:00
2018-11-29 06:16:12 -05:00
def getIndexList(self):
if not os.path.exists(self.__index):
public.writeFile(self.__index, '[]')
indexList = json.loads(public.readFile(self.__index))
2018-11-29 13:03:05 -05:00
plist = []
app = []
for i in indexList:
info = i.split('-')
if not info[0] in app:
app.append(info[0])
path = self.__plugin_dir + '/' + info[0]
if os.path.isdir(path):
json_file = path + '/info.json'
if os.path.exists(json_file):
try:
data = json.loads(public.readFile(json_file))
tmp_data = self.makeList(data)
for index in range(len(tmp_data)):
if tmp_data[index]['versions'] == info[1] or info[1] in tmp_data[index]['versions']:
tmp_data[index]['display'] = True
plist.append(tmp_data[index])
continue
except Exception, e:
2018-11-30 03:58:50 -05:00
print e
2018-11-29 13:03:05 -05:00
return plist
2018-11-29 06:16:12 -05:00
2018-11-30 03:58:50 -05:00
def setIndexSort(self, sort):
data = sort.split('|')
public.writeFile(self.__index, json.dumps(data))
return True
2018-11-28 06:11:55 -05:00
def addIndex(self, name, version):
2018-11-29 05:19:18 -05:00
if not os.path.exists(self.__index):
public.writeFile(self.__index, '[]')
indexList = json.loads(public.readFile(self.__index))
vname = name + '-' + version
if vname in indexList:
return public.returnJson(False, '请不要重复添加!')
if len(indexList) >= 12:
return public.returnJson(False, '首页最多只能显示12个软件!')
indexList.append(vname)
public.writeFile(self.__index, json.dumps(indexList))
return public.returnJson(True, '添加成功!')
def removeIndex(self, name, version):
if not os.path.exists(self.__index):
public.writeFile(self.__index, '[]')
indexList = json.loads(public.readFile(self.__index))
vname = name + '-' + version
if not vname in indexList:
return public.returnJson(True, '删除成功!')
indexList.remove(vname)
public.writeFile(self.__index, json.dumps(indexList))
return public.returnJson(True, '删除成功!')
2018-11-28 06:11:55 -05:00
2018-12-05 07:15:00 -05:00
def run(self, name, func, version, args='', script='index'):
path = public.getRunDir() + '/' + self.__plugin_dir + \
'/' + name + '/' + script + '.py'
py = 'python ' + path
if args == '':
py_cmd = py + ' ' + func + ' ' + version
else:
py_cmd = py + ' ' + func + ' ' + version + ' ' + args
# print path
# print os.path.exists(path)
if not os.path.exists(path):
return ('', '')
data = public.execShell(py_cmd)
return (data[0].strip(), data[1].strip())