Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / scripts / concatenate_module_descriptors.py
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 """Inlines all module.json files into "var allDescriptors" in Runtime.js."""
8
9 from os import path
10 import errno
11 import os
12 import re
13 import shutil
14 import sys
15 try:
16     import simplejson as json
17 except ImportError:
18     import json
19
20
21 def read_file(filename):
22     with open(filename, 'rt') as file:
23         return file.read()
24
25
26 def build_modules(module_jsons):
27     result = []
28     for json_filename in module_jsons:
29         if not path.exists(json_filename):
30             continue
31         module_name = path.basename(path.dirname(json_filename))
32
33         # pylint: disable=E1103
34         module_json = json.loads(read_file(json_filename))
35         module_json['name'] = module_name
36
37         # Clear scripts, as they are not used at runtime
38         # (only the fact of their presence is important).
39         if module_json.get('scripts'):
40             module_json['scripts'] = []
41         result.append(module_json)
42     return json.dumps(result)
43
44
45 def main(argv):
46     input_filename = argv[1]
47     output_filename = argv[2]
48     module_jsons = argv[3:]
49
50     output_contents = re.sub('var allDescriptors = \[\];', 'var allDescriptors = %s;' % build_modules(module_jsons).replace("\\", "\\\\"), read_file(input_filename), 1)
51     if (path.exists(output_filename)):
52         os.remove(output_filename)
53     with open(output_filename, 'w') as output_file:
54         output_file.write(output_contents)
55
56 if __name__ == '__main__':
57     sys.exit(main(sys.argv))