-
Notifications
You must be signed in to change notification settings - Fork 11
/
pack
executable file
·127 lines (109 loc) · 4.13 KB
/
pack
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
#!/usr/bin/env python
import os, sys, struct, cjson, stat
from os.path import join
here = os.path.dirname(os.path.abspath(__file__))
global_shunts = '''
Function.prototype.property = function(prop, desc) {
Object.defineProperty(this.prototype, prop, desc);
};
'''
def packModules(path):
result = ''
template = '''
loader.define('%s', function(exports, require, get){
%s});
'''
fs_base = join(here, path)
for path, dirs, names in os.walk(fs_base):
relpath = path[len(fs_base):]
if not relpath.startswith('/'):
relpath = '/' + relpath
for name in names:
if name.endswith('.js'):
packpath = join(relpath, name)
fspath = join(path, name)
data = open(fspath).read()
module = template % (packpath, data)
result += module
return result
def packCode(app, target):
if not requireCodePack([app, 'extra', 'lib'], target):
return
print 'packing code: %s' % target
result = ''
result += global_shunts
for path, dirs, names in os.walk(join(here, 'extra')):
for name in names:
if name.endswith('.js'):
data = open(join(path, name)).read()
result += '\n%s;\n' % data
result += packModules(app)
result += packModules('lib')
result += '\n$(function(){loader.main()});'
open(join(here, target, 'code.js'), 'w').write(result)
def requireCodePack(apps, target):
target_path = join(here, target, 'code.js')
if not os.path.exists(target_path):
return True
for app in apps:
target_mtime = os.stat(target_path)[stat.ST_MTIME]
for path, dirs, names in os.walk(join(here, app)):
for name in names:
if name.endswith('.js'):
code_mtime = os.stat(join(path, name))[stat.ST_MTIME]
if code_mtime > target_mtime:
return True
return False
def requireAssetPack(app, target):
for app in [app, 'lib']:
fs_base = join(here, app)
target_path = join(here, target, 'assets.pack')
if not os.path.exists(target_path):
return True
target_mtime = os.stat(target_path)[stat.ST_MTIME]
for path, dirs, names in os.walk(fs_base):
for name in names:
ext = os.path.splitext(name)[1].lstrip('.')
fspath = join(path, name)
if ext in ('png', 'jpg', 'ogg', 'model', 'mesh', 'bones', 'txt', 'shader', 'json'):
asset_mtime = os.stat(fspath)[stat.ST_MTIME]
if asset_mtime > target_mtime:
return True
return False
def packAssets(app, target):
if not requireAssetPack(app, target):
return
print 'packing assets: %s' % target
chunks = ''
meta = {}
for app in [app, 'lib']:
fs_base = join(here, app)
for path, dirs, names in os.walk(fs_base):
relpath = path[len(fs_base):]
if not relpath.startswith('/'):
relpath = '/' + relpath
for name in names:
ext = os.path.splitext(name)[1].lstrip('.')
packpath = join(relpath, name)
fspath = join(path, name)
if ext in ('png', 'jpg', 'ogg', 'mesh', 'bones', 'model'):
data = open(fspath, 'rb').read()
offset = len(chunks)
size = len(data)
chunks += data
meta[packpath] = {
'offset': offset,
'size': size,
}
elif ext in ('txt', 'shader'):
meta[packpath] = open(fspath, 'r').read()
elif ext == 'json':
meta[packpath] = cjson.decode(open(fspath, 'r').read())
metadata = cjson.encode(meta)
metasize = struct.pack('I', len(metadata))
open(join(here, target, 'assets.pack'), 'wb').write('PACK'+metasize+metadata+chunks)
def pack(app, target):
packCode(app, target)
packAssets(app, target)
if __name__ == '__main__':
pack('src', 'www')