LVM metadata reader script in lvm-backup-reader
[invirt/scripts/lvm-backup-reader.git] / lvm-backup-reader.py
1 import re
2
3 def complete(value):
4     if value.startswith('[') and not value.endswith(']'):
5         return False
6     return True
7
8 def interpret(value):
9     return eval(value) # good enough for now
10
11 class Metadata:
12     def __init__(self, fn):
13         self.text = open(fn, 'r').read()
14         lines = filter(lambda l: not l.startswith("#") and not l.strip()=='', self.text.splitlines())
15         d = {}
16         parent = []
17         node = d
18         mode = 0
19         for l in lines:
20             if mode == 0:
21                 if l.strip() == '}':
22                     node = parent[-1]
23                     del parent[-1]
24                 elif l.strip().endswith('{'):
25                     name = l.strip()[:-1].strip()
26                     parent.append(node)
27                     child = {}
28                     node[name] = child
29                     node = child
30                 else:
31                     key, value = re.search(r'\t*(\S*) = (.*?)(\s*# .*)?$', l).groups()[0:2]
32                     if not complete(value):
33                         mode = 1
34                     else:
35                         node[key] = interpret(value)
36             elif mode == 1:
37                 value += re.search(r'\t*(.*?)( *# .*)?$', l).group(0).strip()
38                 if complete(value):
39                     node[key] = interpret(value)
40                     mode = 0
41         self.d = d
42
43 import pprint
44 pprint.pprint(Metadata('xenvg-backup').d)