| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2016 The Chromium Authors. All rights reserved. | 2 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """ A collator for Mojo Application Manifests """ | 6 """ A collator for Mojo Application Manifests """ |
| 7 | 7 |
| 8 import argparse | 8 import argparse |
| 9 import json | 9 import json |
| 10 import shutil | 10 import shutil |
| 11 import sys | 11 import sys |
| 12 import urlparse | 12 import urlparse |
| 13 | 13 |
| 14 |
| 14 def ParseJSONFile(filename): | 15 def ParseJSONFile(filename): |
| 15 with open(filename) as json_file: | 16 with open(filename) as json_file: |
| 16 try: | 17 try: |
| 17 return json.load(json_file) | 18 return json.load(json_file) |
| 18 except ValueError: | 19 except ValueError: |
| 19 print "%s is not a valid JSON document" % filename | 20 print "%s is not a valid JSON document" % filename |
| 20 return None | 21 return None |
| 21 | 22 |
| 23 |
| 24 def MergeDicts(left, right): |
| 25 for k, v in right.iteritems(): |
| 26 if k not in left: |
| 27 left[k] = v |
| 28 else: |
| 29 if isinstance(v, dict): |
| 30 assert isinstance(left[k], dict) |
| 31 MergeDicts(left[k], v) |
| 32 elif isinstance(v, list): |
| 33 assert isinstance(left[k], list) |
| 34 left[k].extend(v) |
| 35 else: |
| 36 raise "Refusing to merge conflicting non-collection values." |
| 37 return left |
| 38 |
| 39 |
| 40 def MergeBaseManifest(parent, base): |
| 41 MergeDicts(parent["capabilities"], base["capabilities"]) |
| 42 |
| 43 |
| 22 def main(): | 44 def main(): |
| 23 parser = argparse.ArgumentParser( | 45 parser = argparse.ArgumentParser( |
| 24 description="Collate Mojo application manifests.") | 46 description="Collate Mojo application manifests.") |
| 25 parser.add_argument("--parent") | 47 parser.add_argument("--parent") |
| 26 parser.add_argument("--output") | 48 parser.add_argument("--output") |
| 27 parser.add_argument("--application-name") | 49 parser.add_argument("--application-name") |
| 50 parser.add_argument("--base-manifest", default=None) |
| 28 args, children = parser.parse_known_args() | 51 args, children = parser.parse_known_args() |
| 29 | 52 |
| 30 parent = ParseJSONFile(args.parent) | 53 parent = ParseJSONFile(args.parent) |
| 31 if parent == None: | 54 if parent == None: |
| 32 return 1 | 55 return 1 |
| 33 | 56 |
| 57 if args.base_manifest: |
| 58 base = ParseJSONFile(args.base_manifest) |
| 59 if base == None: |
| 60 return 1 |
| 61 MergeBaseManifest(parent, base) |
| 62 |
| 34 app_path = parent['name'].split(':')[1] | 63 app_path = parent['name'].split(':')[1] |
| 35 if app_path.startswith('//'): | 64 if app_path.startswith('//'): |
| 36 raise ValueError("Application name path component '%s' must not start " \ | 65 raise ValueError("Application name path component '%s' must not start " \ |
| 37 "with //" % app_path) | 66 "with //" % app_path) |
| 38 | 67 |
| 39 if args.application_name != app_path: | 68 if args.application_name != app_path: |
| 40 raise ValueError("Application name '%s' specified in build file does not " \ | 69 raise ValueError("Application name '%s' specified in build file does not " \ |
| 41 "match application name '%s' specified in manifest." % | 70 "match application name '%s' specified in manifest." % |
| 42 (args.application_name, app_path)) | 71 (args.application_name, app_path)) |
| 43 | 72 |
| 44 applications = [] | 73 applications = [] |
| 45 for child in children: | 74 for child in children: |
| 46 application = ParseJSONFile(child) | 75 application = ParseJSONFile(child) |
| 47 if application == None: | 76 if application == None: |
| 48 return 1 | 77 return 1 |
| 49 applications.append(application) | 78 applications.append(application) |
| 50 | 79 |
| 51 if len(applications) > 0: | 80 if len(applications) > 0: |
| 52 parent['applications'] = applications | 81 parent['applications'] = applications |
| 53 | 82 |
| 54 with open(args.output, 'w') as output_file: | 83 with open(args.output, 'w') as output_file: |
| 55 json.dump(parent, output_file) | 84 json.dump(parent, output_file) |
| 56 | 85 |
| 57 return 0 | 86 return 0 |
| 58 | 87 |
| 59 if __name__ == "__main__": | 88 if __name__ == "__main__": |
| 60 sys.exit(main()) | 89 sys.exit(main()) |
| OLD | NEW |