cleaning up messaging related to 'service plc stop'
[myplc.git] / plc_config.py
index b3ae90d..45805b9 100644 (file)
@@ -11,6 +11,7 @@
 #
 
 import xml.dom.minidom
+from xml.parsers.expat import ExpatError
 from StringIO import StringIO
 import time
 import re
@@ -20,6 +21,8 @@ import os
 import types
 
 
+class ConfigurationException(Exception): pass
+
 class PLCConfiguration:
     """
     Configuration file store. Optionally instantiate with a file path
@@ -187,7 +190,11 @@ class PLCConfiguration:
         Merge file into configuration store.
         """
 
-        dom = xml.dom.minidom.parse(file)
+        try:
+            dom = xml.dom.minidom.parse(file)
+        except ExpatError, e:
+            raise ConfigurationException, e
+
         if type(file) in types.StringTypes:
             self._files.append(os.path.abspath(file))
 
@@ -225,7 +232,7 @@ class PLCConfiguration:
                 file = "/etc/planetlab/plc_config.xml"
 
         if type(file) in types.StringTypes:
-            fileobj = open(file, 'r+')
+            fileobj = open(file, 'w')
         else:
             fileobj = file
 
@@ -235,6 +242,44 @@ class PLCConfiguration:
 
         fileobj.close()
 
+    def verify(self, default, read, verify_variables={}):
+        """ Confirm that the existing configuration is consistent
+            according to the checks below.
+
+            It looks for filled-in values in the order of, local object (self),
+            followed by cread (read values), and finally default values.
+
+        Arguments: 
+
+            default configuration
+            site configuration
+            list of category/variable tuples to validate in these configurations
+
+        Returns:
+
+            dict of values for the category/variables passed in
+            If an exception is found, ConfigurationException is raised.
+
+        """
+
+        validated_variables = {}
+        for category_id, variable_id in verify_variables.iteritems():
+            category_id = category_id.lower()
+            variable_id = variable_id.lower()
+            variable_value = None
+            sources = (self, read, default)
+            for source in sources:
+                (category_value, variable_value) = source.get(category_id,variable_id)
+                if variable_value <> None:
+                    entry = validated_variables.get(category_id,[])
+                    entry.append(variable_value['value'])
+                    validated_variables["%s_%s"%(category_id.upper(),variable_id.upper())]=entry
+                    break
+            if variable_value == None:
+                raise ConfigurationException("Cannot find %s_%s)" % \
+                                             (category_id.upper(),
+                                              variable_id.upper()))
+        return validated_variables
 
     def get(self, category_id, variable_id):
         """
@@ -388,6 +433,22 @@ class PLCConfiguration:
             variables[variable_id] = variable
 
 
+    def locate_varname (self, varname):
+        """
+        Locates category and variable from a variable's (shell) name
+
+        Returns:
+        (variable, category) when found
+        (None, None) otherwise
+        """
+        
+        for (category_id, (category, variables)) in self._variables.iteritems():
+            for variable in variables.values():
+                (id, name, value, comments) = self._sanitize_variable(category_id, variable)
+                if (id == varname):
+                    return (category,variable)
+        return (None,None)
+
     def get_package(self, group_id, package_name):
         """
         Get the specified package in the specified package group.
@@ -633,7 +694,7 @@ DO NOT EDIT. This file was automatically generated at
         return header.strip().split(os.linesep)
 
 
-    def output_shell(self, encoding = "utf-8"):
+    def output_shell(self, show_comments = True, encoding = "utf-8"):
         """
         Return variables as a shell script.
         """
@@ -644,11 +705,12 @@ DO NOT EDIT. This file was automatically generated at
         for (category_id, (category, variables)) in self._variables.iteritems():
             for variable in variables.values():
                 (id, name, value, comments) = self._sanitize_variable(category_id, variable)
-                buf.write(os.linesep)
-                if name is not None:
-                    buf.write("# " + name + os.linesep)
-                if comments is not None:
-                    buf.writelines(["# " + line + os.linesep for line in comments])
+                if show_comments:
+                    buf.write(os.linesep)
+                    if name is not None:
+                        buf.write("# " + name + os.linesep)
+                    if comments is not None:
+                        buf.writelines(["# " + line + os.linesep for line in comments])
                 # bash does not have the concept of NULL
                 if value is not None:
                     buf.write(id + "=" + value + os.linesep)
@@ -675,7 +737,7 @@ DO NOT EDIT. This file was automatically generated at
                     buf.writelines(["// " + line + os.linesep for line in comments])
                 if value is None:
                     value = 'NULL'
-                buf.write("DEFINE('%s', %s);" % (id, value) + os.linesep)
+                buf.write("define('%s', %s);" % (id, value) + os.linesep)
 
         buf.write("?>" + os.linesep)
 
@@ -724,6 +786,19 @@ DO NOT EDIT. This file was automatically generated at
         return buf.getvalue()
 
 
+    def output_groups(self, encoding = "utf-8"):
+        """
+        Return list of all package group names.
+        """
+
+        buf = codecs.lookup(encoding)[3](StringIO())
+
+        for (group, packages) in self._packages.values():
+            buf.write(group['name'] + os.linesep)
+
+        return buf.getvalue()
+
+
     def output_comps(self, encoding = "utf-8"):
         """
         Return <comps> section of configuration.
@@ -768,6 +843,6 @@ class TrimTextElement(xml.dom.minidom.Element):
 
 if __name__ == '__main__':
     import sys
-    if len(sys.argv) > 1 and sys.argv[1] in ['build', 'install']:
+    if len(sys.argv) > 1 and sys.argv[1] in ['build', 'install', 'uninstall']:
         from distutils.core import setup
         setup(py_modules=["plc_config"])