From: Peter Iannucci Date: Wed, 25 Mar 2009 03:24:00 +0000 (-0400) Subject: LVM metadata reader script in lvm-backup-reader X-Git-Url: http://xvm.mit.edu/gitweb/invirt/scripts/lvm-backup-reader.git/commitdiff_plain/f123965b7ce149572fe4c9172089e848c956dc5e LVM metadata reader script in lvm-backup-reader svn path=/trunk/scripts/lvm-backup-reader/; revision=2270 --- f123965b7ce149572fe4c9172089e848c956dc5e diff --git a/lvm-backup-reader.py b/lvm-backup-reader.py new file mode 100755 index 0000000..0f5e668 --- /dev/null +++ b/lvm-backup-reader.py @@ -0,0 +1,44 @@ +import re + +def complete(value): + if value.startswith('[') and not value.endswith(']'): + return False + return True + +def interpret(value): + return eval(value) # good enough for now + +class Metadata: + def __init__(self, fn): + self.text = open(fn, 'r').read() + lines = filter(lambda l: not l.startswith("#") and not l.strip()=='', self.text.splitlines()) + d = {} + parent = [] + node = d + mode = 0 + for l in lines: + if mode == 0: + if l.strip() == '}': + node = parent[-1] + del parent[-1] + elif l.strip().endswith('{'): + name = l.strip()[:-1].strip() + parent.append(node) + child = {} + node[name] = child + node = child + else: + key, value = re.search(r'\t*(\S*) = (.*?)(\s*# .*)?$', l).groups()[0:2] + if not complete(value): + mode = 1 + else: + node[key] = interpret(value) + elif mode == 1: + value += re.search(r'\t*(.*?)( *# .*)?$', l).group(0).strip() + if complete(value): + node[key] = interpret(value) + mode = 0 + self.d = d + +import pprint +pprint.pprint(Metadata('xenvg-backup').d)