Description: update python scripts for python3
Author: Valentin Vidic <Valentin.Vidic@CARNet.hr>
Last-Update: 2018-04-05
---
This patch header follows DEP-3: http://dep.debian.net/deps/dep3/
--- a/gfs2/scripts/gfs2_lockcapture
+++ b/gfs2/scripts/gfs2_lockcapture
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/python3
 """
 The script "gfs2_lockcapture" will capture locking information from GFS2 file
 systems and DLM.
@@ -75,7 +75,7 @@
         """
         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 @@
         """
         # 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 @@
         @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 @@
             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 @@
             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 @@
         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 @@
             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 @@
         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 @@
             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 @@
             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 @@
             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)
--- a/gfs2/scripts/gfs2_trace
+++ b/gfs2/scripts/gfs2_trace
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/python3
 """
 This script will enable or disable trace events for GFS2. The script can capture
 trace events and write the trace events captured to a file.
@@ -119,7 +119,7 @@
                 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
@@ -319,7 +319,7 @@
         @return: Returns a list of all the trace event names found.
         @rtype: Array
         """
-        return self.__traceEventsMap.keys()
+        return list(self.__traceEventsMap.keys())
 
     def getTraceEvent(self, traceEventName):
         """
@@ -330,7 +330,7 @@
         match is found then None is returned.
         @rtype: TraceEvent
         """
-        if (self.__traceEventsMap.has_key(traceEventName)):
+        if (traceEventName in self.__traceEventsMap):
             return self.__traceEventsMap.get(traceEventName)
         return None
 
@@ -555,7 +555,7 @@
         exampleMessage += "# %s -N -e gfs2_demote_rq,gfs2_glock_state_change,gfs2_promote -c /tmp/gfs2_trace.log\n" %(self.__commandName)
         self.print_version()
         OptionParser.print_help(self)
-        print exampleMessage
+        print(exampleMessage)
 
 class ExtendOption (Option):
         """
@@ -699,9 +699,9 @@
             # Disable logging to console except for debug when we print into information to console.
             logging.disable(logging.CRITICAL)
             if (len(traceEventsString) > 0):
-                print "trace event name           trace event status"
-                print "----------------           ------------------"
-                print traceEventsString.rstrip()
+                print("trace event name           trace event status")
+                print("----------------           ------------------")
+                print(traceEventsString.rstrip())
             exitScript()
         # #######################################################################
         # Enable or Disable Trace Events
@@ -758,7 +758,7 @@
                 fout.close()
                 message = "The data was written to this file: %s" %(cmdLineOpts.pathToOutputFilename)
                 logging.getLogger(MAIN_LOGGER_NAME).info(message)
-            except UnicodeEncodeError, e:
+            except UnicodeEncodeError as e:
                 message = "There was a unicode encode error writing to the file: %s." %(cmdLineOpts.pathToOutputFilename)
                 logging.getLogger(MAIN_LOGGER_NAME).error(message)
             except IOError:
@@ -780,7 +780,7 @@
                 message = "There was an error creating the tarfile: %s." %(pathToTarFilename)
                 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()
