Created
October 5, 2012 10:26
-
-
Save marconi/3839129 to your computer and use it in GitHub Desktop.
A less file compiler.
This file contains hidden or 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
# -*- coding: utf-8 -*- | |
""" | |
Watchless will compile your less files whenever it detects changes. | |
Accepts one argument, the path where your less files are stored. | |
Say your less files are store in /static/less and it contains the file | |
layout.less, watchless will compile it and store the compiled css file | |
in /static/css/layout.css. | |
Usage: | |
python watchless.py <path to less directory> | |
""" | |
import sys | |
import os | |
import time | |
def compile_less(less_file): | |
print "Compiling %s" % less_file | |
filename, __ = os.path.splitext(os.path.basename(less_file)) | |
css_file = "%s.css" % os.path.join(CSS_ROOT, filename) | |
os.system("lessc %(from)s > %(to)s -x" % {"from": less_file, | |
"to": css_file}) | |
if __name__ == '__main__': | |
if len(sys.argv) != 2: | |
print "You need to specify the path where .less files are located." | |
sys.exit(1) | |
if not os.path.exists(sys.argv[1]): | |
print "The path you specified doesn't exist." | |
sys.exit(1) | |
LESS_ROOT = sys.argv[1] | |
CSS_ROOT = os.path.join(os.path.split(LESS_ROOT)[0], 'css') | |
# gather all less files | |
less_files = {} | |
for filename in os.listdir(LESS_ROOT): | |
less_file = os.path.join(LESS_ROOT, filename) | |
less_files[less_file] = int(os.stat(less_file).st_mtime) | |
try: | |
while True: | |
for less_fle, lastmod in less_files.items(): | |
newmod = int(os.stat(less_fle).st_mtime) | |
if newmod > lastmod: | |
less_files[less_fle] = newmod | |
compile_less(less_fle) | |
time.sleep(1) # zzzzz | |
except KeyboardInterrupt: | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment