OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 # | |
3 # Copyright 2014 The Chromium Authors. All rights reserved. | |
4 # Use of this source code is governed by a BSD-style license that can be | |
5 # found in the LICENSE file. | |
6 | |
7 """ | |
8 Concatenates module scripts based on the module.json descriptor. | |
9 Optionally, minifies the result using rjsmin. | |
10 """ | |
11 | |
12 from cStringIO import StringIO | |
13 from os import path | |
14 import os | |
15 import re | |
16 import sys | |
17 | |
18 try: | |
19 import simplejson as json | |
20 except ImportError: | |
21 import json | |
22 | |
23 rjsmin_path = path.abspath(path.join( | |
24 path.dirname(__file__), | |
25 '..', | |
26 '..', | |
27 'build', | |
28 'scripts')) | |
29 sys.path.append(rjsmin_path) | |
30 import rjsmin | |
31 | |
32 | |
33 def read_file(filename): | |
34 with open(path.normpath(filename), 'rt') as file: | |
35 return file.read() | |
36 | |
37 | |
38 def write_file(filename, content): | |
39 if path.exists(filename): | |
lushnikov
2014/08/15 14:23:13
lets add comment regarding hardlink
apavlov
2014/08/15 14:54:46
Done.
| |
40 os.remove(filename) | |
41 with open(filename, 'wt') as file: | |
42 file.write(content) | |
43 | |
44 | |
45 def concatenate_scripts(file_names, module_dir, output_dir, output): | |
46 for file_name in file_names: | |
47 output.write('/* %s */\n' % file_name) | |
48 file_path = path.join(module_dir, file_name) | |
49 if not path.isfile(file_path): | |
lushnikov
2014/08/15 14:23:13
lets clarify this
apavlov
2014/08/15 14:54:46
Done.
| |
50 file_path = path.join(output_dir, path.basename(module_dir), file_na me) | |
51 output.write(read_file(file_path)) | |
52 output.write(';') | |
53 | |
54 | |
55 def main(argv): | |
56 if len(argv) < 3: | |
57 print('Usage: %s module_json output_file no_minify' % argv[0]) | |
58 return 1 | |
59 | |
60 module_json_file_name = argv[1] | |
61 output_file_name = argv[2] | |
62 no_minify = len(argv) > 3 and argv[3] | |
63 module_dir = path.dirname(module_json_file_name) | |
64 | |
65 output = StringIO() | |
66 descriptor = None | |
67 try: | |
68 descriptor = json.loads(read_file(module_json_file_name)) | |
69 except: | |
70 print 'ERROR: Failed to load JSON from ' + module_json_file_name | |
71 raise | |
72 | |
73 # pylint: disable=E1103 | |
74 scripts = descriptor.get('scripts') | |
75 if scripts: | |
lushnikov
2014/08/15 14:23:13
assert(scripts)
apavlov
2014/08/15 14:54:46
Done.
| |
76 output_root_dir = path.join(path.dirname(output_file_name), '..') | |
77 concatenate_scripts(scripts, module_dir, output_root_dir, output) | |
78 | |
79 output_script = output.getvalue() | |
80 output.close() | |
81 write_file(output_file_name, output_script if no_minify else rjsmin.jsmin(ou tput_script)) | |
82 | |
83 if __name__ == '__main__': | |
84 sys.exit(main(sys.argv)) | |
OLD | NEW |