summaryrefslogtreecommitdiffstats
path: root/src/lib/Server/Core.py
diff options
context:
space:
mode:
authorNarayan Desai <desai@mcs.anl.gov>2004-08-11 19:54:35 +0000
committerNarayan Desai <desai@mcs.anl.gov>2004-08-11 19:54:35 +0000
commit9710f2653e135a7f33240fdc27985f5638e236d7 (patch)
tree4824f94b14e445736dfc5c242997aed6900d04a6 /src/lib/Server/Core.py
parent697c558114b0cef516230dec7bbcc7929499717d (diff)
downloadbcfg2-9710f2653e135a7f33240fdc27985f5638e236d7.tar.gz
bcfg2-9710f2653e135a7f33240fdc27985f5638e236d7.tar.bz2
bcfg2-9710f2653e135a7f33240fdc27985f5638e236d7.zip
Rename: src/Core.py -> src/lib/Server/Core.py
(Logical change 1.33) git-svn-id: https://svn.mcs.anl.gov/repos/bcfg/trunk/bcfg2@191 ce84e21b-d406-0410-9b95-82705330c041
Diffstat (limited to 'src/lib/Server/Core.py')
-rw-r--r--src/lib/Server/Core.py111
1 files changed, 111 insertions, 0 deletions
diff --git a/src/lib/Server/Core.py b/src/lib/Server/Core.py
index e69de29bb..a87c6a589 100644
--- a/src/lib/Server/Core.py
+++ b/src/lib/Server/Core.py
@@ -0,0 +1,111 @@
+#!/usr/bin/python
+# $Id$
+
+from os import stat
+from stat import ST_MODE, S_ISDIR
+from syslog import syslog, LOG_ERR
+
+from Error import PublishError
+from Types import Clause
+
+import _fam
+
+class fam(object):
+ '''The fam object contains alteration monitors'''
+
+ def __init__(self):
+ self.fm = _fam.open()
+ self.users = {}
+ self.handles = {}
+
+ def fileno(self):
+ return self.fm.fileno()
+
+ def AddMonitor(self, path, obj=None):
+ m = stat(path)[ST_MODE]
+ if S_ISDIR(m):
+ h = self.fm.monitorDirectory(path, None)
+ self.handles[h.requestID()] = h
+ else:
+ h = self.fm.monitorFile(path, None)
+ self.handles[h.requestID()] = h
+ if obj != None:
+ self.users[h.requestID()] = obj
+ return h.requestID()
+
+ def HandleEvent(self):
+ event = self.fm.nextEvent()
+ id = event.requestID
+ if self.users.has_key(id):
+ self.users[id].HandleEvent(event)
+
+class PublishedValue(object):
+ def __init__(self,owner,key,value):
+ self.owner=owner
+ self.key=key
+ self.value=value
+
+ def Update(self,owner,value):
+ if owner != self.owner:
+ raise PublishError, (self.key,owner)
+ self.value=value
+
+class Core(object):
+ def __init__(self, repository, structures, generators):
+ self.datastore = repository
+ self.fam = fam()
+ self.pubspace = {}
+ self.structures = []
+ for structure in structures:
+ s = getattr(__import__(structure),structure)
+ self.structures.append(s(self, self.datastore))
+ self.generators = []
+ for generator in generators:
+ g = getattr(__import__(generator),generator)
+ self.generators.append(g(self, self.datastore))
+ # we need to inventory and setup generators
+ # Process generator requirements
+ for g in self.generators:
+ for prq in g.__requires__:
+ if not self.pubspace.has_key(prq):
+ raise GeneratorError, (g.name, prq)
+ g.CompleteSetup()
+
+ def PublishValue(self,owner,key,value):
+ if not self.pubspace.has_key(key):
+ # This is a new entry
+ self.pubspace[key]=PublishedValue(owner,key,value)
+ else:
+ # This is an old entry. Update can fai
+ try:
+ self.pubspace[key].Update(owner,value)
+ except PublishError,e:
+ syslog(LOG_ERR, "Publish conflict for %s. Owner %s, Modifier %s"%(key,self.pubspace[key].owner,owner))
+
+ def ReadValue(self,key):
+ if self.pubspace.has_key(key):
+ return self.pubspace[key].value
+ raise KeyError,key
+
+ def GetStructures(self, metadata):
+ return reduce(lambda x,y:x+y, map(lambda x:x.Construct(metadata), self.structures))
+
+ def BindStructure(self, structure, metadata):
+ for entry in structure.getchildren():
+ self.Bind(entry, metadata)
+
+ def Bind(self, entry, metadata):
+ g = [x for x in self.generators if x.__provides__.get(entry.tag, {}).has_key(entry.attrib['name'])]
+ if len(g) == 1:
+ return g[0].__provides__[entry.tag][entry.attrib['name']](entry, metadata)
+ elif len(g) > 1:
+ print "Data Integrity error for %s %s"%(entry.tag, entry.attrib['name'])
+ else:
+ for g in self.generators:
+ if hasattr(g, "FindHandler"):
+ try:
+ return g.FindHandler(entry)(entry, metadata)
+ except:
+ print g, "failed"
+ raise KeyError, (entry.tag,entry.attrib['name'])
+