forked from hhyo/Archery
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.py
More file actions
65 lines (58 loc) · 2.07 KB
/
Copy pathplugin.py
File metadata and controls
65 lines (58 loc) · 2.07 KB
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
# -*- coding: UTF-8 -*-
"""
@author: hhyo
@license: Apache Licence
@file: plugin.py
@time: 2019/03/04
"""
__author__ = 'hhyo'
import logging
import subprocess
import traceback
logger = logging.getLogger('default')
class Plugin:
def __init__(self, path):
self.path = path
self.required_args = [] # 必须参数
self.disable_args = [] # 禁用参数
def check_args(self, args):
"""
检查请求参数列表
:return: {'status': 0, 'msg': 'ok', 'data': {}}
"""
args_check_result = {'status': 0, 'msg': 'ok', 'data': {}}
# 检查路径
if self.path is None:
return {'status': 1, 'msg': '可执行文件路径不能为空!', 'data': {}}
# 检查禁用参数
for arg in args.keys():
if arg in self.disable_args:
return {'status': 1, 'msg': '{arg}参数已被禁用'.format(arg=arg), 'data': {}}
# 检查必须参数
for req_arg in self.required_args:
if req_arg not in args.keys():
return {'status': 1, 'msg': '必须指定{arg}参数'.format(arg=req_arg), 'data': {}}
elif args[req_arg] is None or args[req_arg] == '':
return {'status': 1, 'msg': '{arg}参数值不能为空'.format(arg=req_arg), 'data': {}}
return args_check_result
def generate_args2cmd(self, args, shell):
"""
将请求参数转换为命令行参数
:return:
"""
@staticmethod
def execute_cmd(cmd_args, shell):
"""
执行命令并且返回process
:return:
"""
try:
p = subprocess.Popen(cmd_args,
shell=shell,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True)
return p
except Exception as e:
logger.error("命令执行失败\n{}".format(traceback.format_exc()))
raise RuntimeError('命令执行失败,失败原因:%s' % str(e))