-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathlesswatch
executable file
·122 lines (109 loc) · 3.74 KB
/
lesswatch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# (c) 2011, Mathieu Comandon <[email protected]>
# License : GPL-3
# For more information read : /usr/share/common-licenses/GPL-3
#
# Version 0.1
#
# if you get a pynotify error try running :
# sudo sysctl -n -w fs.inotify.max_user_watches=16384
# https://github.com/seb-m/pyinotify/wiki/Frequently-Asked-Questions
try:
import pyinotify
except:
print "You need to install the python bindings for inotify (python-pyinotify)"
print "inotify is a Linux-only feature and is not available on other systems, sorry."
exit()
import os
import sys
import time
import glob
import subprocess
try:
from gi.repository import Notify
except ImportError:
Notify = None
class color:
red = '\033[91m'
yellow = '\033[93m'
green = '\033[92m'
blue = '\033[94m'
purple = '\033[95m'
bold = '\033[1m'
none = '\033[0m'
if len(sys.argv) > 1:
path = sys.argv[1]
if not os.path.exists(path):
print "Invalid path %s" % path
exit()
else:
path = os.getcwd()
# Read @import directives and find reverse dependencies
lessfiles = glob.glob(path + '/*.less')
imports = {}
for lessfile in lessfiles:
line = "-"
less = file(lessfile)
while line:
line = less.readline()
if line.startswith("@import"):
less_base = os.path.basename(lessfile)
less_import = line[7:].strip("\"'; \t\n")
if less_import.endswith('.css'):
continue
elif not less_import.endswith('.less'):
less_import += ".less"
if less_import not in imports:
imports[less_import] = []
if less_base not in imports[less_import]:
imports[less_import].append(less_base)
# Watch changes
wm = pyinotify.WatchManager()
mask = pyinotify.IN_MODIFY | pyinotify.IN_DELETE | pyinotify.IN_CREATE
error = False
class EventHandler(pyinotify.ProcessEvent):
def compile_less(self, less_path):
less_file = os.path.basename(less_path)
target = less_path[:-4] + "css"
return_code = subprocess.call(['lessc', less_path, target])
global error
if return_code == 0:
if error is True:
compile_normal_notify = Notify.Notification.new(
"LessCSS compile back to normal",
"File %s has been fixed" % less_file,
"info"
)
compile_normal_notify.show()
error = False
print color.green + "* Updated" + color.none, less_file
else:
print color.red + "! Syntax error in " + less_file + color.none
if Notify:
Notify.init("LessCSS compile error")
error = True
compile_error_notify = Notify.Notification.new(
"LessCSS compile error",
"In file %s" % less_file,
"dialog-error-symbolic"
)
compile_error_notify.show()
def process_IN_MODIFY(self, event):
if not event.pathname.endswith('.less'):
return
cwd = os.getcwd()
less_path = event.pathname
less_filename = os.path.basename(less_path)
print time.strftime("%H:%M:%S"), "-- Change detected in %s ..." % less_filename
self.compile_less(less_path)
less_relative_path = less_path[len(cwd) + 1:]
for less_base in imports[less_relative_path]:
path = os.path.join(cwd, less_base)
print path
self.compile_less(path)
handler = EventHandler()
notifier = pyinotify.Notifier(wm, handler)
wdd = wm.add_watch(path, mask, rec=True)
print "* Watching for changes in %s... Ctrl-C to abort." % os.path.abspath(path)
notifier.loop()