3
3
# Wrapper on pyinotify for running commands
4
4
# (c) 2009 Peter Bengtsson, [email protected]
5
5
#
6
- # TODO: the lock file mechanism doesn't work! threading?
7
- #
6
+ # TODO: Ok, now it does not start a command while another is runnnig
7
+ # But! then what if you actually wanted to test a modification you
8
+ # saved while running another test
9
+ # Yes, we could stop the running command and replace it by the new test
10
+ # But! django tests will complain that a test db is already here
8
11
9
12
import os
10
13
14
+ from subprocess import Popen
15
+ from threading import Lock , Thread
16
+
11
17
__version__ = '1.6'
12
-
13
- # Prepare a lock file
14
- from tempfile import gettempdir
15
- LOCK_FILE = os .path .join (gettempdir (), 'gorunning.lock' )
16
18
17
19
class SettingsClass (object ):
18
20
VERBOSE = False
@@ -62,6 +64,11 @@ def _ignore_file(path):
62
64
return True
63
65
64
66
class PTmp (ProcessEvent ):
67
+
68
+ def __init__ (self ):
69
+ super (PTmp , self ).__init__ ()
70
+ self .lock = Lock ()
71
+
65
72
def process_IN_CREATE (self , event ):
66
73
if os .path .basename (event .pathname ).startswith ('.#' ):
67
74
# backup file
@@ -76,23 +83,33 @@ def process_IN_CREATE(self, event):
76
83
def process_IN_MODIFY (self , event ):
77
84
if _ignore_file (event .pathname ):
78
85
return
79
-
80
- if os .path .isfile (LOCK_FILE ):
81
- # command is still running
82
- return
83
-
84
- print "Modifying:" , event .pathname
85
- command = _find_command (event .pathname )
86
- if command :
87
- if settings .VERBOSE :
88
- print "Command: " ,
89
- print command
90
- open (LOCK_FILE , 'w' ).write ("Running command\n \n %s\n " % command )
91
- os .system (command )
92
- if os .path .isfile (LOCK_FILE ):
93
- os .remove (LOCK_FILE )
94
- print "Waiting for stuff to happen again..."
95
-
86
+
87
+ def execute_command (event , lock ):
88
+ # By default trying to acquire a lock is blocking
89
+ # In this case it will create a queue of commands to run
90
+ #
91
+ # If you try to acquire the lock in the locked state non-blocking
92
+ # style, it will immediatly returns False and you know that a
93
+ # command is already running, and in this case we don't want to run
94
+ # this command at all.
95
+ block = settings .RUN_ON_EVERY_EVENT
96
+ if not lock .acquire (block ):
97
+ # in this case we just want to not execute the command
98
+ return
99
+ print "Modifying:" , event .pathname
100
+ command = _find_command (event .pathname )
101
+ if command :
102
+ if settings .VERBOSE :
103
+ print "Command: " ,
104
+ print command
105
+ p = Popen (command , shell = True )
106
+ sts = os .waitpid (p .pid , 0 )
107
+ lock .release ()
108
+
109
+ command_thread = Thread (target = execute_command , args = [event , self .lock ])
110
+ command_thread .start ()
111
+
112
+
96
113
def start (actual_directories ):
97
114
98
115
wm = WatchManager ()
@@ -175,6 +192,7 @@ def configure_more(directories):
175
192
settings .VERBOSE = getattr (x , 'VERBOSE' , settings .VERBOSE )
176
193
settings .IGNORE_EXTENSIONS = getattr (x , 'IGNORE_EXTENSIONS' , tuple ())
177
194
settings .IGNORE_DIRECTORIES = getattr (x , 'IGNORE_DIRECTORIES' , tuple ())
195
+ settings .RUN_ON_EVERY_EVENT = getattr (x , 'RUN_ON_EVERY_EVENT' , False )
178
196
actual_directories = configure_more (settings .DIRECTORIES )
179
197
180
198
sys .exit (start (actual_directories ))
0 commit comments