forked from ViaGenetics/dx_applet_utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanage_command_execution.py
70 lines (56 loc) · 1.9 KB
/
manage_command_execution.py
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
66
67
68
69
70
import os
import sys
import dxpy
import logging
import subprocess
from time import gmtime, strftime
logger = logging.getLogger(__name__)
logger.addHandler(dxpy.DXLogHandler())
logger.propagate = False
logger.setLevel("INFO")
def execute_command(command, debug=False):
"""Executes given command
:param: `command`: Command to execute
:param: `debug`: If True, will log all messages to console
:returns: Object with results of the executed command
"""
if debug:
p = subprocess.Popen(command, shell=True)
p.wait()
out = ""
err = ""
sys_code = p.returncode
else:
p = subprocess.Popen(
command,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
out,err = p.communicate()
sys_code = p.returncode
return {
"sysCode": sys_code,
"cmd": command,
"out": out.strip(),
"err": err.strip()
}
def check_execution_syscode(command_result, command_description):
"""Verifies if executed command finished successfully
:param: `command_result`: Object returned from execute_command function
:param: `command_description`: Description of command for display in logs
"""
if command_result["sysCode"] == 0:
logger.info("{0}: Execution of {1} finished successfully!".format(
strftime("%Y-%m-%d %H:%M:%S", gmtime()), command_description)
)
else:
logger.error("{0}: Execution of {1} failed!".format(
strftime("%Y-%m-%d %H:%M:%S", gmtime()), command_description)
)
logger.error("{0}: Command that was executed: {1}".format(
strftime("%Y-%m-%d %H:%M:%S", gmtime()), command_result["cmd"])
)
logger.error("STDOUT: {0}".format(command_result["out"]))
logger.error("STDERR: {0}".format(command_result["err"]))
sys.exit(1)