OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 """Inlines all module.json files into modules.js.""" | 7 """Inlines all module.json files into modules.js.""" |
8 | 8 |
9 from os import path | 9 from os import path |
10 import errno | 10 import errno |
11 import shutil | 11 import shutil |
12 import sys | 12 import sys |
13 try: | |
14 import simplejson as json | |
15 except ImportError: | |
16 import json | |
17 | 13 |
18 | 14 |
19 def read_file(filename): | 15 def read_file(filename): |
20 with open(filename, 'rt') as file: | 16 with open(filename, 'rt') as file: |
21 return file.read() | 17 return file.read() |
22 | 18 |
23 | 19 |
24 def build_modules(module_jsons): | 20 def build_modules(module_jsons): |
25 result = [] | 21 result = [] |
26 for json_filename in module_jsons: | 22 for json_filename in module_jsons: |
27 if not path.exists(json_filename): | 23 if not path.exists(json_filename): |
28 continue | 24 continue |
29 module_name = path.basename(path.dirname(json_filename)) | 25 module_name = path.basename(path.dirname(json_filename)) |
30 | 26 json = read_file(json_filename).replace('{', '{"name":"%s",' % module_na
me, 1) |
31 # pylint: disable=E1103 | 27 result.append(json) |
32 module_json = json.loads(read_file(json_filename)) | 28 return ','.join(result) |
33 module_json['name'] = module_name | |
34 | |
35 # Clear scripts, as they are not used at runtime | |
36 # (only the fact of their presence is important). | |
37 if module_json.get('scripts'): | |
38 module_json['scripts'] = [] | |
39 result.append(module_json) | |
40 return json.dumps(result) | |
41 | 29 |
42 | 30 |
43 def main(argv): | 31 def main(argv): |
44 input_filename = argv[1] | 32 input_filename = argv[1] |
45 output_filename = argv[2] | 33 output_filename = argv[2] |
46 module_jsons = argv[3:] | 34 module_jsons = argv[3:] |
47 | 35 |
48 with open(output_filename, 'w') as output_file: | 36 with open(output_filename, 'w') as output_file: |
49 output_file.write('var allDescriptors=%s;' % build_modules(module_jsons)
) | 37 output_file.write('var allDescriptors=[%s];' % build_modules(module_json
s)) |
50 | 38 |
51 | 39 |
52 if __name__ == '__main__': | 40 if __name__ == '__main__': |
53 sys.exit(main(sys.argv)) | 41 sys.exit(main(sys.argv)) |
OLD | NEW |