-
Notifications
You must be signed in to change notification settings - Fork 4
/
nemea-modulesinfo.in
103 lines (77 loc) · 3.25 KB
/
nemea-modulesinfo.in
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/python3
# -*- coding: utf-8; -*-
"""Print Nemea modules info from supervisor. Optionally limit output
to info on supplied module names."""
__author__ = 'Ulrik Haugen <[email protected]>'
__copyright__ = 'Copyright 2017 Linköpings universitet'
import pprint as prettyprint
bindir="@bindir@"
def encodeunicode(element, context, maxlevels, level):
"""Encode unicode strings when pretty printing."""
if prettyprint._type(element) is unicode:
element = element.encode()
return prettyprint._safe_repr(element, context, maxlevels, level)
def setuppprint():
"""Setup pprint to use encodeunicode."""
global pprint
prettyprinter = prettyprint.PrettyPrinter()
prettyprinter.format = encodeunicode
pprint = prettyprinter.pprint
def getnemeamodulesinfo():
"""Call supervisor_cli for modules info and parse the returned
json."""
from subprocess import Popen, PIPE
from json import loads
from os.path import join
supervisorproc = Popen([ join(bindir, "supervisor_cli"), "-i", ],
stdout=PIPE, stderr=PIPE)
modulesinfojson, supervisorerr = supervisorproc.communicate()
if supervisorerr.strip() or supervisorproc.returncode:
raise EnvironmentError("Error retrieving modules info,"
" supervisor returned %d, stderr: \n%s"
% (supervisorproc.returncode,
supervisorerr.strip()))
return loads(modulesinfojson)
def selectmodulesinfo(modulesinfo, modulesrequested):
"""Select data from _modulesinfo_, limited to modules in the set
_modulesrequested_ unless empty, return tuple of selected data and
warnings."""
selectedmodulesinfo = None
warnings = []
if modulesrequested:
if not modulesrequested.issubset(modulesinfo.keys()):
warnings.append("Warning: No info on module(s): %s\n"
% ", ".join(sorted(modulesrequested.difference(
modulesinfo.keys()))))
if len(modulesrequested) == 1:
selectedmodulesinfo = modulesinfo[list(modulesrequested)[0]]
else:
selectedmodulesinfo = {
modulename: moduleinfo
for modulename, moduleinfo in modulesinfo.iteritems()
if modulename.encode() in modulesrequested }
else:
selectedmodulesinfo = modulesinfo
return selectedmodulesinfo, warnings
def usage(programname):
"""Print usage."""
print "usage: %s [ modulename ] ...\n\n%s" % (programname, __doc__)
def main(programname, *programargs, **kwargs):
"""Dispatch to printmodulesinfo, print errors/warnings to keyword arg
_errout_."""
import os
if ('-h' in programargs) or ('--help' in programargs):
usage(programname)
return os.EX_USAGE
errout = kwargs['errout']
setuppprint()
selectedmodulesinfo, warnings = selectmodulesinfo(getnemeamodulesinfo(),
frozenset(programargs))
for warning in warnings: errout.write(warning)
pprint(selectedmodulesinfo)
if warnings:
return os.EX_DATAERR
return os.EX_OK
if __name__ == '__main__':
import sys
sys.exit(main(*sys.argv, errout = sys.stderr))