Commits

Olle Lundberg committed 7b6cfa8

pyling suggestions

  • Participants
  • Parent commits 543c3d8

Comments (0)

Files changed (1)

clusterCheckpoint.py

 
 class ClustatParser(object):
 
-    def __init__(self, xml, groupNames=None):
+    def __init__(self, xml, groupnames = None):
+        """ A class that makes it easier to work with the output of clustat
+        @type xml: string
+        @type groupnames: list or none
+        """
         self._xml = xml
-        self._groupNames = groupNames
+        self._groupnames = groupnames
         self._tree = libxml2.parseDoc(self._xml)
         self._context = self._tree.xpathNewContext()
-        self._localNode = [attr.content for attr in
-                self._context.xpathEval('//clustat/nodes/node[@local=1]/@name')][0]
-        if self._groupNames is None or len(self._groupNames) < 1:
-            self._parseGroupNames()
+        self._localnode, _ = self._xpath('//clustat/nodes/node[@local=1]/@name')
+        if self._groupnames is None or len(self._groupnames) < 1:
+            self._parsegroupnames()
 
-    def _parseGroupNames(self):
-        self._groupNames = [attr.content for attr in
-                self._context.xpathEval('//clustat/groups/group[@owner="%s"]/@name' % self._localNode)]
+    def _xpath(self, expression):
+        """Takes an xpath expression and executes it on the current context
+        @type expression: string
+        @return: list
+        """
+        return [ attr.content for attr in self._context.xpathEval(expression) ]
 
-    def checkStatus(self, wantedStatus):
+    def _parsegroupnames(self):
+        """Parses all cluster groups from the prvided clustat xml"""
+        self._groupnames = self._xpath(
+                '//clustat/groups/group[@owner="%s"]/@name' % self._localnode
+                )
 
-        if len(self._groupNames) < 1:
+    def checkstatus(self, wantedstatus):
+        """Checks that the output of clustat corresponds with the wanted
+        status.
+        @type wantedstatus: string
+        @return: Tuple(int, string)
+        """
+        if len(self._groupnames) < 1:
             return (1, "There are no cluster groups to check.")
-        for groupName in self._groupNames:
-            clusterStatus = [attr.content for attr in
-                    self._context.xpathEval('//clustat/groups/group[@name="%s" and @owner="%s"]/@state_str' %
-                        (groupName, self._localNode))]
-            if len(clusterStatus) < 1:
-                return (2, "Could not find cluster group: %s" % groupName)
-            clusterStatus = clusterStatus[0]
-            if wantedStatus != clusterStatus:
+        for groupname in self._groupnames:
+            clusterstatus = self._xpath(
+                    '//clustat/groups/group[@name="%s" and @owner="%s"]' \
+                            '/@state_str' % (groupname, self._localnode)
+                    )
+            if len(clusterstatus) < 1:
+                return (2, "Could not find cluster group: %s" % groupname)
+            clusterstatus = clusterstatus[0]
+            if wantedstatus != clusterstatus:
                 return (3, "Cluster group %s returned state %s" %
-                        (groupName, clusterStatus))
+                        (groupname, clusterstatus))
         return (0, "Everything went better than expected!")
 
 
 error.
 If the script encounters any errors it returns an exit code above 0."""
     parser = OptionParser(
-            usage="usage: %prog [options] [GROUP-NAME [GROUP-NAME [...]]]",
-            description=desc
+            usage = "usage: %prog [options] [GROUP-NAME [GROUP-NAME [...]]]",
+            description = desc
             )
-    parser.add_option("-f", "--file", dest="filename",
-            help="Read xml from file instead of clustat", metavar="FILE")
-    parser.add_option("-e", "--executable", dest="executable",
-            help="Specify path to executable (defaults to /usr/bin/clustat)",
-            default="/usr/bin/clustat", metavar="EXECUTABLE")
-    parser.add_option("-p", "--parameter", dest="parameter",
-            help="What parameters to be passed to executable (defaults to -x)",
-            action="append", metavar="PARAMETER")
+    parser.add_option("-f", "--file", dest = "filename",
+            help = "Read xml from file instead of clustat", metavar = "FILE")
+    parser.add_option("-e", "--executable", dest = "executable",
+            help = "Specify path to executable (defaults to /usr/bin/clustat)",
+            default = "/usr/bin/clustat", metavar = "EXECUTABLE")
+    parser.add_option("-p", "--parameter", dest = "parameter",
+            help = "What parameters to be passed to executable (defaults to -x)",
+            action = "append", metavar = "PARAMETER")
     (options, arguments) = parser.parse_args()
 
     if not options.parameter:
         from subprocess import Popen, PIPE
         command = [options.executable]
         command.extend(options.parameter)
-        readFrom = Popen(command, stdin=None, stdout=PIPE, stderr=PIPE)
-        (stdout, stderr) = readFrom.communicate()
+        readfrom = Popen(command, stdin = None, stdout = PIPE, stderr = PIPE)
+        (stdout, stderr) = readfrom.communicate()
 
-        if stderr:
-            print >> sys.stderr, "Clustat returned the following error:\n%s" % stderr
-            sys.exit(readFrom.returncode)
+        if stderr or readfrom.returncode:
+            print >> sys.stderr, "Clustat returned the following error:\n%s" \
+                    % stderr
+            sys.exit(readfrom.returncode)
 
     clustat = ClustatParser(stdout, arguments)
-    (exitcode, out) = clustat.checkStatus('started')
+    (exitcode, out) = clustat.checkstatus('started')
 
     if (exitcode > 0):
         print >> sys.stderr, out