This repository has been archived by the owner on Mar 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
closureapi.py
116 lines (92 loc) · 3.69 KB
/
closureapi.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
"""
MIT License
Copyright (c) 2010, Ben Ogle
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
import httplib, urllib, sys
import StringIO
import getopt
WHITESPACE_ONLY = 'WHITESPACE_ONLY'
SIMPLE_OPTIMIZATIONS = 'SIMPLE_OPTIMIZATIONS'
ADVANCED_OPTIMIZATIONS = 'ADVANCED_OPTIMIZATIONS'
COMPILATION_LEVELS = [WHITESPACE_ONLY, SIMPLE_OPTIMIZATIONS, ADVANCED_OPTIMIZATIONS]
OUT_FILE = 'compressed.js'
DEFAULT_LEVEL = 1
def string_compress(js_string, level=SIMPLE_OPTIMIZATIONS):
"""
compresses a string of js with the closure compiler web service
returns a compressed string of js
Modified code from:
http://code.google.com/closure/compiler/docs/api-tutorial1.html
"""
params = urllib.urlencode([
('js_code', js_string),
('compilation_level', level),
('output_format', 'text'),
('output_info', 'compiled_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()
conn.close
l_orig, l_new = len(js_string), len(data)
perc = float(l_new)/float(l_orig) * 100.0
print 'Compressed %d to %d : %.2f%% of original size' % (l_orig, l_new, perc)
return data
def concat(filenames):
s = StringIO.StringIO()
for file in filenames:
f = open(file, 'r')
s.write(f.read())
f.close()
return s.getvalue()
def compress(out_file, input_fnames, level=DEFAULT_LEVEL):
out_file = out_file or OUT_FILE
level = level or DEFAULT_LEVEL
compressed = string_compress(concat(input_fnames), COMPILATION_LEVELS[level])
out = open(out_file, 'w')
out.write(compressed)
out.close()
def usage():
print '%s [options] [jsfiles]' % (sys.argv[0],)
print 'Options:'
print ' -h -- This.'
print ' -o <outfile> -- output file'
print ' -l -- Int index in [WHITESPACE_ONLY, SIMPLE_OPTIMIZATIONS, ADVANCED_OPTIMIZATIONS]'
if __name__ == '__main__':
opts, args = getopt.getopt(sys.argv[1:], 'ho:l:', ['help', 'output=', 'level='])
out_file = None
level = DEFAULT_LEVEL
for opt, arg in opts:
if opt in ('-h', '--help'):
usage()
sys.exit(2)
elif opt in ('-o', '--output'):
out_file = arg
elif opt in ('-l', '--level'):
try:
level = int(arg)
except TypeError:
usage()
sys.exit(1)
if not args:
usage()
sys.exit(1)
compress(out_file, args, level)