Index: pykota/trunk/bin/cupspykota
===================================================================
--- pykota/trunk/bin/cupspykota (revision 2147)
+++ pykota/trunk/bin/cupspykota (revision 2164)
@@ -27,4 +27,6 @@
 import sys
 import os
+import errno
+import tempfile
 import popen2
 import cStringIO
@@ -638,39 +640,59 @@
     retcode = 0
     if len(sys.argv) == 1 :
-        # we will execute each existing backend in device enumeration mode
-        # and generate their PyKota accounting counterpart
         (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")
+        tmpdir = tempfile.gettempdir()
+        lockfilename = os.path.join(tmpdir, "%s..LCK" % myname)
+        if os.path.exists(lockfilename) :
+            # there's already a lockfile, see if still used
+            lockfile = open(lockfilename, "r")
+            pid = int(lockfile.read())
+            lockfile.close()
             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("%s" % 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]
-                        print '%s cupspykota:%s "PyKota+%s" "PyKota managed %s"' % (devicetype, device, name, fullname)
+                # 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, remove the lock
+                    os.remove(lockfilename)
+            
+        if not os.path.exists(lockfilename) :
+            lockfile = open(lockfilename, "w")
+            lockfile.write("%i" % os.getpid())
+            lockfile.close()
+            # we will execute each existing backend in device enumeration mode
+            # and generate their PyKota accounting counterpart
+            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("%s" % 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]
+                            print '%s cupspykota:%s "PyKota+%s" "PyKota managed %s"' % (devicetype, device, name, fullname)
+            os.remove(lockfilename)
         retcode = 0                
     elif len(sys.argv) not in (6, 7) :    
