Index: /tea4cups/trunk/tea4cups
===================================================================
--- /tea4cups/trunk/tea4cups (revision 688)
+++ /tea4cups/trunk/tea4cups (revision 690)
@@ -87,5 +87,5 @@
 from struct import pack, unpack
 
-__version__ = "3.12_unofficial"
+__version__ = "3.13alpha_unofficial"
 
 class TeeError(Exception):
@@ -914,5 +914,4 @@
     def waitForLock(self) :    
         """Waits until we can acquire the lock file."""
-        self.logDebug("Waiting for lock...")
         lockfilename = self.DeviceURI.replace("/", ".")
         lockfilename = lockfilename.replace(":", ".")
@@ -921,27 +920,29 @@
         lockfilename = lockfilename.replace("@", ".")
         lockfilename = os.path.join(self.Directory, "%s-%s..LCK" % (self.myname, lockfilename))
-        stillrunning = True
-        while 1 :
-            self.LockFile = open(lockfilename, "a+")
-            fcntl.lockf(self.LockFile, fcntl.LOCK_EX) # Will wait until lock available
-            self.LockFile.seek(0, 0)      
-            try : 
-                oldpid = int(self.LockFile.readlines()[-1].strip())
-            except :    
-                stillrunning = False
-            else :    
-                try :
-                    os.kill(oldpid, 0)
-                except OSError :    
-                    stillrunning = False
-            if not stillrunning :        
+        self.logDebug("Waiting for lock %s to become available..." % lockfilename)
+        haslock = False
+        while not haslock :
+            try :
+                # open the lock file, optionally creating it if needed.
+                self.LockFile = open(lockfilename, "a+")
+                
+                # we wait indefinitely for the lock to become available.
+                # works over NFS too.
+                fcntl.lockf(self.LockFile, fcntl.LOCK_EX)
+                haslock = True
+                
+                self.logDebug("Lock %s acquired." % lockfilename)
+                
+                # Here we save the PID in the lock file, but we don't use
+                # it, because the lock file may be in a directory shared
+                # over NFS between two (or more) print servers, so the PID
+                # has no meaning in this case.
                 self.LockFile.truncate(0)
                 self.LockFile.seek(0, 0)
-                self.LockFile.write("%s\n" % self.pid)
+                self.LockFile.write(str(self.pid))
                 self.LockFile.flush()
-                break
-            else :    
-                time.sleep(0.1)
-        self.logDebug("Lock acquired.")
+            except IOError :            
+                self.logDebug("I/O Error while waiting for lock %s" % lockfilename)
+                time.sleep(0.25)
         
     def readConfig(self) :
@@ -1268,13 +1269,14 @@
             except OSError, msg :    
                 self.logInfo("Problem when removing %s : %s" % (self.DataFile, msg), "error")
-        self.logDebug("Removing lock...")
-        try :
-            fcntl.flock(self.LockFile, fcntl.LOCK_UN)
-            self.LockFile.close()
-            # NB : we don't remove the lock file, since it might already be opened by another process.
-        except :    
-            self.logInfo("Problem while removing lock.", "error")
-        else :    
-            self.logDebug("Lock removed.")
+                
+        if self.LockFile is not None :
+            self.logDebug("Removing lock...")
+            try :
+                fcntl.lockf(self.LockFile, fcntl.LOCK_UN)
+                self.LockFile.close()
+            except :    
+                self.logInfo("Problem while unlocking.", "error")
+            else :    
+                self.logDebug("Lock removed.")
         self.logDebug("Clean.")
 
Index: /tea4cups/trunk/NEWS
===================================================================
--- /tea4cups/trunk/NEWS (revision 688)
+++ /tea4cups/trunk/NEWS (revision 690)
@@ -23,4 +23,8 @@
 Tea4CUPS News :
 
+  * 3.13alpha (2006-11-07) :
+  
+    - Backported locking mechanism from PyKota.
+    
   * 3.12 (2006-08-11) :
   
