Index: tea4cups/trunk/cupsoftee
===================================================================
--- tea4cups/trunk/cupsoftee (revision 568)
+++ tea4cups/trunk/cupsoftee (revision 569)
@@ -36,4 +36,75 @@
         self.debug = 1
         
+    def discoverOtherBackends(self) :    
+        """Discovers the other CUPS backends.
+        
+           Executes each existing backend in turn in device enumeration mode.
+           Returns the list of available backends.
+        """
+        # Unfortunately this method can't output any debug information
+        # to stdout or stderr, else CUPS considers that the device is
+        # not available.
+        available = []
+        (directory, myname) = os.path.split(sys.argv[0])
+        tmpdir = tempfile.gettempdir()
+        lockfilename = os.path.join(tmpdir, "%s..LCK" % myname)
+        if os.path.exists(lockfilename) :
+            lockfile = open(lockfilename, "r")
+            pid = int(lockfile.read())
+            lockfile.close()
+            try :
+                # see if the pid contained in the lock file is still running
+                os.kill(pid, 0)
+            except OSError, e :    
+                if e.errno != errno.EPERM :
+                    # process doesn't exist anymore
+                    os.remove(lockfilename)
+            
+        if not os.path.exists(lockfilename) :
+            lockfile = open(lockfilename, "w")
+            lockfile.write("%i" % os.getpid())
+            lockfile.close()
+            allbackends = [ os.path.join(directory, b) \
+                                for b in os.listdir(directory) 
+                                    if os.access(os.path.join(directory, b), os.X_OK) \
+                                        and (b != myname)] 
+            for backend in allbackends :                            
+                answer = os.popen(backend, "r")
+                try :
+                    devices = [line.strip() for line in answer.readlines()]
+                except :    
+                    devices = []
+                status = answer.close()
+                if status is None :
+                    for d in devices :
+                        # each line is of the form : 
+                        # 'xxxx xxxx "xxxx xxx" "xxxx xxx"'
+                        # so we have to decompose it carefully
+                        fdevice = cStringIO.StringIO(d)
+                        tokenizer = shlex.shlex(fdevice)
+                        tokenizer.wordchars = tokenizer.wordchars + \
+                                                        r".:,?!~/\_$*-+={}[]()#"
+                        arguments = []
+                        while 1 :
+                            token = tokenizer.get_token()
+                            if token :
+                                arguments.append(token)
+                            else :
+                                break
+                        fdevice.close()
+                        try :
+                            (devicetype, device, name, fullname) = arguments
+                        except ValueError :    
+                            pass    # ignore this 'bizarre' device
+                        else :    
+                            if name.startswith('"') and name.endswith('"') :
+                                name = name[1:-1]
+                            if fullname.startswith('"') and fullname.endswith('"') :
+                                fullname = fullname[1:-1]
+                            available.append('%s cupsoftee:%s "CupsOfTee+%s" "CupsOfTee managed %s"' \
+                                                 % (devicetype, device, name, fullname))
+            os.remove(lockfilename)
+        return available
+                        
     def fakePrint(self) :    
         """Fakes to print the job."""
@@ -78,79 +149,4 @@
                     backend)
         
-    def discoverOtherBackends(self) :    
-        """Discovers the other CUPS backends.
-        
-           Executes each existing backend in turn in device enumeration mode.
-           Returns the list of available backends.
-        """
-        self.logDebug("Entering device enumeration loop.")
-        available = []
-        tmpdir = tempfile.gettempdir()
-        lockfilename = os.path.join(tmpdir, "cupsoftee..LCK")
-        if os.path.exists(lockfilename) :
-            self.logInfo("Lock file present.")
-            locked = 1
-            lockfile = open(lockfilename, "r")
-            pid = int(lockfile.read())
-            lockfile.close()
-            try :
-                # see if the pid contained in the lock file is still running
-                os.kill(pid, 0)
-            except OSError, e :    
-                if e.errno != errno.EPERM :
-                    # process doesn't exist anymore
-                    self.logInfo("Removing stalled lock.")
-                    os.remove(lockfilename)
-                    locked = 0
-            if locked :        
-                self.logInfo("No device enumeration.")
-            
-        if not os.path.exists(lockfilename) :
-            lockfile = open(lockfilename, "w")
-            lockfile.write("%i" % os.getpid())
-            lockfile.close()
-            (directory, myname) = os.path.split(sys.argv[0])
-            for backend in [ os.path.join(directory, b) \
-                                 for b in os.listdir(directory) 
-                                     if os.path.isfile(os.path.join(directory, b))\
-                                        and (b != myname)] :
-                answer = os.popen(backend, "r")
-                try :
-                    devices = [line.strip() for line in answer.readlines()]
-                except :    
-                    devices = []
-                status = answer.close()
-                if status is None :
-                    for d in devices :
-                        # each line is of the form : 
-                        # 'xxxx xxxx "xxxx xxx" "xxxx xxx"'
-                        # so we have to decompose it carefully
-                        fdevice = cStringIO.StringIO(d)
-                        tokenizer = shlex.shlex(fdevice)
-                        tokenizer.wordchars = tokenizer.wordchars + \
-                                                        r".:,?!~/\_$*-+={}[]()#"
-                        arguments = []
-                        while 1 :
-                            token = tokenizer.get_token()
-                            if token :
-                                arguments.append(token)
-                            else :
-                                break
-                        fdevice.close()
-                        try :
-                            (devicetype, device, name, fullname) = arguments
-                        except ValueError :    
-                            pass    # ignore this 'bizarre' device
-                        else :    
-                            if name.startswith('"') and name.endswith('"') :
-                                name = name[1:-1]
-                            if fullname.startswith('"') and fullname.endswith('"') :
-                                fullname = fullname[1:-1]
-                            available.append('%s cupsoftee:%s "CupsOfTee+%s" "CupsOfTee managed %s"' \
-                                                 % (devicetype, device, name, fullname))
-            os.remove(lockfilename)
-        self.logDebug("Exiting device enumeration loop.")
-        return available
-                        
     def logDebug(self, message) :    
         """Logs something to debug output if debug is enabled."""
@@ -163,5 +159,4 @@
         sys.stderr.write("%s: %s\n" % (level.upper(), message))
         sys.stderr.flush()
-        
 
 if __name__ == "__main__" :    
