Skip to content

Instantly share code, notes, and snippets.

@PaulGuo
Created February 23, 2012 11:23
Show Gist options
  • Save PaulGuo/1892447 to your computer and use it in GitHub Desktop.
Save PaulGuo/1892447 to your computer and use it in GitHub Desktop.
(dot)LESS Auto Compiler
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import sys,os,re
import subprocess
import pyinotify
class OnWriteHandler(pyinotify.ProcessEvent):
def my_init(self,cwd,extension,cmd):
self.cwd=cwd
self.extensions=extension.split(',')
self.cmd=cmd
def _run_cmd(self,event):
print('==> file modification detected')
print(event.pathname)
source=event.pathname
dest=''.join([os.path.splitext(event.pathname)[0],'.css'])
cmd=self.cmd % (source,dest)
subprocess.call(cmd.split(' '),cwd=self.cwd)
def process_IN_MODIFY(self,event):
if all(not event.pathname.endswith(ext) for ext in self.extensions):
return
self._run_cmd(event)
def auto_compile(path,extension,cmd):
excl_lst=['.*/\.svn.*',
'.*/\.config.*',
'.*/\.local.*',
'.*/\.cache.*',
'.*/\.subversion.*',
'.*/\.[^\/]*/.*']
excl=pyinotify.ExcludeFilter(excl_lst)
wm=pyinotify.WatchManager()
handler=OnWriteHandler(cwd=path,extension=extension,cmd=cmd)
notifier=pyinotify.Notifier(wm,default_proc_fun=handler)
wm.add_watch(path,pyinotify.ALL_EVENTS,rec=True,auto_add=True)
print('==> start monitoring %s (type cmd^c to exit)' % path)
notifier.loop()
if __name__=='__main__':
if len(sys.argv)<3:
print >> sys.stderr,'command line error: missing argument(s).'
sys.exit(1)
path=sys.argv[1]
extension=sys.argv[2]
cmd='/home/liuhuo/env/less.js/bin/lessc %s %s'
if len(sys.argv)==4:
cmd=sys.argv[3]
auto_compile(path,extension,cmd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment