44d1dcb
From 674eac235a72565075129f125e3aefdecb033a05 Mon Sep 17 00:00:00 2001
44d1dcb
From: Mark Reynolds <mreynolds@redhat.com>
44d1dcb
Date: Wed, 8 Jul 2015 14:25:04 -0400
44d1dcb
Subject: Ticket 48204 - Add Python 3 compatibility to ds-logpipe
44d1dcb
44d1dcb
From: Petr Viktorin <pviktori@redhat.com>
44d1dcb
44d1dcb
Description:
44d1dcb
- Use 'as' syntax when catching exceptions
44d1dcb
- Use 0o... syntax for octal literals
44d1dcb
- Don't use unbuffered text files in Python 3
44d1dcb
44d1dcb
https://fedorahosted.org/389/ticket/48204
44d1dcb
44d1dcb
Reviewed by: mreynolds
44d1dcb
44d1dcb
diff --git a/ldap/admin/src/scripts/ds-logpipe.py b/ldap/admin/src/scripts/ds-logpipe.py
44d1dcb
index b2d8304..ca6c27f 100644
44d1dcb
--- a/ldap/admin/src/scripts/ds-logpipe.py
44d1dcb
+++ b/ldap/admin/src/scripts/ds-logpipe.py
44d1dcb
@@ -1,5 +1,7 @@
44d1dcb
 #!/usr/bin/env python
44d1dcb
 
44d1dcb
+from __future__ import print_function
44d1dcb
+
44d1dcb
 import sys
44d1dcb
 import os, os.path
44d1dcb
 import errno
44d1dcb
@@ -11,7 +13,7 @@ import fcntl
44d1dcb
 import pwd
44d1dcb
 
44d1dcb
 maxlines = 1000 # set on command line
44d1dcb
-S_IFIFO = 0010000
44d1dcb
+S_IFIFO = 0o010000
44d1dcb
 
44d1dcb
 buffer = [] # default circular buffer used by default plugin
44d1dcb
 totallines = 0
44d1dcb
@@ -29,8 +31,8 @@ def defaultplugin(line):
44d1dcb
 
44d1dcb
 def printbuffer():
44d1dcb
     sys.stdout.writelines(buffer)
44d1dcb
-    print "Read %d total lines" % totallines
44d1dcb
-    print logfname, "=" * 60
44d1dcb
+    print("Read %d total lines" % totallines)
44d1dcb
+    print(logfname, "=" * 60)
44d1dcb
     sys.stdout.flush()
44d1dcb
 
44d1dcb
 def defaultpost(): printbuffer()
44d1dcb
@@ -51,7 +53,7 @@ def sighandler(signum, frame):
44d1dcb
         signal.signal(signal.SIGTERM, signal.SIG_DFL)
44d1dcb
         signal.signal(signal.SIGALRM, signal.SIG_DFL)
44d1dcb
         if signum == signal.SIGALRM and debug:
44d1dcb
-            print "script timed out waiting to open pipe"
44d1dcb
+            print("script timed out waiting to open pipe")
44d1dcb
         finish()
44d1dcb
     else: printbuffer()
44d1dcb
 
44d1dcb
@@ -126,7 +128,7 @@ def parse_plugins(parser, options, args):
44d1dcb
                 newargs.append(arg)
44d1dcb
         if prefunc:
44d1dcb
             if debug:
44d1dcb
-                print 'Calling "pre" function in', plgfile
44d1dcb
+                print('Calling "pre" function in', plgfile)
44d1dcb
             if not prefunc(bvals):
44d1dcb
                 parser.error('the "pre" function in %s returned an error' % plgfile)
44d1dcb
         args = newargs
44d1dcb
@@ -140,27 +142,27 @@ def open_pipe(logfname):
44d1dcb
         try:
44d1dcb
             logf = open(logfname, 'r') # blocks until there is some input
44d1dcb
             opencompleted = True
44d1dcb
-        except IOError, e:
44d1dcb
+        except IOError as e:
44d1dcb
             if e.errno == errno.EINTR:
44d1dcb
                 continue # open was interrupted, try again
44d1dcb
             else: # hard error
44d1dcb
-                raise Exception, "%s [%d]" % (e.strerror, e.errno)
44d1dcb
+                raise Exception("%s [%d]" % (e.strerror, e.errno))
44d1dcb
     return logf
44d1dcb
 
44d1dcb
 def is_proc_alive(procpid):
44d1dcb
     retval = False
44d1dcb
     try:
44d1dcb
         retval = os.path.exists("/proc/%d" % procpid)
44d1dcb
-    except IOError, e:
44d1dcb
+    except IOError as e:
44d1dcb
         if e.errno != errno.ENOENT: # may not exist yet - that's ok
44d1dcb
             # otherwise, probably permissions or other badness
44d1dcb
-            raise Exception, "could not open file %s - %s [%d]" % (procfile, e.strerror, e.errno)
44d1dcb
+            raise Exception("could not open file %s - %s [%d]" % (procfile, e.strerror, e.errno))
44d1dcb
     # using /proc/pid failed, try kill
44d1dcb
     if not retval:
44d1dcb
         try:
44d1dcb
             os.kill(procpid, 0) # sig 0 is a "ping"
44d1dcb
             retval = True # if we got here, proc exists
44d1dcb
-        except OSError, e:
44d1dcb
+        except OSError as e:
44d1dcb
             pass # no such process, or EPERM/EACCES
44d1dcb
     return retval
44d1dcb
 
44d1dcb
@@ -172,10 +174,10 @@ def get_pid_from_file(pidfile):
44d1dcb
             pfd = open(pidfile, 'r')
44d1dcb
             line = pfd.readline()
44d1dcb
             pfd.close()
44d1dcb
-        except IOError, e:
44d1dcb
+        except IOError as e:
44d1dcb
             if e.errno != errno.ENOENT: # may not exist yet - that's ok
44d1dcb
                 # otherwise, probably permissions or other badness
44d1dcb
-                raise Exception, "Could not read pid from file %s - %s [%d]" % (pidfile, e.strerror, e.errno)
44d1dcb
+                raise Exception("Could not read pid from file %s - %s [%d]" % (pidfile, e.strerror, e.errno))
44d1dcb
         if line:
44d1dcb
             procpid = int(line)
44d1dcb
     return procpid
44d1dcb
@@ -185,8 +187,8 @@ def write_pid_file(pidfile):
44d1dcb
         pfd = open(pidfile, 'w')
44d1dcb
         pfd.write("%d\n" % os.getpid())
44d1dcb
         pfd.close()
44d1dcb
-    except IOError, e:
44d1dcb
-        raise Exception, "Could not write pid to file %s - %s [%d]" % (pidfile, e.strerror, e.errno)
44d1dcb
+    except IOError as e:
44d1dcb
+        raise Exception("Could not write pid to file %s - %s [%d]" % (pidfile, e.strerror, e.errno))
44d1dcb
 
44d1dcb
 def handle_script_pidfile(scriptpidfile):
44d1dcb
     scriptpid = get_pid_from_file(scriptpidfile)
44d1dcb
@@ -194,7 +196,7 @@ def handle_script_pidfile(scriptpidfile):
44d1dcb
     if scriptpid and is_proc_alive(scriptpid):
44d1dcb
         # already running
44d1dcb
         if debug:
44d1dcb
-            print "Script is already running: process id %d" % scriptpid
44d1dcb
+            print("Script is already running: process id %d" % scriptpid)
44d1dcb
         return False
44d1dcb
     else:
44d1dcb
         # either process is not running or no file
44d1dcb
@@ -210,15 +212,15 @@ def read_and_process_line(logf, plgfuncs):
44d1dcb
         try:
44d1dcb
             line = logf.readline()
44d1dcb
             readcompleted = True # read completed
44d1dcb
-        except IOError, e:
44d1dcb
+        except IOError as e:
44d1dcb
             if e.errno == errno.EINTR:
44d1dcb
                 continue # read was interrupted, try again
44d1dcb
             else: # hard error
44d1dcb
-                raise Exception, "%s [%d]" % (e.strerror, e.errno)
44d1dcb
+                raise Exception("%s [%d]" % (e.strerror, e.errno))
44d1dcb
     if line: # read something
44d1dcb
         for plgfunc in plgfuncs:
44d1dcb
             if not plgfunc(line):
44d1dcb
-                print "Aborting processing due to function %s.%s" % (plgfunc.__module__, plgfunc.__name__)
44d1dcb
+                print("Aborting processing due to function %s.%s" % (plgfunc.__module__, plgfunc.__name__))
44d1dcb
                 finish() # this will exit the process
44d1dcb
                 done = True
44d1dcb
                 break
44d1dcb
@@ -281,28 +283,28 @@ if options.scriptpidfile:
44d1dcb
 serverpid = options.serverpid
44d1dcb
 if serverpid:
44d1dcb
     if not is_proc_alive(serverpid):
44d1dcb
-        print "Server pid [%d] is not alive - exiting" % serverpid
44d1dcb
+        print("Server pid [%d] is not alive - exiting" % serverpid)
44d1dcb
         sys.exit(1)
44d1dcb
 
44d1dcb
 try:
44d1dcb
     if os.stat(logfname).st_mode & S_IFIFO:
44d1dcb
         if debug:
44d1dcb
-            print "Using existing log pipe", logfname
44d1dcb
+            print("Using existing log pipe", logfname)
44d1dcb
     else:
44d1dcb
-        print "Error:", logfname, "exists and is not a log pipe"
44d1dcb
-        print "use a filename other than", logfname
44d1dcb
+        print("Error:", logfname, "exists and is not a log pipe")
44d1dcb
+        print("use a filename other than", logfname)
44d1dcb
         sys.exit(1)
44d1dcb
-except OSError, e:
44d1dcb
+except OSError as e:
44d1dcb
     if e.errno == errno.ENOENT:
44d1dcb
         if debug:
44d1dcb
-            print "Creating log pipe", logfname
44d1dcb
+            print("Creating log pipe", logfname)
44d1dcb
         os.mkfifo(logfname)
44d1dcb
-        os.chmod(logfname, 0600)
44d1dcb
+        os.chmod(logfname, 0o600)
44d1dcb
     else:
44d1dcb
-        raise Exception, "%s [%d]" % (e.strerror, e.errno)
44d1dcb
+        raise Exception("%s [%d]" % (e.strerror, e.errno))
44d1dcb
 
44d1dcb
 if debug:
44d1dcb
-    print "Listening to log pipe", logfname, "number of lines", maxlines
44d1dcb
+    print("Listening to log pipe", logfname, "number of lines", maxlines)
44d1dcb
 
44d1dcb
 # set up our signal handlers
44d1dcb
 signal.signal(signal.SIGHUP, sighandler)
44d1dcb
@@ -333,14 +335,14 @@ while not done:
44d1dcb
     logf = open_pipe(logfname)
44d1dcb
     # if we get here, logf is not None
44d1dcb
     if debug:
44d1dcb
-        print "opened pipe", logf
44d1dcb
+        print("opened pipe", logf)
44d1dcb
 
44d1dcb
     if timerisset:
44d1dcb
         # cancel the timer - the open succeeded
44d1dcb
         timerisset = False
44d1dcb
         signal.setitimer(signal.ITIMER_REAL, 0)
44d1dcb
         if debug:
44d1dcb
-            print "cancelled startup timer"
44d1dcb
+            print("cancelled startup timer")
44d1dcb
 
44d1dcb
     lines = 0
44d1dcb
     # read and process the next line in the pipe
44d1dcb
@@ -352,11 +354,11 @@ while not done:
44d1dcb
 
44d1dcb
     # the other end of the pipe closed - we close our end too
44d1dcb
     if debug:
44d1dcb
-        print "read", lines, "lines"
44d1dcb
+        print("read", lines, "lines")
44d1dcb
     logf.close()
44d1dcb
     logf = None
44d1dcb
     if debug:
44d1dcb
-        print "closed log pipe", logfname
44d1dcb
+        print("closed log pipe", logfname)
44d1dcb
 
44d1dcb
     if not serverpid and options.serverpidfile:
44d1dcb
         # see if the server has written its server pid file yet
44d1dcb
@@ -368,7 +370,7 @@ while not done:
44d1dcb
     if serverpid and not is_proc_alive(serverpid):
44d1dcb
         done = True
44d1dcb
         if debug:
44d1dcb
-            print "server pid", serverpid, "exited - script exiting"
44d1dcb
+            print("server pid", serverpid, "exited - script exiting")
44d1dcb
 
44d1dcb
     if neverdone:
44d1dcb
         done = False
44d1dcb
@@ -387,12 +389,12 @@ while not done:
44d1dcb
             signal.setitimer(signal.ITIMER_REAL, 0.25)
44d1dcb
             timerisset = True
44d1dcb
             if debug:
44d1dcb
-                print "set startup timer - see if server is really shut down"
44d1dcb
+                print("set startup timer - see if server is really shut down")
44d1dcb
         else: # we read something
44d1dcb
             # pipe closed - usually when server shuts down
44d1dcb
             done = True
44d1dcb
             
44d1dcb
     if not done and debug:
44d1dcb
-        print "log pipe", logfname, "closed - reopening - read", totallines, "total lines"
44d1dcb
+        print("log pipe", logfname, "closed - reopening - read", totallines, "total lines")
44d1dcb
 
44d1dcb
 finish()
44d1dcb
diff --git a/ldap/admin/src/scripts/failedbinds.py b/ldap/admin/src/scripts/failedbinds.py
44d1dcb
index 8afe0ff..23a7bea 100644
44d1dcb
--- a/ldap/admin/src/scripts/failedbinds.py
44d1dcb
+++ b/ldap/admin/src/scripts/failedbinds.py
44d1dcb
@@ -1,4 +1,5 @@
44d1dcb
 import re
44d1dcb
+import sys
44d1dcb
 import os, os.path
44d1dcb
 
44d1dcb
 # regex that matches a BIND request line
44d1dcb
@@ -91,12 +92,15 @@ def pre(plgargs):
44d1dcb
     global logf
44d1dcb
     logfile = plgargs.get('logfile', None)
44d1dcb
     if not logfile:
44d1dcb
-        print "Error: missing required argument failedbinds.logfile"
44d1dcb
+        print("Error: missing required argument failedbinds.logfile")
44d1dcb
         return False
44d1dcb
     needchmod = False
44d1dcb
     if not os.path.isfile(logfile): needchmod = True
44d1dcb
-    logf = open(logfile, 'a', 0) # 0 for unbuffered output
44d1dcb
-    if needchmod: os.chmod(logfile, 0600)
44d1dcb
+    if sys.version_info < (3, 0):
44d1dcb
+        logf = open(logfile, 'a', 0) # 0 for unbuffered output
44d1dcb
+    else:
44d1dcb
+        logf = open(logfile, 'a')
44d1dcb
+    if needchmod: os.chmod(logfile, 0o600)
44d1dcb
     return True
44d1dcb
 
44d1dcb
 def post():
44d1dcb
@@ -153,6 +157,7 @@ def plugin(line):
44d1dcb
         logmsg = conn.addreq(timestamp, opnum, dn, method, mech)
44d1dcb
         if logmsg:
44d1dcb
             logf.write(logmsg + "\n")
44d1dcb
+            logf.flush()
44d1dcb
         return True
44d1dcb
 
44d1dcb
     # is this a RESULT line?
44d1dcb
@@ -164,6 +169,7 @@ def plugin(line):
44d1dcb
         logmsg = conn.addres(timestamp, opnum, errnum)
44d1dcb
         if logmsg:
44d1dcb
             logf.write(logmsg + "\n")
44d1dcb
+            logf.flush()
44d1dcb
         return True
44d1dcb
 
44d1dcb
     return True # no match
44d1dcb
diff --git a/ldap/admin/src/scripts/logregex.py b/ldap/admin/src/scripts/logregex.py
44d1dcb
index 7537953..8b1f87f 100644
44d1dcb
--- a/ldap/admin/src/scripts/logregex.py
44d1dcb
+++ b/ldap/admin/src/scripts/logregex.py
44d1dcb
@@ -10,7 +10,7 @@ def pre(plgargs):
44d1dcb
     global regex_regex_ary
44d1dcb
     regexary = plgargs.get('regex', None)
44d1dcb
     if not regexary:
44d1dcb
-        print "Error: missing required argument logregex.regex"
44d1dcb
+        print("Error: missing required argument logregex.regex")
44d1dcb
         return False
44d1dcb
     if isinstance(regexary,list):
44d1dcb
         regex_regex_ary = [re.compile(xx) for xx in regexary]
44d1dcb
-- 
44d1dcb
cgit v0.10.2
44d1dcb