-
Notifications
You must be signed in to change notification settings - Fork 1
/
nodeinfo.parser
executable file
·65 lines (53 loc) · 1.7 KB
/
nodeinfo.parser
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
#!/usr/bin/env python3
#
# nodeinfo.parser script extracting information from nodeinfo output
#
# Copyright (c) 2016-2017 Christoph Niethammer <[email protected]>
#
import argparse
import bz2
import gzip
import os
import sys
import xml.etree.ElementTree
def list_infos(root):
for info in root.findall('node/info') :
if len(list(info)) > 0 :
print(info.attrib['title'])
else :
print(info.attrib['title'], '(not available)')
def print_info(root, title):
for info in root.findall("node/info[@title='"+title+"']") :
print(info.find('commandoutput').text.strip())
def list_all(root):
list_infos(root)
def main(argv):
parser = argparse.ArgumentParser(
prog='nodeinfo parser',
description="Parses, extracts and searches for information from a nodeinfo file created with the nodeinfo script"
)
parser.add_argument("--info", help="list available info", action="store_true")
parser.add_argument("filename", nargs=1)
parser.add_argument("infoname", nargs="?")
args = parser.parse_args()
action = "list" if not args.infoname else "show"
filename = args.filename[0]
_, ext = os.path.splitext(filename)
if ext == ".bz2":
nodeinfo_fp = bz2.open(filename, "r")
elif ext == ".gz":
nodeinfo_fp = gzip.open(filename, "rb")
else:
nodeinfo_fp = open(filename)
e = xml.etree.ElementTree.parse(nodeinfo_fp)
nodeinfo_fp.close()
root = e.getroot()
if action == "list" :
if args.info:
list_infos(root)
else:
list_all(root)
elif action == 'show' :
print_info(root, args.infoname)
if __name__ == "__main__" :
main(sys.argv)