OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2015 The Chromium Authors. All rights reserved. | 2 # Copyright 2015 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 import itertools | |
6 import json | 7 import json |
7 import os.path | 8 import os.path |
8 import sys | 9 import sys |
9 import optparse | 10 import optparse |
10 _script_path = os.path.realpath(__file__) | 11 _script_path = os.path.realpath(__file__) |
11 | 12 |
12 sys.path.insert(0, os.path.normpath(_script_path + "/../../json_comment_eater")) | 13 sys.path.insert(0, os.path.normpath(_script_path + "/../../json_comment_eater")) |
13 try: | 14 try: |
14 import json_comment_eater | 15 import json_comment_eater |
15 finally: | 16 finally: |
16 sys.path.pop(0) | 17 sys.path.pop(0) |
17 | 18 |
18 sys.path.insert(0, os.path.normpath(_script_path + "/../../json_to_struct")) | 19 sys.path.insert(0, os.path.normpath(_script_path + "/../../json_to_struct")) |
19 try: | 20 try: |
20 import json_to_struct | 21 import json_to_struct |
21 finally: | 22 finally: |
22 sys.path.pop(0) | 23 sys.path.pop(0) |
23 | 24 |
24 def _Load(filename): | 25 def _Load(filename): |
25 """Loads a JSON file into a Python object and return this object. | 26 """Loads a JSON file into a Python object and return this object. |
26 """ | 27 """ |
27 with open(filename, 'r') as handle: | 28 with open(filename, 'r') as handle: |
28 result = json.loads(json_comment_eater.Nom(handle.read())) | 29 result = json.loads(json_comment_eater.Nom(handle.read())) |
29 return result | 30 return result |
30 | 31 |
31 def _LoadFieldTrialConfig(filename): | 32 def _LoadFieldTrialConfig(filename, platform): |
32 """Loads a field trial config JSON and converts it into a format that can be | 33 """Loads a field trial config JSON and converts it into a format that can be |
33 used by json_to_struct. | 34 used by json_to_struct. |
34 """ | 35 """ |
35 return _FieldTrialConfigToDescription(_Load(filename)) | 36 return _FieldTrialConfigToDescription(_Load(filename), platform) |
36 | 37 |
37 def _CreateGroup(group_data): | 38 def _CreateGroup(group_data): |
38 group = {'name': group_data['group_name']} | 39 group = {'name': group_data['group_name']} |
39 params_data = group_data.get('params') | 40 params_data = group_data.get('params') |
40 if (params_data): | 41 if (params_data): |
41 group['params'] = [{'key': param, 'value': params_data[param]} | 42 group['params'] = [{'key': param, 'value': params_data[param]} |
42 for param in sorted(params_data.keys())]; | 43 for param in sorted(params_data.keys())]; |
43 enable_features_data = group_data.get('enable_features') | 44 enable_features_data = group_data.get('enable_features') |
44 if enable_features_data: | 45 if enable_features_data: |
45 group['enable_features'] = enable_features_data | 46 group['enable_features'] = enable_features_data |
46 disable_features_data = group_data.get('disable_features') | 47 disable_features_data = group_data.get('disable_features') |
47 if disable_features_data: | 48 if disable_features_data: |
48 group['disable_features'] = disable_features_data | 49 group['disable_features'] = disable_features_data |
49 return group | 50 return group |
50 | 51 |
51 def _CreateTrial(trial_name, groups): | 52 def _CreateTrial(trial_name, group_configs, platform): |
Alexei Svitkine (slow)
2016/08/31 20:17:27
Please add a comment to this function now that it'
robliao
2016/09/01 21:49:07
Done.
| |
53 platform_group_lists = [config['groups'] for config in group_configs | |
54 if platform in config['platforms']] | |
55 platform_groups = list(itertools.chain.from_iterable(platform_group_lists)) | |
52 return { | 56 return { |
53 'name': trial_name, | 57 'name': trial_name, |
54 'groups': [_CreateGroup(group) for group in groups], | 58 'groups': [_CreateGroup(group) for group in platform_groups], |
55 } | 59 } |
56 | 60 |
57 def _FieldTrialConfigToDescription(config): | 61 def _GenerateTrials(config, platform): |
62 for trial_name in sorted(config.keys()): | |
63 trial = _CreateTrial(trial_name, config[trial_name], platform) | |
64 if trial['groups']: | |
65 yield trial | |
66 | |
67 def _FieldTrialConfigToDescription(config, platform): | |
58 return { | 68 return { |
59 'elements': { | 69 'elements': { |
60 'kFieldTrialConfig': { | 70 'kFieldTrialConfig': { |
61 'trials': [_CreateTrial(trial_name, config[trial_name]) | 71 'trials': [trial for trial in _GenerateTrials(config, platform)] |
62 for trial_name in sorted(config.keys())] | |
63 } | 72 } |
64 } | 73 } |
65 } | 74 } |
66 | 75 |
67 def main(arguments): | 76 def main(arguments): |
68 parser = optparse.OptionParser( | 77 parser = optparse.OptionParser( |
69 description='Generates a struct from a JSON description.', | 78 description='Generates a struct from a JSON description.', |
70 usage='usage: %prog [option] -s schema description') | 79 usage='usage: %prog [option] -s schema -p platform description') |
71 parser.add_option('-b', '--destbase', | 80 parser.add_option('-b', '--destbase', |
72 help='base directory of generated files.') | 81 help='base directory of generated files.') |
73 parser.add_option('-d', '--destdir', | 82 parser.add_option('-d', '--destdir', |
74 help='directory to output generated files, relative to destbase.') | 83 help='directory to output generated files, relative to destbase.') |
75 parser.add_option('-n', '--namespace', | 84 parser.add_option('-n', '--namespace', |
76 help='C++ namespace for generated files. e.g search_providers.') | 85 help='C++ namespace for generated files. e.g search_providers.') |
86 parser.add_option('-p', '--platform', | |
87 help='target platform for the field trial, mandatory.') | |
77 parser.add_option('-s', '--schema', help='path to the schema file, ' | 88 parser.add_option('-s', '--schema', help='path to the schema file, ' |
78 'mandatory.') | 89 'mandatory.') |
79 parser.add_option('-o', '--output', help='output filename, ' | 90 parser.add_option('-o', '--output', help='output filename, ' |
80 'mandatory.') | 91 'mandatory.') |
81 parser.add_option('-y', '--year', | 92 parser.add_option('-y', '--year', |
82 help='year to put in the copy-right.') | 93 help='year to put in the copy-right.') |
83 (opts, args) = parser.parse_args(args=arguments) | 94 (opts, args) = parser.parse_args(args=arguments) |
84 | 95 |
85 if not opts.schema: | 96 if not opts.schema: |
86 parser.error('You must specify a --schema.') | 97 parser.error('You must specify a --schema.') |
87 | 98 |
99 if not opts.platform: | |
100 parser.error('You must specify a --platform.') | |
101 | |
102 supported_platforms = ['android', 'chromeos', 'ios', 'linux', 'mac', 'win'] | |
103 if opts.platform not in supported_platforms: | |
104 parser.error('\'%s\' is an unknown platform. Supported platforms: %s' % | |
105 (opts.platform, supported_platforms)) | |
106 | |
88 description_filename = os.path.normpath(args[0]) | 107 description_filename = os.path.normpath(args[0]) |
89 shortroot = opts.output | 108 shortroot = opts.output |
90 if opts.destdir: | 109 if opts.destdir: |
91 output_root = os.path.join(os.path.normpath(opts.destdir), shortroot) | 110 output_root = os.path.join(os.path.normpath(opts.destdir), shortroot) |
92 else: | 111 else: |
93 output_root = shortroot | 112 output_root = shortroot |
94 | 113 |
95 if opts.destbase: | 114 if opts.destbase: |
96 basepath = os.path.normpath(opts.destbase) | 115 basepath = os.path.normpath(opts.destbase) |
97 else: | 116 else: |
98 basepath = '' | 117 basepath = '' |
99 | 118 |
100 schema = _Load(opts.schema) | 119 schema = _Load(opts.schema) |
101 description = _LoadFieldTrialConfig(description_filename) | 120 description = _LoadFieldTrialConfig(description_filename, opts.platform) |
102 json_to_struct.GenerateStruct( | 121 json_to_struct.GenerateStruct( |
103 basepath, output_root, opts.namespace, schema, description, | 122 basepath, output_root, opts.namespace, schema, description, |
104 os.path.split(description_filename)[1], os.path.split(opts.schema)[1], | 123 os.path.split(description_filename)[1], os.path.split(opts.schema)[1], |
105 opts.year) | 124 opts.year) |
106 | 125 |
107 if __name__ == '__main__': | 126 if __name__ == '__main__': |
108 main(sys.argv[1:]) | 127 main(sys.argv[1:]) |
OLD | NEW |