Skip to content

Commit 22a989b

Browse files
authored
QCmd.py, to execute command in linux platform
thanks to 墨小刀(472531607) to point out this problem.
1 parent 901b5b0 commit 22a989b

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

MySQL/scripts/qiueer/QCmd.py

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#-*- encoding:utf-8 -*-
2+
'''
3+
@author: qiueer
4+
'''
5+
import re, types
6+
import platform
7+
8+
9+
def docmd(command,timeout=300, debug=False, raw=False):
10+
'''
11+
功能:
12+
执行命令
13+
参数:command,命令以及其参数/选项
14+
timeout,命令超时时间,单位秒
15+
debug,是否debug,True输出debug信息,False不输出
16+
raw,命令输出是否为元素的输出,True是,False会将结果的每一行去除空格、换行符、制表符等,默认False
17+
返回:
18+
含有3个元素的元组,前两个元素类型是list,第三个元素类型是int,第一个list存储stdout的输出,第二个list存储stderr的输出,第三int存储命令执行的返回码,其中-1表示命令执行超时
19+
示例:
20+
cmd.docmd("ls -alt")
21+
'''
22+
import subprocess, datetime, os, time, signal
23+
start = datetime.datetime.now()
24+
25+
ps = None
26+
retcode = 0
27+
if platform.system() == "Linux":
28+
ps = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
29+
else:
30+
ps = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False)
31+
while ps.poll() is None:
32+
time.sleep(0.2)
33+
now = datetime.datetime.now()
34+
if (now - start).seconds > timeout:
35+
os.kill(ps.pid, signal.SIGINT)
36+
retcode = -1
37+
return (None,None,retcode)
38+
stdo = ps.stdout.readlines()
39+
stde = ps.stderr.readlines()
40+
41+
if not ps.returncode:
42+
retcode = ps.returncode
43+
44+
if raw == True: #去除行末换行符
45+
stdo = [line.strip("\n") for line in stdo]
46+
stde = [line.strip("\n") for line in stde]
47+
48+
if raw == False: #去除行末换行符,制表符、空格等
49+
stdo = [str.strip(line) for line in stdo]
50+
stde = [str.strip(line) for line in stde]
51+
return (stdo,stde,retcode)
52+
53+
54+
def docmd_ex(command,timeout=300, debug=False, raw=False, pure=True):
55+
'''
56+
功能:
57+
执行命令
58+
参数:command,命令以及其参数/选项
59+
timeout,命令超时时间,单位秒
60+
debug,是否debug,True输出debug信息,False不输出
61+
raw,命令输出是否为元素的输出,True是,False会将结果的每一行去除空格、换行符、制表符等,默认False
62+
返回:
63+
含有3个元素的元组,前两个元素类型是list,第三个元素类型是int,第一个list存储stdout的输出,第二个list存储stderr的输出,第三int存储命令执行的返回码,其中-1表示命令执行超时
64+
示例:
65+
cmd.docmd("ls -alt")
66+
'''
67+
import subprocess, datetime, os, time, signal
68+
start = datetime.datetime.now()
69+
(stdo,stde, retcode) = ([], [], -1)
70+
ps = None
71+
if platform.system() == "Linux":
72+
ps = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
73+
else:
74+
ps = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False)
75+
while ps.poll() is None:
76+
time.sleep(0.2)
77+
now = datetime.datetime.now()
78+
if (now - start).seconds > timeout:
79+
os.kill(ps.pid, signal.SIGINT)
80+
return None,None,-1
81+
82+
if pure == True:
83+
stdo = ps.stdout.read()
84+
stde = ps.stderr.read()
85+
elif pure == False:
86+
stdo = ps.stdout.readlines()
87+
stde = ps.stderr.readlines()
88+
89+
retcode = ps.returncode
90+
91+
if raw == True and pure == False: #去除行末换行符
92+
stdo = [line.strip("\n") for line in stdo]
93+
stde = [line.strip("\n") for line in stde]
94+
95+
if raw == False and pure == False: #去除行末换行符,制表符、空格等
96+
stdo = [str.strip(line) for line in stdo]
97+
stde = [str.strip(line) for line in stde]
98+
99+
return stdo,stde,retcode
100+
101+
def docmds(commands,timeout=300, debug=False, raw=False):
102+
'''
103+
功能:执行多个命令,每个命令之间用 , 或 ; 号分割
104+
返回:哈希,key为每个命令,key对应的value为一元组,元组值请参看docmd的说明
105+
'''
106+
cmds = re.split('[,;]+' , commands)
107+
result = {}
108+
for cmdline in cmds:
109+
(stdo,stde,retcode) = docmd(cmdline, timeout, debug=debug, raw=raw)
110+
result[cmdline] = (stdo,stde,retcode)
111+
return result
112+
113+
114+
115+

0 commit comments

Comments
 (0)