mdserver-web/class/core/plugin_api.py

165 lines
5.3 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-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()
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):
pass
# 构造本地插件信息
def getPluginInfo(self, info):
2018-11-28 06:11:55 -05:00
checks = ''
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
pluginInfo = {
"id": 10000,
"pid": info['pid'],
"type": 1000,
"name": info['name'],
"title": info['title'],
"ps": info['ps'],
"dependnet": "",
"mutex": "",
"install_checks": checks,
"uninsatll_checks": checks,
"versions": info['versions'],
# "updates": info['updates'],
"setup": False,
"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-21 05:18:17 -05:00
pluginInfo['setup'] = os.path.exists(pluginInfo['install_checks'])
pluginInfo['status'] = os.path.exists(pluginInfo['install_checks'])
return pluginInfo
2018-11-28 06:11:55 -05:00
def getAllList(self, sType='0'):
2018-11-21 05:18:17 -05:00
ret = {}
ret['type'] = json.loads(public.readFile(self.__type))
plugins_info = []
2018-11-28 06:11:55 -05:00
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
2018-11-28 06:11:55 -05:00
2018-11-21 05:18:17 -05:00
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))
if type(data['versions']) == list and data['name'] == 'php':
for index in range(len(data['versions'])):
tmp = data.copy()
2018-11-28 10:56:10 -05:00
tmp['title'] = tmp['title'] + \
'-' + data['versions'][index]
2018-11-28 06:11:55 -05:00
tmp['versions'] = data['versions'][index]
2018-11-21 05:18:17 -05:00
pg = self.getPluginInfo(tmp)
2018-11-28 06:11:55 -05:00
if sType == '0':
2018-11-21 05:18:17 -05:00
plugins_info.append(pg)
else:
if pg['pid'] == sType:
plugins_info.append(pg)
else:
2018-11-28 06:11:55 -05:00
pg = self.getPluginInfo(data)
if sType == '0':
2018-11-21 05:18:17 -05:00
plugins_info.append(pg)
else:
if pg['pid'] == sType:
plugins_info.append(pg)
2018-11-25 10:30:19 -05:00
except Exception, e:
2018-11-21 05:18:17 -05:00
print e
2018-11-28 06:11:55 -05:00
# pass
return plugins_info
def getPluginList(self, sType, sPage=1, sPageSize=15):
ret = {}
ret['type'] = json.loads(public.readFile(self.__type))
plugins_info = self.getAllList(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
def addIndex(self, name, version):
pass
def run(self):
pass