From 42b1fae3f2c48df33c2a5c1d31fb82cfb5c6c202 Mon Sep 17 00:00:00 2001 From: Hans van Kranenburg Date: Fri, 4 Sep 2020 15:48:20 +0200 Subject: [PATCH] d/scripts/xen-init-name: 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-name | 74 +++++++++--------------------------- 1 file changed, 17 insertions(+), 57 deletions(-) diff --git a/debian/scripts/xen-init-name b/debian/scripts/xen-init-name index 21aad6fd48..fe8f61fca1 100755 --- a/debian/scripts/xen-init-name +++ b/debian/scripts/xen-init-name @@ -1,61 +1,21 @@ -#!/usr/bin/python +#!/usr/bin/python3 import json -import re -import sys import subprocess +import sys - -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): - out.write('{}\n'.format(self.name)) - - -class DataJSON(Data): - def __init__(self, p): - s = json.loads(p) - self.name = s['c_info']['name'] - - -class DataSXP(Data): - def __init__(self, p): - s = SXPParser.loads(p) - for i in s: - if i and i[0] == 'domain': - data = dict(j for j in i if len(j) == 2) - self.name = data['name'] - break - - -if __name__ == '__main__': - p = subprocess.check_output(('xen', 'create', '--quiet', '--dryrun', '--defconfig', sys.argv[1])) - if p[0] == '(': - d = DataSXP(p) - else: - d = DataJSON(p) - d(sys.stdout) +""" +This little script is used by the xendomains init script. It gets a filename of +a domU guest config file as argument and uses xen create --dryrun to parse the +config file and extract the domU name from it. + +Note that there is no proper error handling. So, if a user symlinks something +else than a correctly formatted domU config file into /etc/xen/auto (or a +different path configured to use, of course) then this will just do ugly things +and explode. +""" + +cmd = ('xen', 'create', '--quiet', '--dryrun', '--defconfig', sys.argv[1]) +domU_json = subprocess.check_output(cmd) +name = json.loads(domU_json)['c_info']['name'] +print(name) -- 2.30.2