Skip to content

Instantly share code, notes, and snippets.

@eloraburns
Created July 10, 2012 18:00
Show Gist options
  • Save eloraburns/3085166 to your computer and use it in GitHub Desktop.
Save eloraburns/3085166 to your computer and use it in GitHub Desktop.
Run a command when the filesystem changes
#!/usr/bin/env python
import os
import sys
from time import sleep
usable_files = (
# Customize this as you see fit
'.py',
'.php',
'.yaml',
)
if len(sys.argv) != 3:
print "Usage: {0} PATH 'SOME COMMAND TO RUN'".format(sys.argv[0])
sys.exit(1)
path, execute = sys.argv[1:3]
def get_usable_filenames(path):
for root, dirs, files in os.walk(path):
for filename in files:
if filename.endswith(usable_files):
yield os.path.join(root, filename)
def get_file_hash(filename):
info = os.stat(filename)
return filename, info.st_size, info.st_mtime
def get_state(path):
return set(map(get_file_hash, get_usable_filenames(path)));
old_state = get_state(path)
new_state = old_state
while True:
if old_state != new_state:
print
old_state = new_state
os.system(execute)
else:
sys.stdout.write(".")
sys.stdout.flush()
sleep(2)
new_state = get_state(path)
@eloraburns
Copy link
Author

tdaemon is too specific to Python and the very particular test harnesses tdaemon explicitly supports. Most other test-runner things (like python-gorun) use inotify, which is great, except that it doesn't work over HGFS (vmware file sharing) or on OSX. (incron falls into this category, too)

The overhead of doing a stat call on relevant filenames in a tree is pretty small. If it's small enough for you, then there's no reason to use something more complicated.

In my case, I run the script from my OSX host, and have it SSH into a vmware development image to run the test command itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment