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.
7 """Inlines all module.json files into "var allDescriptors" in Runtime.js."""
16 import simplejson as json
21 def read_file(filename):
22 with open(filename, 'rt') as file:
26 def build_modules(module_jsons):
28 for json_filename in module_jsons:
29 if not path.exists(json_filename):
31 module_name = path.basename(path.dirname(json_filename))
33 # pylint: disable=E1103
34 module_json = json.loads(read_file(json_filename))
35 module_json['name'] = module_name
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)
46 input_filename = argv[1]
47 output_filename = argv[2]
48 module_jsons = argv[3:]
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)
56 if __name__ == '__main__':
57 sys.exit(main(sys.argv))