OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2014 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2014 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 # Disable the lint error for too-long lines for the URL below. | 6 # Disable the lint error for too-long lines for the URL below. |
7 # pylint: disable=C0301 | 7 # pylint: disable=C0301 |
8 | 8 |
9 """Fix Chrome App manifest.json files for use with multi-platform zip files. | 9 """Fix Chrome App manifest.json files for use with multi-platform zip files. |
10 | 10 |
(...skipping 16 matching lines...) Expand all Loading... |
27 Becomes | 27 Becomes |
28 | 28 |
29 "platforms": [ | 29 "platforms": [ |
30 { | 30 { |
31 "nacl_arch": "x86-64", | 31 "nacl_arch": "x86-64", |
32 "sub_package_path": "<toolchain>/<config>/_platform_specific/x86-64/" | 32 "sub_package_path": "<toolchain>/<config>/_platform_specific/x86-64/" |
33 }, | 33 }, |
34 ... | 34 ... |
35 """ | 35 """ |
36 | 36 |
| 37 import argparse |
37 import collections | 38 import collections |
38 import json | 39 import json |
39 import optparse | |
40 import os | 40 import os |
41 import sys | 41 import sys |
42 | 42 |
43 if sys.version_info < (2, 7, 0): | 43 if sys.version_info < (2, 7, 0): |
44 sys.stderr.write("python 2.7 or later is required run this script\n") | 44 sys.stderr.write("python 2.7 or later is required run this script\n") |
45 sys.exit(1) | 45 sys.exit(1) |
46 | 46 |
47 | 47 |
48 class Error(Exception): | 48 class Error(Exception): |
49 """Local Error class for this file.""" | 49 """Local Error class for this file.""" |
50 pass | 50 pass |
51 | 51 |
52 | 52 |
53 def Trace(msg): | 53 def Trace(msg): |
54 if Trace.verbose: | 54 if Trace.verbose: |
55 sys.stderr.write(str(msg) + '\n') | 55 sys.stderr.write(str(msg) + '\n') |
56 | 56 |
57 Trace.verbose = False | 57 Trace.verbose = False |
58 | 58 |
59 | 59 |
60 def main(argv): | 60 def main(args): |
61 parser = optparse.OptionParser( | 61 parser = argparse.ArgumentParser(description=__doc__) |
62 usage='Usage: %prog [options] manifest.json', description=__doc__) | 62 parser.add_argument('-p', '--prefix', |
63 parser.add_option('-p', '--prefix', | 63 help='Prefix to set for all sub_package_paths in the ' |
64 help='Prefix to set for all sub_package_paths in the ' | 64 'manifest. If none is specified, the prefix will be ' |
65 'manifest. If none is specified, the prefix will be ' | 65 'removed; i.e. the start of the path will be ' |
66 'removed; i.e. the start of the path will be ' | 66 '"_platform_specific/..."') |
67 '"_platform_specific/..."') | 67 parser.add_argument('-v', '--verbose', |
68 parser.add_option('-v', '--verbose', | 68 help='Verbose output', action='store_true') |
69 help='Verbose output', action='store_true') | 69 parser.add_argument('manifest_json') |
70 | 70 |
71 options, args = parser.parse_args(argv) | 71 options = parser.parse_args(args) |
72 if options.verbose: | 72 if options.verbose: |
73 Trace.verbose = True | 73 Trace.verbose = True |
74 | 74 |
75 if not args: | 75 Trace('Reading %s' % options.manifest_json) |
76 parser.error('Expected manifest file.') | 76 with open(options.manifest_json) as f: |
77 | |
78 manifest = args[0] | |
79 | |
80 Trace('Reading %s' % manifest) | |
81 with open(manifest) as f: | |
82 # Keep the dictionary order. This is only supported on Python 2.7+ | 77 # Keep the dictionary order. This is only supported on Python 2.7+ |
83 if sys.version_info >= (2, 7, 0): | 78 if sys.version_info >= (2, 7, 0): |
84 data = json.load(f, object_pairs_hook=collections.OrderedDict) | 79 data = json.load(f, object_pairs_hook=collections.OrderedDict) |
85 else: | 80 else: |
86 data = json.load(f) | 81 data = json.load(f) |
87 | 82 |
88 if 'platforms' not in data: | 83 if 'platforms' not in data: |
89 raise Error('%s does not have "platforms" key.' % manifest) | 84 raise Error('%s does not have "platforms" key.' % options.manifest_json) |
90 | 85 |
91 platforms = data['platforms'] | 86 platforms = data['platforms'] |
92 if type(platforms) is not list: | 87 if type(platforms) is not list: |
93 raise Error('Expected "platforms" key to be array.') | 88 raise Error('Expected "platforms" key to be array.') |
94 | 89 |
95 if options.prefix: | 90 if options.prefix: |
96 prefix = options.prefix + '/' | 91 prefix = options.prefix + '/' |
97 else: | 92 else: |
98 prefix = '' | 93 prefix = '' |
99 | 94 |
100 for platform in platforms: | 95 for platform in platforms: |
101 nacl_arch = platform.get('nacl_arch') | 96 nacl_arch = platform.get('nacl_arch') |
102 | 97 |
103 if 'sub_package_path' not in platform: | 98 if 'sub_package_path' not in platform: |
104 raise Error('Expected each platform to have "sub_package_path" key.') | 99 raise Error('Expected each platform to have "sub_package_path" key.') |
105 | 100 |
106 sub_package_path = platform['sub_package_path'] | 101 sub_package_path = platform['sub_package_path'] |
107 index = sub_package_path.find('_platform_specific') | 102 index = sub_package_path.find('_platform_specific') |
108 if index == -1: | 103 if index == -1: |
109 raise Error('Could not find "_platform_specific" in the ' | 104 raise Error('Could not find "_platform_specific" in the ' |
110 '"sub_package_path" key.') | 105 '"sub_package_path" key.') |
111 | 106 |
112 new_path = prefix + sub_package_path[index:] | 107 new_path = prefix + sub_package_path[index:] |
113 platform['sub_package_path'] = new_path | 108 platform['sub_package_path'] = new_path |
114 | 109 |
115 Trace(' %s: "%s" -> "%s"' % (nacl_arch, sub_package_path, new_path)) | 110 Trace(' %s: "%s" -> "%s"' % (nacl_arch, sub_package_path, new_path)) |
116 | 111 |
117 with open(manifest, 'w') as f: | 112 with open(options.manifest_json, 'w') as f: |
118 json.dump(data, f, indent=2) | 113 json.dump(data, f, indent=2) |
119 | 114 |
120 return 0 | 115 return 0 |
121 | 116 |
122 | 117 |
123 if __name__ == '__main__': | 118 if __name__ == '__main__': |
124 try: | 119 try: |
125 rtn = main(sys.argv[1:]) | 120 rtn = main(sys.argv[1:]) |
126 except Error, e: | 121 except Error, e: |
127 sys.stderr.write('%s: %s\n' % (os.path.basename(__file__), e)) | 122 sys.stderr.write('%s: %s\n' % (os.path.basename(__file__), e)) |
128 rtn = 1 | 123 rtn = 1 |
129 except KeyboardInterrupt: | 124 except KeyboardInterrupt: |
130 sys.stderr.write('%s: interrupted\n' % os.path.basename(__file__)) | 125 sys.stderr.write('%s: interrupted\n' % os.path.basename(__file__)) |
131 rtn = 1 | 126 rtn = 1 |
132 sys.exit(rtn) | 127 sys.exit(rtn) |
OLD | NEW |