forked from ajenti/ajenti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile_resources.py
executable file
·118 lines (87 loc) · 2.71 KB
/
compile_resources.py
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
#!/usr/bin/env python
import subprocess
import shutil
import glob
import os
import re
import sys
import gevent
try:
from gevent import subprocess
except:
import subprocess
import uuid
import threading
import logging
import ajenti.compat
import ajenti.log
def check_output(*args, **kwargs):
try:
return subprocess.check_output(*args, **kwargs)
except Exception as e:
logging.error('Call failed')
logging.error(' '.join(args[0]))
logging.error(str(e))
sys.exit(0)
ajenti.log.init()
def dirname():
return 'tmp/' + str(uuid.uuid4())
def compile_coffeescript(inpath):
outpath = '%s.c.js' % inpath
if os.path.exists(outpath) and os.stat(outpath).st_mtime > os.stat(inpath).st_mtime:
logging.info('Skipping %s' % inpath)
return
logging.info('Compiling CoffeeScript: %s' % inpath)
d = dirname()
check_output('coffee -o %s -c "%s"' % (d, inpath), shell=True)
shutil.move(glob.glob('./%s/*.js' % d)[0], outpath)
shutil.rmtree(d)
def compile_less(inpath):
outpath = '%s.c.css' % inpath
if os.path.exists(outpath) and os.stat(outpath).st_mtime > os.stat(inpath).st_mtime:
logging.info('Skipping %s' % inpath)
#return
logging.info('Compiling LESS %s' % inpath)
out = check_output('lessc "%s" "%s"' % (inpath, outpath), shell=True)
if out:
logging.error(out)
#print subprocess.check_output('recess --compile "%s" > "%s"' % (inpath, outpath), shell=True)
compilers = {
r'.+\.coffee$': compile_coffeescript,
r'.+[^i]\.less$': compile_less,
}
greenlets = []
def traverse(fx):
plugins_path = './ajenti/plugins'
for plugin in os.listdir(plugins_path):
path = os.path.join(plugins_path, plugin, 'content')
if os.path.exists(path):
for (dp, dn, fn) in os.walk(path):
for name in fn:
file_path = os.path.join(dp, name)
greenlets.append(gevent.spawn(fx, file_path))
done = 0
done_gls = []
length = 40
total = len(greenlets)
print
while True:
for gl in greenlets:
if gl.ready() and not gl in done_gls:
done_gls.append(gl)
done += 1
dots = int(1.0 * length * done / total)
sys.stdout.write('\r%3i/%3i [' % (done, total) + '.' * dots + ' ' * (length - dots) + ']')
sys.stdout.flush()
gevent.sleep(0.1)
if done == total:
print
break
def compile(file_path):
for pattern in compilers:
if re.match(pattern, file_path):
compilers[pattern](file_path)
if not os.path.exists('tmp'):
os.mkdir('tmp')
greenlets = []
traverse(compile)