Created
May 5, 2010 14:04
-
-
Save gz/390808 to your computer and use it in GitHub Desktop.
automatic sass to css compiling
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 | |
# auto_sass.py - watches your sass files in a directory and compiles them to a css file | |
# whenever you save it. | |
# currently this only works under linux and requires pynotify | |
# use: ./auto_sass.py <directory> | |
import os, sys, subprocess | |
from pyinotify import WatchManager, Notifier, ProcessEvent, IN_CLOSE_WRITE | |
SASS_FILE_EXTENSION = 'sass' | |
CSS_FILE_EXTENSION = 'css' | |
def which(program): | |
import os | |
def is_exe(fpath): | |
return os.path.exists(fpath) and os.access(fpath, os.X_OK) | |
fpath, fname = os.path.split(program) | |
if fpath: | |
if is_exe(program): | |
return program | |
else: | |
for path in os.environ["PATH"].split(os.pathsep): | |
exe_file = os.path.join(path, program) | |
if is_exe(exe_file): | |
return exe_file | |
return None | |
class SassModifyDetector(ProcessEvent): | |
def __init__(self): | |
self.last = None | |
def process_IN_CLOSE_WRITE(self, event): | |
if event.name and event.path: | |
sass_file = os.path.join(event.path, event.name) | |
basename, extension = os.path.splitext(sass_file) | |
if extension == '.' + SASS_FILE_EXTENSION: | |
print "[converting]", sass_file | |
css_file = basename + '.' + CSS_FILE_EXTENSION | |
p = subprocess.Popen( ('sass %s > %s' % (sass_file, css_file)), stdout=subprocess.PIPE, shell=True) | |
if __name__ == '__main__': | |
try: | |
path = sys.argv[1] | |
except IndexError: | |
print 'Usage: %s directory to monitor' % sys.argv[0] | |
else: | |
if not which('sass'): | |
print 'You need to install sass and/or add it to your path!' | |
else: | |
wm = WatchManager() | |
notifier = Notifier(wm, SassModifyDetector(), 0, 0, 1000) | |
wm.add_watch(path, IN_CLOSE_WRITE, rec=True) | |
try: | |
while 1: | |
notifier.process_events() | |
if notifier.check_events(): | |
notifier.read_events() | |
except KeyboardInterrupt: | |
notifier.stop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment