forked from ajenti/ajenti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile_resources.py
executable file
·154 lines (116 loc) · 3.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/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, 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.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.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,
}
def compress_js(inpath):
outpath = os.path.splitext(inpath)[0] + '.c.js'
if not do_compress:
return shutil.copy(inpath, outpath)
logging.info('Compressing JS: %s' % inpath)
cmd = 'yui-compressor "%s"' % inpath
out = check_output(cmd, shell=True)
if out:
open(outpath, 'w').write(out)
def compress_css(inpath):
outpath = os.path.splitext(inpath)[0] + '.c.css'
return shutil.copy(inpath, outpath)
#if not do_compress:
# return shutil.copy(inpath, outpath)
logging.info('Compressing CSS: %s' % inpath)
check_output('yui-compressor -o "%s" "%s"' % (outpath, inpath), shell=True)
compressors = {
r'.+[^\.][^mci]\.js$': compress_js,
r'.+[^\.][^mci]\.css$': compress_css,
}
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)
def compress(file_path):
for pattern in compressors:
if re.match(pattern, file_path):
compressors[pattern](file_path)
do_compress = True
if len(sys.argv) > 1 and sys.argv[1] == 'nocompress':
do_compress = False
if not os.path.exists('tmp'):
os.mkdir('tmp')
greenlets = []
traverse(compile)
greenlets = []
traverse(compress)