d/scripts/xen-init-list: python3 and remove cruft
authorHans van Kranenburg <hans@knorrie.org>
Fri, 4 Sep 2020 13:56:07 +0000 (15:56 +0200)
committerHans van Kranenburg <hans@knorrie.org>
Thu, 17 Sep 2020 18:54:26 +0000 (20:54 +0200)
This little script is used by the xendomains init script. See new
comment inside for more info.

* Change it to python3
* Remove all the obsolete xend related SXP cruft
* Remove all the overly obsessed object orientedness

Signed-off-by: Hans van Kranenburg <hans@knorrie.org>
debian/scripts/xen-init-list

index 99646ce1e505c7556ae8220be4dcb562479d224c..7ef25fe355121e742f02ca9e1147f38702a6058c 100755 (executable)
@@ -1,73 +1,18 @@
-#!/usr/bin/python
+#!/usr/bin/python3
 
 import json
-import re
-import sys
 import subprocess
 
-
-class SXPParser(object):
-    tokenizer_rules = r""" (?P<open> \( ) | (?P<close> \) ) | (?P<whitespace> \s+ ) | [^()^\s]+ """
-    tokenizer_re = re.compile(tokenizer_rules, re.X)
-
-    @classmethod
-    def loads(cls, input):
-        data = []
-        stack = []
-        for match in cls.tokenizer_re.finditer(input):
-            if match.group('open'):
-                stack.append([])
-            elif match.group('close'):
-                top = stack.pop()
-                if stack:
-                    stack[-1].append(top)
-                else:
-                    data.append(top)
-            elif match.group('whitespace'):
-                pass
-            else:
-                if stack:
-                    stack[-1].append(match.group())
-        return data
-
-
-class Data(object):
-    def __call__(self, out):
-        for domid, info in sorted(self.data.iteritems(), reverse=True):
-            if domid == 0:
-                continue
-            out.write('{!s} {}\n'.format(domid, *info))
-
-
-class DataJSON(Data):
-    def __init__(self, p):
-        s = json.loads(p)
-        self.data = d = {}
-        for i in s:
-            domid = i['domid']
-            name = i['config']['c_info']['name']
-            d[domid] = (name, )
-
-
-class DataSXP(Data):
-    def __init__(self, p):
-        s = SXPParser.loads(p)
-        self.data = d = {}
-        for i in s:
-            if i and i[0] == 'domain':
-                try:
-                    data = dict(j for j in i if len(j) == 2)
-                    domid = int(data['domid'])
-                    name = data['name']
-                    d[domid] = (name, )
-                except (KeyError, ValueError) as e:
-                    pass
-
-
-if __name__ == '__main__':
-    p = subprocess.check_output(('xen', 'list', '-l'))
-    if p[0] == '(':
-        d = DataSXP(p)
-    else:
-        d = DataJSON(p)
-    d(sys.stdout)
+"""
+This little script is used by the xendomains init script. It prints all running
+domUs with their domain id and name.
+"""
+
+cmd = ('xen', 'list', '-l')
+xen_list_json = subprocess.check_output(cmd)
+for domU_info in json.loads(xen_list_json):
+    domid = domU_info['domid']
+    if domid == 0:
+        continue
+    name = domU_info['config']['c_info']['name']
+    print(domid, name)