addons-server/scripts/lesswatch.py

49 строки
1.3 KiB
Python
Исходник Обычный вид История

2012-05-22 03:35:50 +04:00
#!/usr/bin/env python
import os, time, sys, re
towatch = []
includes = {}
2012-05-30 03:59:56 +04:00
def say(s):
t = time.strftime('%X')
print '[%s] %s' % (t, s)
2012-05-22 03:35:50 +04:00
def render_list(files):
for f in files:
os.system('lessc %s %s.css' % (f, f))
if (f in includes):
2012-05-30 03:59:56 +04:00
say('re-compiling %d dependencies' % len(includes[f]))
2012-05-22 03:35:50 +04:00
render_list(includes[f])
2012-05-30 03:59:56 +04:00
say('re-compiled %d files' % len(files))
2012-05-22 03:35:50 +04:00
def watch():
2012-05-30 03:59:56 +04:00
say('watching %d files...' % len(towatch))
2012-05-22 03:35:50 +04:00
before = set([(f, os.stat(f).st_mtime) for f in towatch])
while 1:
after = set([(f, os.stat(f).st_mtime) for f in towatch])
changed = [f for (f, d) in before.difference(after)]
if len(changed):
render_list(changed)
before = after
time.sleep(.5)
for root, dirs, files in os.walk('./media'):
less = filter(lambda x: re.search('\.less$', x), files)
less = [(root + '/' + f) for f in less]
for f in less:
body = post_file = open(f, 'r').read()
m = re.search('@import \'([a-zA-Z0-9_-]+)\';', body)
if m:
k = root + '/' + m.group(1) + '.less'
if not k in includes:
includes[k] = []
includes[k].append(f)
if '.git' in dirs:
dirs.remove('.git')
towatch += less
watch()