summaryrefslogtreecommitdiffstats
path: root/src/lib
diff options
context:
space:
mode:
authorNarayan Desai <desai@mcs.anl.gov>2004-09-07 17:54:04 +0000
committerNarayan Desai <desai@mcs.anl.gov>2004-09-07 17:54:04 +0000
commit913425e3f9f4dbadd782dbf17e63829aaac1656f (patch)
tree657e1d814c686ae3ed84457e63b244e8737c28ab /src/lib
parenta5b5c2ff029cdff6af0d0446dda7e1f50ef83b71 (diff)
downloadbcfg2-913425e3f9f4dbadd782dbf17e63829aaac1656f.tar.gz
bcfg2-913425e3f9f4dbadd782dbf17e63829aaac1656f.tar.bz2
bcfg2-913425e3f9f4dbadd782dbf17e63829aaac1656f.zip
add ScopedXMLFile
(Logical change 1.58) git-svn-id: https://svn.mcs.anl.gov/repos/bcfg/trunk/bcfg2@318 ce84e21b-d406-0410-9b95-82705330c041
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/Server/Generator.py57
1 files changed, 56 insertions, 1 deletions
diff --git a/src/lib/Server/Generator.py b/src/lib/Server/Generator.py
index 90eded211..a1048a4de 100644
--- a/src/lib/Server/Generator.py
+++ b/src/lib/Server/Generator.py
@@ -138,4 +138,59 @@ class SingleXMLFileBacked(XMLFileBacked):
XMLFileBacked.__init__(self, filename)
fam.AddMonitor(filename, self)
-
+class ScopedXMLFile(SingleXMLFileBacked):
+ __containers__ = ['Class','Host','Image']
+
+ def StoreRecord(self, metadata, entry):
+ if not self.store.has_key(entry.tag):
+ self.store[entry.tag] = {}
+ if not self.store[entry.tag].has_key(entry.attrib['name']):
+ self.store[entry.tag][entry.attrib['name']] = []
+ self.store[entry.tag][entry.attrib['name']].append((metadata, entry))
+
+ def Index(self):
+ a = XML(self.data)
+ self.store = {}
+ for e in a.getchildren():
+ if e.tag not in self.__containers__:
+ self.StoreRecord(('Global','all'), e)
+ else:
+ m = (e.tag, e.attrib['name'])
+ for entry in e.getchildren():
+ self.StoreRecord(m, entry)
+ # now to build the __provides__ table
+ self.__provides__ = {}
+ for key in self.store.keys():
+ self.__provides__[key] = {}
+ for j in self.store[key].keys():
+ self.__provides__[key][j] = self.FetchRecord
+ # also need to sort all leaf node lists
+ self.store[key][j].sort(self.Sort)
+
+ def Sort(self, m1, m2):
+ d = {('Global','Host'):-1,('Global','Image'):-1,("Global",'Class'):-1,
+ ('Image', 'Global'):1, ('Image', 'Image'):0, ('Image', 'Host'):1, ('Image', 'Class'):-1,
+ ('Class','Global'):1, ('Class', 'Image'):1, ('Class','Class'):0, ('Class', 'Host'): -1,
+ ('Host', 'Global'):1, ('Host', 'Image'):1, ('Host','Class'):1, ('Host','Host'):0}
+ if d.has_key((m1[0][0], m2[0][0])):
+ return d[(m1[0][0],m2[0][0])]
+
+ def MatchMetadata(self, m, metadata):
+ if m[0] == 'Global':
+ return True
+ elif m[0] == 'Image':
+ if m[1] == metadata.image:
+ return True
+ elif m[0] == 'Class':
+ if m[1] in metadata.classes:
+ return True
+ elif m[0] == 'Host':
+ if m[1] == metadata.hostname:
+ return True
+ return False
+
+ def FetchRecord(self, entry, metadata):
+ l = self.store[entry.tag][entry.attrib['name']]
+ useful = filter(lambda x:self.MatchMetadata(x[0], metadata), l)
+ data = useful[-1][-1]
+ entry.attrib.update(data.attrib)