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