]> khamidou — GitHub mirrors - grabber.git/commitdiff
got rid of some cruft
authorkhamidou <khamidou@gmail.com>
Wed, 30 Dec 2009 08:50:23 +0000 (09:50 +0100)
committerkhamidou <khamidou@gmail.com>
Wed, 30 Dec 2009 08:50:23 +0000 (09:50 +0100)
grabber

diff --git a/grabber b/grabber
index e157bf546df18eaf76ce33580773a3ab190ecdc7..c063424e0ec78ba5dc079ce0e4e2e76127934b67 100755 (executable)
--- a/grabber
+++ b/grabber
@@ -3,26 +3,47 @@
 import sys
 import fcntl
 import signal
+import glob
+import re
 from os import popen
 
-# Try to acquire the file lock :
+
+lockfile = "/tmp/grabber" + os.getpid()
 
 def bail_out(msg):
        print msg
         sys.exit(-1)
 
-lockfile = "/tmp/grabber" + os.getpid()
+def close_file():
+    fd.close()
+    os.unlink(lockfile)
+
+
+fl = glob.glob("/tmp/grabber*") 
+if  fl != []:
+    # Grabber is already running - it is has been invoked a second time it's because 
+    # the user want to leave automated typing mode.
+    
+    pid = int(re.sub("\D", "", fl[0])) # Get the pid of the original process.
+    os.kill(pid, os.SIGUSR1)
+    sys.exit(0)
+
+# Try to acquire the file lock :
 
 try:
     fd = open(lockfile, "wt")
 except IOError:
-    bail_out("Unable to open /tmp/grabber - exiting")
+    bail_out("Unable to open " + lockfile + " - exiting")
+
+# Create signal handler : 
+def handler():
+    xte_pipe.close()
+    menu_pipe.close()
+    close_file()
+
+# Set the signal handler and a 5-second alarm
+signal.signal(signal.SIGALRM, handler)
 
-try:
-    fcntl.lockf(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
-except IOError:
-    # Grabber is already running - it is has been invoked a second time it's because 
-    # the user want to leave automated grab mode.
 
 try:
     xte_pipe = popen("xte", "w")
@@ -35,11 +56,5 @@ while True:
     for c in menu_pipe.read():
         xte_pipe.write("key " + c + "\n")
 
-# 
-# Unlock the file at the end
-try:
-    fcntl.lockf(fd, fcntl.LOCK_UN)
-except IOError:
-    bail_out("Unable to release lock on file")