Created
October 17, 2013 20:30
-
-
Save eloraburns/7031695 to your computer and use it in GitHub Desktop.
Watches your source tree for changes and runs a command when it does. Looks at filename, file size, and file mtime to determine what constitutes a "change". Saving a file with no other changes will cause it to re-run the command (I consider this a feature).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
import sys | |
from time import sleep | |
usable_files = ( | |
'.py', | |
'.php', | |
'.yaml', | |
'.json', | |
'.rst', | |
# Add file extensions you care about here | |
) | |
if len(sys.argv) < 3: | |
print "Usage: {0} PATH SOME COMMAND TO RUN".format(sys.argv[0]) | |
print " e.g.: {0} . nosetests -v".format(sys.argv[0]) | |
sys.exit(1) | |
path = sys.argv[1] | |
execute = sys.argv[2:] | |
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))) | |
new_state = get_state(path) | |
old_state = None | |
while True: | |
if old_state != new_state: | |
old_state = new_state | |
os.system(' '.join(execute)) | |
else: | |
sys.stdout.write(",") | |
sys.stdout.flush() | |
sleep(1) | |
new_state = get_state(path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment