Skip to content

Commit 9c80f36

Browse files
author
Peter Bengtsson
committed
fixed merge conflict i think
2 parents 0946674 + b844884 commit 9c80f36

File tree

1 file changed

+41
-23
lines changed

1 file changed

+41
-23
lines changed

gorun.py

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
# Wrapper on pyinotify for running commands
44
# (c) 2009 Peter Bengtsson, [email protected]
55
#
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
811

912
import os
1013

14+
from subprocess import Popen
15+
from threading import Lock, Thread
16+
1117
__version__='1.6'
12-
13-
# Prepare a lock file
14-
from tempfile import gettempdir
15-
LOCK_FILE = os.path.join(gettempdir(), 'gorunning.lock')
1618

1719
class SettingsClass(object):
1820
VERBOSE = False
@@ -62,6 +64,11 @@ def _ignore_file(path):
6264
return True
6365

6466
class PTmp(ProcessEvent):
67+
68+
def __init__(self):
69+
super(PTmp, self).__init__()
70+
self.lock = Lock()
71+
6572
def process_IN_CREATE(self, event):
6673
if os.path.basename(event.pathname).startswith('.#'):
6774
# backup file
@@ -76,23 +83,33 @@ def process_IN_CREATE(self, event):
7683
def process_IN_MODIFY(self, event):
7784
if _ignore_file(event.pathname):
7885
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+
96113
def start(actual_directories):
97114

98115
wm = WatchManager()
@@ -175,6 +192,7 @@ def configure_more(directories):
175192
settings.VERBOSE = getattr(x, 'VERBOSE', settings.VERBOSE)
176193
settings.IGNORE_EXTENSIONS = getattr(x, 'IGNORE_EXTENSIONS', tuple())
177194
settings.IGNORE_DIRECTORIES = getattr(x, 'IGNORE_DIRECTORIES', tuple())
195+
settings.RUN_ON_EVERY_EVENT = getattr(x, 'RUN_ON_EVERY_EVENT', False)
178196
actual_directories = configure_more(settings.DIRECTORIES)
179197

180198
sys.exit(start(actual_directories))

0 commit comments

Comments
 (0)