-
Notifications
You must be signed in to change notification settings - Fork 5
/
buildaway.py
executable file
·371 lines (285 loc) · 11.6 KB
/
buildaway.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#!/usr/bin/env python
import os
import os.path
import sys
VERSION = (0, 1, 0)
class BuildException(Exception):
pass
class DepNode(object):
def __init__(self, module=None, file_name=None, is_module=True):
self.module = module
self.file_name = file_name
self.is_module = is_module
self.loaded = False
self.content = ""
# For dependency graph
self.dependencies = []
self.dependents = []
self.visited = False
def push_dependency(self, dep):
self.dependencies.append(dep)
dep.dependents.append(self)
def get_file_suffix(self):
if self.file_name is not None:
name, ext = os.path.splitext(self.file_name)
return ext
def __str__(self):
return '%s (%s)' % (self.module, self.file_name)
class DepGraph(object):
def __init__(self):
self.leaves = []
self.all_nodes = []
def build(self, inputs, sources):
import re
cache = {}
queue = [DepNode(module=f, file_name=f) for f in inputs]
def has_node(mod_name, only_if_loaded=False):
"Checks the cache for a node representing a module by this name"
if mod_name in cache:
if only_if_loaded and not cache[mod_name].loaded:
return False
else:
return True
else:
return False
def get_node(mod_name):
"Checks the cache for a node representing this module, or creates a new one"
if not has_node(mod_name):
node = DepNode(mod_name)
self.all_nodes.append(node)
cache[mod_name] = node
return cache[mod_name]
def guess_file(node):
"Guesses which file contains a particular module from it's file name"
fields = node.module.split('.')
mod_name = fields[-1]
file_name = '%s.js' % mod_name
for source in sources:
if os.path.basename(source) == file_name:
return source
def add_node_dep(node, dep_name):
existed = has_node(dep_name)
dep = get_node(dep_name)
node.push_dependency(dep)
# If this was a newly encountered module, add it
# to the queue to try to load it (and any further
# dependencies that it might have)
if not existed:
queue.append(dep)
mod_re = re.compile('away3d\.module\(\s?["\']([_a-zA-Z0-9.]*)["\']\s?,\s?(.*),\s?function\(\)\s?\{(.*)\}\s?\)\s?;?\s?$', re.DOTALL)
dep_re = re.compile('["\']([a-zA-Z0-9.]+)["\']')
inc_re = re.compile('away3d\.include\(\s*([\[\]_a-zA-Z0-9., "\'\s]*),\s*function\(\s*\)', re.DOTALL)
for node in queue:
# Check if this node has already been checked since
# it was added to the queue.
if has_node(node.module, only_if_loaded=True):
continue
fname = node.file_name or guess_file(node)
if fname is None or not os.path.isfile(fname):
raise BuildException('Could not find file for module %s' % node.module)
with open(fname, 'r') as f:
buf = f.read()
m = re.search(mod_re, buf)
if m is not None:
name = m.group(1)
dep_arg = m.group(2).strip()
node = get_node(name)
node.file_name = fname
node.content = m.group(3)
node.loaded = True
dep_names = re.findall(dep_re, dep_arg)
for dep_name in dep_names:
add_node_dep(node, dep_name)
else:
# Not a module file, but it might still contain include
# statements and hence have dependencies.
m = re.search(inc_re, buf)
if m is not None:
dep_arg = m.group(1)
dep_names = re.findall(dep_re, dep_arg)
if len(dep_names) > 0:
# This is not a proper module, so using file name
# as module name.
file_node = get_node(fname)
file_node.file_name = fname
file_node.module = fname
file_node.is_module = False
for dep_name in dep_names:
add_node_dep(node, dep_name)
# Leaves list should contain all leaf nodes, i.e. nodes that do
# not have any dependencies.
for node in self.all_nodes:
if len(node.dependencies) == 0:
self.leaves.append(node)
return self.leaves
def evaluate_chain(self):
chain = []
def visit(node):
if not node.visited:
node.visited = True
for dep in node.dependents:
visit(dep)
chain.insert(0, node)
for node in self.leaves:
visit(node)
return chain
def find_js_files(path):
# In the special case that path is a file, the user should probably
# be trusted with using this file, even if it's suffix is not js
if os.path.isfile(path):
return [path]
found = []
def visit(arg, dirname, names):
for name in names:
base, ext = os.path.splitext(name)
if ext == '.js':
found.append(os.path.join(dirname, name))
os.path.walk(path, visit, None)
return found
class BuildOpts(object):
def __init__(self):
self.inputs = []
self.sources = []
self.output = None
self.module_format = 'away3d'
def parse_args(self, args):
import getopt
inputs = []
sources = []
output = None
opts, extras = getopt.getopt(sys.argv[2:], 's:i:o:t:')
for opt in opts:
if opt[0] == '-i':
self.inputs.extend(find_js_files(opt[1]))
elif opt[0] == '-s':
self.sources.extend(find_js_files(opt[1]))
elif opt[0] == '-o':
self.output = opt[1]
elif opt[0] == '-t':
self.module_format = opt[1]
def output_as_file(self):
if self.output is None:
return sys.stdout
else:
return open(self.output, 'w')
class ModuleTranslator(object):
def __init__(self, module_format):
self.module_format = module_format
def print_header(self, out_file):
if self.module_format == 'away3d':
out_file.write('away3d = {}\n')
out_file.write('away3d.originOfModule = function() { return "%s"; }\n\n' % out_file.name)
def translate_to_file(self, node, out_file, include_deps=True, comment_file=True):
if not node.is_module:
with open(node.file_name, 'r') as in_file:
out_file.write(in_file.read());
# Use "native" away3d module format.
if self.module_format == 'away3d':
out_file.write('// %s\n' % node.file_name)
if include_deps:
out_file.write('away3d.module("%s", ' % node.module)
if len(node.dependencies) > 0:
deps = ',\n'.join([ "\t'%s'" % d.module for d in node.dependencies])
out_file.write('[\n%s\n],' % deps)
else:
out_file.write('null,')
out_file.write('\nfunction()\n{\n')
out_file.write(node.content.lstrip('\n'))
out_file.write('\n});')
else:
out_file.write('%s = (function() {\n' % node.module)
out_file.write(node.content.lstrip('\n'))
out_file.write('})();\n\n')
# Convert to the module format used by the Require.js library.
elif self.module_format == 'require.js':
if include_deps:
deps = '\n' + ',\n'.join([ "\t'%s'" % d.module for d in node.dependencies]) + '\n'
else:
deps = ''
out_file.write('// %s\n' % node.file_name)
out_file.write('define("%s", [%s], function() {\n' % (node.module, deps))
out_file.write(node.content.lstrip('\n'))
out_file.write('});\n\n')
# Convert to the Google Closure compiler module format.
elif self.module_format == 'closure':
out_file.write('// %s\n' % node.file_name)
out_file.write('goog.provide("%s");\n' % node.module)
if include_deps:
for dep in node.dependencies:
out_file.write('goog.require("%s");\n' % dep.module)
out_file.write('\n%s = (function() {\n' % node.module)
out_file.write(node.content.lstrip('\n'))
out_file.write('})();\n\n')
def translate_to_path(self, node, out_path, include_deps=True):
with open(out_path, 'w') as out_file:
self.translate_to_file(node, out_file, include_deps)
def listdep(graph, opts):
"List module dependencies in order of dependency."
output = opts.output_as_file()
chain = graph.evaluate_chain()
for node in chain:
output.write('%s\n' % node)
def concat(graph, opts):
"Concatenate module files in order of dependency."
output = opts.output_as_file()
chain = graph.evaluate_chain()
translator = ModuleTranslator(opts.module_format)
translator.print_header(output)
for node in chain:
name, ext = os.path.splitext(node.file_name)
if ext == '.js':
translator.translate_to_file(node, output, False)
output.close()
def gather(graph, opts):
"Copy dependency module files to a common directory."
if opts.output is not None and os.path.isdir(opts.output):
import shutil
translator = ModuleTranslator(opts.module_format)
for node in graph.all_nodes:
fname = os.path.basename(node.file_name)
dst = os.path.join(opts.output, fname)
print('Copying %s > %s' % (node.file_name, dst))
translator.translate_to_path(node, dst)
else:
raise BuildException('Output must be directory')
def printhelp():
print('')
print('Away3D.js build tool, version %d.%d.%d' % VERSION)
print('')
print('Commands available:')
print(' help Print this message.')
for key, val in commands.items():
print(' %s %s' % (key.ljust(10), val.__doc__))
print('')
print('Options available:')
print(' -i <path> Specifies a required input.')
print(' -s <path> Specifies a path to search for dependencies.')
print(' -o <path> Specifies output file or directory.')
print(' -t <fmt> Converts to other module formats. Can be "require.js" or "closure".')
print('')
# These are the commands that the tool supports (excluding the help command)
commands = {
'list': listdep,
'concat': concat,
'gather': gather,
}
if __name__ == '__main__':
if len(sys.argv) > 1:
cmd = sys.argv[1]
if cmd == 'help':
printhelp()
else:
if cmd in commands:
opts = BuildOpts()
opts.parse_args(sys.argv[2:])
try:
graph = DepGraph()
graph.build(opts.inputs, opts.sources)
commands[cmd](graph, opts)
except BuildException as e:
print('Error: %s' % e)
else:
print('Unknown command: %s' % cmd)
sys.exit(-1)
else:
printhelp()