[Cluster-devel] [PATCH 2/2] gfs2_lockcapture: update for python3

Valentin Vidic Valentin.Vidic at CARNet.hr
Fri Apr 6 07:58:56 UTC 2018


---
 gfs2/scripts/gfs2_lockcapture | 32 ++++++++++++++++++--------------
 1 file changed, 18 insertions(+), 14 deletions(-)

diff --git a/gfs2/scripts/gfs2_lockcapture b/gfs2/scripts/gfs2_lockcapture
index 5fe92bc3..424499e6 100644
--- a/gfs2/scripts/gfs2_lockcapture
+++ b/gfs2/scripts/gfs2_lockcapture
@@ -75,7 +75,7 @@ class ClusterNode:
         """
         rString = ""
         rString += "%s:%s(id:%d)" %(self.getClusterName(), self.getClusterNodeName(), self.getClusterNodeID())
-        fsLabels = self.__mapOfMountedFilesystemLabels.keys()
+        fsLabels = list(self.__mapOfMountedFilesystemLabels.keys())
         fsLabels.sort()
         for fsLabel in fsLabels:
             rString += "\n\t%s --> %s" %(fsLabel, self.__mapOfMountedFilesystemLabels.get(fsLabel))
@@ -126,10 +126,10 @@ class ClusterNode:
         """
         # If true will prepend the cluster name to gfs2 fs name
         if (includeClusterName):
-            return self.__mapOfMountedFilesystemLabels.keys()
+            return list(self.__mapOfMountedFilesystemLabels.keys())
         else:
             listOfGFS2MountedFilesystemLabels = []
-            for fsLabel in self.__mapOfMountedFilesystemLabels.keys():
+            for fsLabel in list(self.__mapOfMountedFilesystemLabels.keys()):
                 fsLabelSplit = fsLabel.split(":", 1)
                 if (len(fsLabelSplit) == 2):
                     listOfGFS2MountedFilesystemLabels.append(fsLabelSplit[1])
@@ -146,7 +146,7 @@ class ClusterNode:
         @rtype: Map
         """
         mapOfGFS2MountedFilesystemPaths = {}
-        for fsLabel in self.__mapOfMountedFilesystemLabels.keys():
+        for fsLabel in list(self.__mapOfMountedFilesystemLabels.keys()):
             value = self.__mapOfMountedFilesystemLabels.get(fsLabel)
             mountPoint = value.split("type", 1)[0].split("on")[1]
             if (len(mountPoint) > 0):
@@ -238,7 +238,7 @@ def runCommandOutput(command, listOfCommandOptions, standardOut=subprocess.PIPE,
             message += "\n%s" %(stderr.rstrip())
         logging.getLogger(MAIN_LOGGER_NAME).error(message)
         return None
-    return stdout.strip().rstrip()
+    return stdout.decode().strip().rstrip()
 
 def writeToFile(pathToFilename, data, appendToFile=True, createFile=False):
     """
@@ -271,7 +271,7 @@ def writeToFile(pathToFilename, data, appendToFile=True, createFile=False):
             fout.write(data + "\n")
             fout.close()
             return True
-        except UnicodeEncodeError, e:
+        except UnicodeEncodeError as e:
             message = "There was a unicode encode error writing to the file: %s." %(pathToFilename)
             logging.getLogger(MAIN_LOGGER_NAME).error(message)
             return False
@@ -638,13 +638,13 @@ def getClusterNode(listOfGFS2Names):
         mapOfMountedFilesystemLabels = getLabelMapForMountedFilesystems(clusterName, getMountedGFS2Filesystems())
         # These will be the GFS2 filesystems that will have their lockdump information gathered.
         if (len(listOfGFS2Names) > 0):
-            for label in mapOfMountedFilesystemLabels.keys():
+            for label in list(mapOfMountedFilesystemLabels.keys()):
                 foundMatch = False
                 for gfs2FSName in listOfGFS2Names:
                     if ((gfs2FSName == label) or ("%s:%s"%(clusterName, gfs2FSName) == label)):
                         foundMatch = True
                         break
-                if ((not foundMatch) and (mapOfMountedFilesystemLabels.has_key(label))):
+                if ((not foundMatch) and (label in mapOfMountedFilesystemLabels)):
                     del(mapOfMountedFilesystemLabels[label])
         # Cast the node id to an int, and default is 0 if node is not found or
         # not castable.
@@ -680,7 +680,7 @@ def parse_dlm_ls(dlm_ls):
             dlmToolLSMap = dict.fromkeys(dlmToolLSKeys)
             lines = section.split("\n")
             for line in lines:
-                for dlmToolLSKey in dlmToolLSMap.keys():
+                for dlmToolLSKey in list(dlmToolLSMap.keys()):
                     if (line.startswith(dlmToolLSKey)):
                         value = line.replace(dlmToolLSKey, " ", 1).strip().rstrip()
                         dlmToolLSMap[dlmToolLSKey] = value
@@ -1181,7 +1181,7 @@ class OptionParserExtended(OptionParser):
         examplesMessage += "\nthe data collected in the output directory: /tmp and all the questions will be answered with yes.\n"
         examplesMessage += "\n# %s -r 2 -s 25 -P -o /tmp\n" %(self.__commandName)
         OptionParser.print_help(self)
-        print examplesMessage
+        print(examplesMessage)
 
 class ExtendOption (Option):
     """
@@ -1316,8 +1316,8 @@ if __name__ == "__main__":
             logging.getLogger(MAIN_LOGGER_NAME).error(message)
         if (cmdLineOpts.enablePrintInfo):
             logging.disable(logging.CRITICAL)
-            print "List of all the mounted GFS2 filesystems that can have their lockdump data captured:"
-            print clusternode
+            print("List of all the mounted GFS2 filesystems that can have their lockdump data captured:")
+            print(clusternode)
             exitScript()
         # #######################################################################
         # Verify they want to continue because this script will trigger sysrq events.
@@ -1328,7 +1328,11 @@ if __name__ == "__main__":
             prompt = " [y/n] "
             while True:
                 sys.stdout.write(question + prompt)
-                choice = raw_input().lower()
+                try: # python2 compatible input
+                    input = raw_input
+                except NameError:
+                    pass
+                choice = input().lower()
                 if (choice in valid):
                     if (valid.get(choice)):
                         # If yes, or y then exit loop and continue.
@@ -1508,7 +1512,7 @@ if __name__ == "__main__":
             logging.getLogger(MAIN_LOGGER_NAME).error(message)
         # #######################################################################
     except KeyboardInterrupt:
-        print ""
+        print("")
         message =  "This script will exit since control-c was executed by end user."
         logging.getLogger(MAIN_LOGGER_NAME).error(message)
         exitScript(errorCode=1)
-- 
2.17.0




More information about the Cluster-devel mailing list