76 lines
2.8 KiB
Python
76 lines
2.8 KiB
Python
# -*- coding: utf-8 -*-
|
|
#########################################################
|
|
import os, sys, traceback, subprocess, json, platform
|
|
from framework import app, logger
|
|
|
|
|
|
class ToolSubprocess(object):
|
|
@classmethod
|
|
def execute_command_return(
|
|
cls, command, format=None, force_log=False, shell=False, env=None
|
|
):
|
|
try:
|
|
# pass
|
|
# logger.debug('execute_command_return : %s', ' '.join(command))
|
|
if app.config["config"]["running_type"] == "windows":
|
|
tmp = []
|
|
if type(command) == type([]):
|
|
for x in command:
|
|
if x.find(" ") == -1:
|
|
tmp.append(x)
|
|
else:
|
|
tmp.append(f'"{x}"')
|
|
command = " ".join(tmp)
|
|
|
|
iter_arg = b"" if app.config["config"]["is_py2"] else ""
|
|
if app.config["config"]["is_py2"]:
|
|
process = subprocess.Popen(
|
|
command,
|
|
stdout=subprocess.PIPE,
|
|
stderr=subprocess.STDOUT,
|
|
universal_newlines=True,
|
|
bufsize=1,
|
|
shell=shell,
|
|
env=env,
|
|
)
|
|
else:
|
|
process = subprocess.Popen(
|
|
command,
|
|
stdin=subprocess.PIPE,
|
|
stdout=subprocess.PIPE,
|
|
stderr=subprocess.STDOUT,
|
|
universal_newlines=True,
|
|
shell=shell,
|
|
env=env,
|
|
encoding="utf8",
|
|
)
|
|
|
|
# process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, shell=shell, env=env, encoding='utf8')
|
|
ret = []
|
|
with process.stdout:
|
|
for line in iter(process.stdout.readline, iter_arg):
|
|
ret.append(line.strip())
|
|
if force_log:
|
|
logger.debug(ret[-1])
|
|
process.wait() # wait for the subprocess to exit
|
|
|
|
if format is None:
|
|
ret2 = "\n".join(ret)
|
|
elif format == "json":
|
|
try:
|
|
index = 0
|
|
for idx, tmp in enumerate(ret):
|
|
# logger.debug(tmp)
|
|
if tmp.startswith("{") or tmp.startswith("["):
|
|
index = idx
|
|
break
|
|
ret2 = json.loads("".join(ret[index:]))
|
|
except Exception:
|
|
ret2 = None
|
|
|
|
return ret2
|
|
except Exception as exception:
|
|
logger.error("Exception:%s", exception)
|
|
logger.error(traceback.format_exc())
|
|
logger.error("command : %s", command)
|