forked from eoinmcg/freebird
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmin.py
executable file
·104 lines (82 loc) · 2.74 KB
/
min.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
#!/usr/bin/python
from BeautifulSoup import BeautifulSoup, Tag
from zipfile import ZipFile, ZIP_DEFLATED
import os, httplib, urllib, sys, shutil, fileinput
# name of our dev html file. we'll pull all js files out of here
dev = '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 = 'g.js'
# files to be included in the zip
files = ['index.html', 'b.png', 't.png', 'manifest.appcache', 'favicon.ico', compressed]
# target folder for all our zip files
folder = 'game'
# 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'),
])
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()
for line in fileinput.FileInput(index,inplace=1):
line = line.replace('<html>', '<html manifest="manifest.appcache">')
print line,
# create folder for our game, if it doesnt exist
if not os.path.exists(folder):
os.makedirs(folder)
# 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