From 2eb4de195465b6baaaf807242545559f73e212e2 Mon Sep 17 00:00:00 2001 From: Hans van Kranenburg Date: Fri, 4 Sep 2020 15:56:07 +0200 Subject: [PATCH] d/scripts/xen-init-list: python3 and remove cruft 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 --- debian/scripts/xen-init-list | 83 ++++++------------------------------ 1 file changed, 14 insertions(+), 69 deletions(-) diff --git a/debian/scripts/xen-init-list b/debian/scripts/xen-init-list index 99646ce1e5..7ef25fe355 100755 --- a/debian/scripts/xen-init-list +++ b/debian/scripts/xen-init-list @@ -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 \( ) | (?P \) ) | (?P \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) -- 2.30.2