tools.py
2.05 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import subprocess
import logging
import yaml
# setup logger for this module
logger = logging.getLogger(__name__)
# ---------------------------------------------------------------------------
# load data from yaml file
# ---------------------------------------------------------------------------
def load_yaml(filename, default=None):
try:
f = open(filename, 'r', encoding='utf-8')
except IOError:
logger.error('Can\'t open file "{}"'.format(filename))
return default
else:
with f:
try:
return yaml.load(f)
except yaml.YAMLError as e:
# except yaml.parser.ParserError:
mark = e.problem_mark
logger.error('In YAML file "{0}" near line {1}, column {2}.'.format(filename, mark.line, mark.column+1))
return default
# ---------------------------------------------------------------------------
# Runs a script and returns its stdout parsed as yaml, or None on error.
# Note: requires python 3.5+
# ---------------------------------------------------------------------------
def run_script(script, stdin='', timeout=5):
try:
p = subprocess.run([script],
input=stdin,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True,
timeout=timeout,
)
except FileNotFoundError:
logger.error('Script not found: "{0}".'.format(script))
except PermissionError:
logger.error('Script "{0}" not executable (wrong permissions?).'.format(script))
except subprocess.TimeoutExpired:
logger.error('Timeout {0}s exceeded while running script "{1}"'.format(timeout, script))
else:
if p.returncode != 0:
logger.warning('Script "{0}" returned error code {1}.'.format(script, p.returncode))
else:
try:
output = yaml.load(p.stdout)
except:
logger.error('Error parsing yaml output of script "{0}"'.format(script))
else:
return output