-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathprocess_manager.py
More file actions
42 lines (35 loc) · 1.21 KB
/
process_manager.py
File metadata and controls
42 lines (35 loc) · 1.21 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
'''
All tools needed to manage external executable and processes
'''
import sys
import subprocess
import logging
log = logging.getLogger('QESAP')
def subprocess_run(cmd, env=None):
"""Tiny wrapper around subprocess
Args:
cmd (list of string): directly used as input for subprocess.run
Returns:
(int, list of string): exit code and list of stdout
"""
if 0 == len(cmd):
log.error("Empty command")
return (1, [])
log.info("Run: '%s'", ' '.join(cmd))
if env is not None:
log.info("with env %s", env)
proc = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, check=False, env=env)
if proc.returncode != 0:
log.error("ERROR %d in %s", proc.returncode, ' '.join(cmd[0:1]))
for line in proc.stdout.decode('UTF-8').splitlines():
log.error("OUTPUT: %s", line)
return (proc.returncode, [])
stdout = [line.decode("utf-8") for line in proc.stdout.splitlines()]
print(f"OUTPUT START FOR COMMAND: {cmd}")
sys.stdout.flush()
for line in stdout:
print(line)
sys.stdout.flush()
print(f"OUTPUT END FOR COMMAND: {cmd}")
sys.stdout.flush()
return (0, stdout)