[Dev OpenGP] [81] Message of the day (in progress).

[ Thread Index | Date Index | More opengp.tuxfamily.org/development Archives ]


Revision: 81
Author:   alband85
Date:     2009-03-24 14:36:02 +0100 (Tue, 24 Mar 2009)

Log Message:
-----------
Message of the day (in progress).

Modified Paths:
--------------
    trunk/src/lib/ogp/plugins/motd/motd.py
    trunk/src/lib/ogp/plugins/plugin.py


Modified: trunk/src/lib/ogp/plugins/motd/motd.py
===================================================================
--- trunk/src/lib/ogp/plugins/motd/motd.py	2009-03-24 13:35:37 UTC (rev 80)
+++ trunk/src/lib/ogp/plugins/motd/motd.py	2009-03-24 13:36:02 UTC (rev 81)
@@ -7,11 +7,10 @@
 class Motd(Plugin):
 	name = "motd"
 	files = ['motd', 'motd.tail']
-	__motd_xpath = "/" + OgpXmlConsts.TAG_PLUGIN + "/" + OgpXmlConsts.TAG_FILES + "/" + \
-			OgpXmlConsts.TAG_FILE + "[@" + OgpXmlConsts.ATTR_FILE_NAME + "='motd']" #+ \
-			#"/" + OgpXmlConsts.TAG_CONF
-	__conf_xpath = "/" + OgpXmlConsts.TAG_PLUGIN + "/" + OgpXmlConsts.TAG_CONF 
-	__distro_xpath = "/" + OgpXmlConsts.TAG_PLUGIN + "/" + OgpXmlConsts.TAG_CONF + "/distro" 
+	__motd_xpath = OgpXmlConsts.TAG_FILES + "/" + OgpXmlConsts.TAG_FILE + \
+			"[@" + OgpXmlConsts.ATTR_FILE_NAME + "='motd']" 
+	__conf_xpath = OgpXmlConsts.TAG_CONF 
+	__distro_xpath = OgpXmlConsts.TAG_CONF + "/distro" 
 
 	def pushFile(self, file, content, blocking=False):
 		if file == 'motd.tail':
@@ -19,33 +18,19 @@
 			file = 'motd'
 
 		if file == 'motd':
-			print xpath_arg
-			file_e = self.currentConf.xpath(self.__motd_xpath)[0]
+			file_e = self.currentConf.xpath(self.__motd_xpath + '/' + OgpXmlConsts.TAG_CONF)[0]
 			file_e.text = content
 			file_e.blocking = blocking
 
 	def pullFile(self, file, fullTree=False):
 		if fullTree:
-			parentConf = self.__core.pullPluginConf(self.parentDn, self.name, fullTree=True)
+			parentConf = self.core.pullPluginConf(self.parentDn, self.name, fullTree=True)
 			if parentConf is None:
 				parentConf = OgpElement.makePlugin(self.name, self.files)
 			parentConf.merge(self.currentConf)
-			return parentConf.xpath(self.__motd_xpath)[0].text
+			return parentConf.xpath(self.__motd_xpath + '/' + OgpXmlConsts.TAG_CONF)[0].text
 		else:
-			# Not working here. Got an XML root issue. 
-			# Got to fix it quickly!
-			print self.currentConf.toString()
-			print "---------------------------------------"
-			xp_test = "//plugin/files/file[@name='motd']"
-			print self.currentConf.xpath(xp_test)
-			tmp = fromstring(self.currentConf.toString())
-			tmp2 = tmp.xpath(self.__motd_xpath)
-			print tmp2
-			print tmp2[0]
-			#print (self.__motd_xpath)
-			#print self.currentConf.xpath("//plugin")
-			#return self.currentConf.xpath(self.__motd_xpath)[0].text
-			return
+			return self.currentConf.xpath(self.__motd_xpath + '/' + OgpXmlConsts.TAG_CONF)[0].text
 
 	def help(self, cmdName=None):
 		if cmdName is None:
@@ -65,7 +50,7 @@
 				blocking = argv['blocking']
 			except:
 				blocking = False
-			dist_e = self.currentConf.xpath(self.__motd_xpath)
+			dist_e = self.currentConf.xpath(self.__distro_xpath)
 			if len(dist_e) == 0:
 				dist_e = Element('distro')
 				conf_e = self.currentConf.xpath(self.__conf_xpath)[0]
@@ -76,7 +61,7 @@
 			dist_e.blocking = blocking
 
 	def installConf(self):
-		motd = self.pullFile('motd', True)
+		motd = str(self.pullFile('motd', True))
 		dist_e = self.currentConf.xpath(self.__motd_xpath)
 		if len(dist_e) != 0:
 			distro = dist_e[0].text
@@ -84,12 +69,12 @@
 			distro = None
 
 		if distro == 'debian':
-			f = open('/etc/motd.tail','w')
+			f = open('/home/alban/tmp/ogp/etc/motd.tail','w')
 			f.write(motd)
 			f.close()
-			spawnl(P_WAIT, '/etc/init.d/bootmisc.sh', 'start')
+			spawnl(P_WAIT, '/home/alban/tmp/ogp/etc/init.d/bootmisc.sh', 'start')
 		else:
-			f = open('/etc/motd', 'w')
+			f = open('/home/alban/tmp/ogp/etc/motd', 'w')
 			f.write(motd)
 			f.close()
 

Modified: trunk/src/lib/ogp/plugins/plugin.py
===================================================================
--- trunk/src/lib/ogp/plugins/plugin.py	2009-03-24 13:35:37 UTC (rev 80)
+++ trunk/src/lib/ogp/plugins/plugin.py	2009-03-24 13:36:02 UTC (rev 81)
@@ -4,8 +4,10 @@
 from ldap.dn import str2dn, dn2str
 from lxml.etree import *
 from ogp.core import *
+from ogp.misc import *
+import os
+from stat import ST_MODE, S_IXUSR, S_IRUSR, S_IWUSR, S_IXGRP, S_IRGRP, S_IWGRP, S_IXOTH, S_IROTH, S_IWOTH, S_ISUID, S_ISGID, S_ISVTX
 
-
 class omitted(object):
 	pass
 
@@ -14,7 +16,8 @@
 		Plugin class and metaclass __setattr__ method
 		Throws an exception when attempting to modify the plugin name.
 	"""
-	if item == "name" or item == "files":
+	ro = ['name', 'files']
+	if item in ro:
 		raise OgpPluginError('__setattr__: ' + item + ' is readonly.')
 	self.__dict__[item] = value
 
@@ -35,11 +38,11 @@
 	parentDn = None
 	currentConf = None
 	dn = None
-	__core = None
+	core = None
 	__registeredPlugins = dict()
 
 	def __init__(self, dn):
-		self.__core = OgpCore.getInstance()
+		self.core = OgpCore.getInstance()
 		self.dn = dn
 		# Dirty but it pleases Michel :-P
 		# Loads RW XML conf from LDAP
@@ -82,13 +85,13 @@
 		"""
 			Commit changes to LDAP
 		"""
-		self.__core.pushPluginConf(self.dn, self.currentConf)
+		self.core.pushPluginConf(self.dn, self.currentConf)
 
 	def cancel(self):
 		"""
 			Do not commit and discard changes.
 		"""
-		self.currentConf = self.__core.pullPluginConf(self.dn, self.name)
+		self.currentConf = self.core.pullPluginConf(self.dn, self.name)
 		if self.currentConf is None:
 			self.currentConf = OgpElement.makePlugin(self.name, self.files)
 
@@ -134,7 +137,7 @@
 				gid_e.blocking = blocking
 
 	def __getFile(self, fileName):
-		arg = '/' + OgpXmlConsts.TAG_PLUGIN + '/' + OgpXmlConsts.TAG_FILES + '/' + OgpXmlConsts.TAG_FILE + '[@' + OgpXmlConsts.ATTR_FILE_NAME + "='" + fileName + "']"
+		arg = OgpXmlConsts.TAG_FILES + '/' + OgpXmlConsts.TAG_FILE + '[@' + OgpXmlConsts.ATTR_FILE_NAME + "='" + fileName + "']"
 		try:
 			return self.currentConf.xpath(arg)[0]
 		except:
@@ -148,7 +151,7 @@
 		file_e = self.__getFile(fileName)
 		sec_e = file_e.xpath(OgpXmlConsts.TAG_SECURITY)[0]
 		for tag in rights:
-			if tag in OgpXmlConsts.TAGS_SECURITY:
+			if tag in OgpXmlConsts.TAGS_MOD:
 				tag_e = sec_e.xpath(tag)
 				if len(tag_e) != 0:
 					tag_e = tag_e[0]
@@ -166,6 +169,55 @@
 				#TODO: log!
 				pass
 
+	def setSecurityAttributes(self, fileName, filePath):
+		"""
+			Reads attributes for file 'fileName' in XML tree and sets them
+			on file 'filePath'
+		"""
+		# Default file stats (644)
+		mask = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH 
+		
+		# Security attributes for fileName
+		xpath_sec_attr = OgpXmlConsts.TAG_FILES + '/' + OgpXmlConsts.TAG_FILE + "[@" + OgpXmlConsts.ATTR_FILE_NAME + "='" + fileName + "']" + \
+				'/' + OgpXmlConsts.TAG_SECURITY
+		parentConf = self.core.pullPluginConf(self.parentDn, self.name, fullTree=True)
+		if parentConf is None:
+			parentConf = OgpElement.makePlugin(self.name, self.files)
+		parentConf.merge(self.currentConf)
+		sec_e = parentConf.xpath(xpath_sec_attr)[0]
+		mod_attr = {
+					'ux':S_IXUSR, 
+					'ur':S_IRUSR,
+					'uw':S_IWUSR,
+					'gx':S_IXGRP,
+					'gr':S_IRGRP,
+					'gw':S_IWGRP,
+					'ox':S_IXOTH,
+					'or':S_IROTH,
+					'ow':S_IWOTH,
+					'us':S_ISUID,
+					'gs':S_ISGID,
+					't': S_ISVTX
+				}
+		print oct(mask)
+		for attr in sec_e:
+			if attr.tag in OgpXmlConsts.TAGS_OWN:
+				if attr.tag == 'uid':
+					os.chown(filePath, int(attr.text), -1)
+				elif attr.tag == 'gid':
+					os.chown(filePath, -1, int(attr.text))
+				else:
+					pass
+			elif attr.tag in OgpXmlConsts.TAGS_MOD:
+				print attr.tag + ": " + attr.text
+				if smart_bool(attr.text):
+					#print str(oct(mask)) + ' | ' + str(oct(mod_attr[attr.tag]))
+					mask = mask | mod_attr[attr.tag]
+				else:
+					#print str(oct(mask)) + ' & ~' + str(oct(mod_attr[attr.tag]))
+					mask = mask & ~(mod_attr[attr.tag])
+				print oct(mask)
+
 	# Abstract methods
 	def installConf(self):
 		"""


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/