-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmininify.py
More file actions
executable file
·130 lines (106 loc) · 3.45 KB
/
mininify.py
File metadata and controls
executable file
·130 lines (106 loc) · 3.45 KB
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
#!/usr/bin/python
# Simple python script that takes a html file extracts all scripts and concatenates them into
# a single file. The concatenated script is then sent to the google closure compiler for further
# squishing.
# Finally a game.zip archive is created with all specified files. The size of this archive
# is printed to standard output
#
# Usage:
# In the terminal type:
# python min.py OR ./min.py (providing it is executable and python is in your $PATH)
# Thanks to @eoinmcg from https://gist.github.com/3436116
from BeautifulSoup import BeautifulSoup, Tag
from zipfile import ZipFile, ZIP_DEFLATED
import os, httplib, urllib, sys, shutil
# name of our dev html file. we'll pull all js files out of here
dev = 'index_dev.html'
index = 'index.html'
# closure compiler method. options SIMPLE, ADVANCED and WHITESPACE_ONLY
optimise = 'SIMPLE'
# destination for our concatented and compressed js file
compressed = 'game.min.js'
# files to be included in the zip
files = [
'index.html',
"css/game.min.css",
"img/magnet.gif",
"img/source.gif",
"img/wall.gif",
"img/well.gif",
"img/wellFinal.gif",
"img/bgPattern.gif",
"img/rotate.gif",
"img/blobSprite.gif",
compressed
]
# target folder for all our zip files
folder = 'blobrising'
# grab all scripts from our dev html for concatentation
dev_file = open(dev, 'r')
html = dev_file.read()
dev_file.close()
soup = BeautifulSoup(html)
concat_js = open('all.js', 'w')
scripts = soup.findAll(['script'])
for script in scripts:
if script.has_key('src'):
src = open(script['src'], 'r')
concat_js.write(src.read())
src.close()
concat_js.close()
# get contat'd js to send to the closure compiler
js = open('all.js').read()
params = urllib.urlencode([
('js_code', js),
('compilation_level', optimise + '_OPTIMIZATIONS'),
('output_format', 'text'),
('output_info', 'compiled_code'),
('formatting', 'print_input_delimiter')
])
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
# and write the closure output to our js file
final_js = open(compressed, 'w')
final_js.write(data);
final_js.close()
# update our index.html to mirror dev.html
dev_file = open(dev, 'r')
html = dev_file.read()
dev_file.close()
soup = BeautifulSoup(html)
# remove all script tags
for tag in soup.findAll('script'):
tag.extract()
# append final script tag to body
script = Tag(soup, "script")
script["src"] = compressed
soup.body.insert(soup.body.contents, script)
index_file = open(index, 'w')
index_file.write(soup.prettify())
index_file.close()
# create folder for our game, if it doesnt exist
if not os.path.exists(folder):
os.makedirs(folder)
if not os.path.exists(folder + "/css"):
os.makedirs(folder + "/css")
if not os.path.exists(folder + "/img"):
os.makedirs(folder + "/img")
# copy files into folder (to avoid creating a zip bomb)
for filename in files:
shutil.copy2(filename, folder + '/' + filename)
# zip all our files
zf = ZipFile(folder + '.zip', 'w', ZIP_DEFLATED)
for filename in files:
zf.write(folder + '/' + filename)
zf.close()
# and a bit of a cleanup
shutil.rmtree(folder)
# finally, tell us how much we've squeezed in
total = os.path.getsize(folder + '.zip')
remaining = 13312 - total
print 'Total used: ', total
print 'Bytes remaining: ', remaining