first commit
This commit is contained in:
34
app/util/shell_execute.py
Executable file
34
app/util/shell_execute.py
Executable file
@@ -0,0 +1,34 @@
|
||||
import base64
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
import traceback
|
||||
from io import StringIO
|
||||
from subprocess import PIPE, Popen, TimeoutExpired
|
||||
|
||||
def popen(cmd, sys_env=True, **kwargs):
|
||||
if sys_env and kwargs.get('env') is not None:
|
||||
kwargs['env'] = os.environ.copy().update(kwargs['env'])
|
||||
return Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, encoding='utf-8', **kwargs)
|
||||
|
||||
|
||||
def execute(cmd, input_str=None, timeout=None, **kwargs):
|
||||
p = popen(cmd, **kwargs)
|
||||
try:
|
||||
out, err = p.communicate(input_str, timeout=timeout)
|
||||
except TimeoutExpired:
|
||||
out = ''
|
||||
err = get_exception()
|
||||
p.kill()
|
||||
stat = p.returncode
|
||||
return stat, out, err
|
||||
|
||||
|
||||
def get_exception():
|
||||
with StringIO() as io:
|
||||
traceback.print_exc(file=io)
|
||||
io.seek(0)
|
||||
content = io.read()
|
||||
|
||||
return content
|
||||
Reference in New Issue
Block a user