-
Notifications
You must be signed in to change notification settings - Fork 2
/
js-compressor.py
68 lines (55 loc) · 1.94 KB
/
js-compressor.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
#!/usr/bin/python2.6
# @author: allenm, oldj
#
# @link: https://github.com/allenm/js-css-compressor
#
import httplib
import urllib
import sys
import os
# Define the parameters for the POST request and encode them in
# a URL-safe format.
def compressor(savename, filenames):
''' compressor and combine the javascript files. This script use the google closure REST API '''
filenames = (v.strip() for v in filenames.split(";"))
code = []
for fn in filenames:
if fn.startswith('http://'):
# url
code.append(('code_url', fn))
else:
# local file
if not os.path.isfile(fn):
print 'ERROR: "%s" is not a valid file!' % fn
return False
code.append(('js_code', open(fn).read()))
code.extend([
('compilation_level', 'SIMPLE_OPTIMIZATIONS'),
('output_format', 'text'),
('output_info', 'compiled_code'),
])
params = urllib.urlencode(code)
# Always use the following value for the Content-type header.
headers = {'Content-type': 'application/x-www-form-urlencoded'}
conn = httplib.HTTPConnection('closure-compiler.appspot.com')
conn.request('POST', '/compile', params, headers)
response = conn.getresponse()
data = response.read()
print 'DATA:'
print '-' * 50
print data.rstrip()
conn.close()
donefile = open(savename, 'w')
donefile.write(data)
donefile.close()
print '-' * 50
print '>> output: %s (%.2fK)' % (savename, len(data) / 1024.0)
if __name__ == "__main__":
if sys.argv.__len__() >= 3:
compressor(sys.argv[1], sys.argv[2])
else:
print '''This script must contain at least two parameters.
The first one is the filename which you want store the data after compress,
the second is the urls or filenames of javascript file which you want compress,
if you have more than one file to compress,
use ";" to partition them.'''