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 _CreateExperiment(experiment_data): |
38 group = {'name': group_data['group_name']} | 39 experiment = {'name': experiment_data['name']} |
39 params_data = group_data.get('params') | 40 params_data = experiment_data.get('params') |
40 if (params_data): | 41 if (params_data): |
41 group['params'] = [{'key': param, 'value': params_data[param]} | 42 experiment['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 = experiment_data.get('enable_features') |
44 if enable_features_data: | 45 if enable_features_data: |
45 group['enable_features'] = enable_features_data | 46 experiment['enable_features'] = enable_features_data |
46 disable_features_data = group_data.get('disable_features') | 47 disable_features_data = experiment_data.get('disable_features') |
47 if disable_features_data: | 48 if disable_features_data: |
48 group['disable_features'] = disable_features_data | 49 experiment['disable_features'] = disable_features_data |
49 return group | 50 return experiment |
50 | 51 |
51 def _CreateTrial(trial_name, groups): | 52 def _CreateTrial(study_name, experiment_configs, platform): |
53 """Returns the applicable experiments for |study_name| and |platform|. This | |
54 iterates through all of the experiment_configs for |study_name| and picks out | |
55 the applicable experiments based off of the valid platforms. | |
56 """ | |
57 platform_experiment_lists = [ | |
58 config['experiments'] for config in experiment_configs | |
59 if platform in config['platforms']] | |
60 platform_experiments = list(itertools.chain.from_iterable( | |
61 platform_experiment_lists)) | |
52 return { | 62 return { |
53 'name': trial_name, | 63 'name': study_name, |
54 'groups': [_CreateGroup(group) for group in groups], | 64 'groups': [_CreateExperiment(experiment) |
65 for experiment in platform_experiments], | |
55 } | 66 } |
56 | 67 |
57 def _FieldTrialConfigToDescription(config): | 68 def _GenerateTrials(config, platform): |
69 for study_name in sorted(config.keys()): | |
70 study = _CreateTrial(study_name, config[study_name], platform) | |
71 if study['groups']: | |
Alexei Svitkine (slow)
2016/09/19 16:59:39
Can you add a comment above this that explains thi
robliao
2016/09/19 17:16:26
Done.
| |
72 yield study | |
73 | |
74 def _FieldTrialConfigToDescription(config, platform): | |
58 return { | 75 return { |
59 'elements': { | 76 'elements': { |
60 'kFieldTrialConfig': { | 77 'kFieldTrialConfig': { |
61 'trials': [_CreateTrial(trial_name, config[trial_name]) | 78 'trials': [study for study in _GenerateTrials(config, platform)] |
62 for trial_name in sorted(config.keys())] | |
63 } | 79 } |
64 } | 80 } |
65 } | 81 } |
66 | 82 |
67 def main(arguments): | 83 def main(arguments): |
68 parser = optparse.OptionParser( | 84 parser = optparse.OptionParser( |
69 description='Generates a struct from a JSON description.', | 85 description='Generates a struct from a JSON description.', |
70 usage='usage: %prog [option] -s schema description') | 86 usage='usage: %prog [option] -s schema -p platform description') |
71 parser.add_option('-b', '--destbase', | 87 parser.add_option('-b', '--destbase', |
72 help='base directory of generated files.') | 88 help='base directory of generated files.') |
73 parser.add_option('-d', '--destdir', | 89 parser.add_option('-d', '--destdir', |
74 help='directory to output generated files, relative to destbase.') | 90 help='directory to output generated files, relative to destbase.') |
75 parser.add_option('-n', '--namespace', | 91 parser.add_option('-n', '--namespace', |
76 help='C++ namespace for generated files. e.g search_providers.') | 92 help='C++ namespace for generated files. e.g search_providers.') |
93 parser.add_option('-p', '--platform', | |
94 help='target platform for the field trial, mandatory.') | |
77 parser.add_option('-s', '--schema', help='path to the schema file, ' | 95 parser.add_option('-s', '--schema', help='path to the schema file, ' |
78 'mandatory.') | 96 'mandatory.') |
79 parser.add_option('-o', '--output', help='output filename, ' | 97 parser.add_option('-o', '--output', help='output filename, ' |
80 'mandatory.') | 98 'mandatory.') |
81 parser.add_option('-y', '--year', | 99 parser.add_option('-y', '--year', |
82 help='year to put in the copy-right.') | 100 help='year to put in the copy-right.') |
83 (opts, args) = parser.parse_args(args=arguments) | 101 (opts, args) = parser.parse_args(args=arguments) |
84 | 102 |
85 if not opts.schema: | 103 if not opts.schema: |
86 parser.error('You must specify a --schema.') | 104 parser.error('You must specify a --schema.') |
87 | 105 |
106 if not opts.platform: | |
107 parser.error('You must specify a --platform.') | |
108 | |
109 supported_platforms = ['android', 'chromeos', 'ios', 'linux', 'mac', 'win'] | |
110 if opts.platform not in supported_platforms: | |
111 parser.error('\'%s\' is an unknown platform. Supported platforms: %s' % | |
112 (opts.platform, supported_platforms)) | |
113 | |
88 description_filename = os.path.normpath(args[0]) | 114 description_filename = os.path.normpath(args[0]) |
89 shortroot = opts.output | 115 shortroot = opts.output |
90 if opts.destdir: | 116 if opts.destdir: |
91 output_root = os.path.join(os.path.normpath(opts.destdir), shortroot) | 117 output_root = os.path.join(os.path.normpath(opts.destdir), shortroot) |
92 else: | 118 else: |
93 output_root = shortroot | 119 output_root = shortroot |
94 | 120 |
95 if opts.destbase: | 121 if opts.destbase: |
96 basepath = os.path.normpath(opts.destbase) | 122 basepath = os.path.normpath(opts.destbase) |
97 else: | 123 else: |
98 basepath = '' | 124 basepath = '' |
99 | 125 |
100 schema = _Load(opts.schema) | 126 schema = _Load(opts.schema) |
101 description = _LoadFieldTrialConfig(description_filename) | 127 description = _LoadFieldTrialConfig(description_filename, opts.platform) |
102 json_to_struct.GenerateStruct( | 128 json_to_struct.GenerateStruct( |
103 basepath, output_root, opts.namespace, schema, description, | 129 basepath, output_root, opts.namespace, schema, description, |
104 os.path.split(description_filename)[1], os.path.split(opts.schema)[1], | 130 os.path.split(description_filename)[1], os.path.split(opts.schema)[1], |
105 opts.year) | 131 opts.year) |
106 | 132 |
107 if __name__ == '__main__': | 133 if __name__ == '__main__': |
108 main(sys.argv[1:]) | 134 main(sys.argv[1:]) |
OLD | NEW |