OLD | NEW |
1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import json | 5 import json |
6 import sys | 6 import sys |
7 | 7 |
8 import fieldtrial_to_struct | |
9 | 8 |
10 def _hex(ch): | 9 def _hex(ch): |
11 hv = hex(ord(ch)).replace('0x', '') | 10 hv = hex(ord(ch)).replace('0x', '') |
12 hv.zfill(2) | 11 hv.zfill(2) |
13 return hv.upper() | 12 return hv.upper() |
14 | 13 |
15 # URL escapes the delimiter characters from the output. urllib.quote is not | 14 # URL escapes the delimiter characters from the output. urllib.quote is not |
16 # used because it cannot escape '.'. | 15 # used because it cannot escape '.'. |
17 def _escape(str): | 16 def _escape(str): |
18 result = str | 17 result = str |
(...skipping 19 matching lines...) Expand all Loading... |
38 ', '.join(_FindDuplicates(enable_features))) | 37 ', '.join(_FindDuplicates(enable_features))) |
39 disable_features_set = set(disable_features) | 38 disable_features_set = set(disable_features) |
40 if len(disable_features_set) != len(disable_features): | 39 if len(disable_features_set) != len(disable_features): |
41 raise Exception('Duplicate feature(s) in disable_features: ' + | 40 raise Exception('Duplicate feature(s) in disable_features: ' + |
42 ', '.join(_FindDuplicates(disable_features))) | 41 ', '.join(_FindDuplicates(disable_features))) |
43 features_in_both = enable_features_set.intersection(disable_features_set) | 42 features_in_both = enable_features_set.intersection(disable_features_set) |
44 if len(features_in_both) > 0: | 43 if len(features_in_both) > 0: |
45 raise Exception('Conflicting features set as both enabled and disabled: ' + | 44 raise Exception('Conflicting features set as both enabled and disabled: ' + |
46 ', '.join(features_in_both)) | 45 ', '.join(features_in_both)) |
47 | 46 |
48 # Generate a list of command-line switches to enable field trials for the | 47 # Generate a list of command-line switches to enable field trials defined in |
49 # provided config_path and platform. | 48 # fieldtrial_testing_config_*.json. |
50 def GenerateArgs(config_path, platform): | 49 def GenerateArgs(config_path): |
51 try: | 50 try: |
52 with open(config_path, 'r') as config_file: | 51 with open(config_path, 'r') as base_file: |
53 config = json.load(config_file) | 52 variations = json.load(base_file) |
54 except (IOError, ValueError): | 53 except (IOError, ValueError): |
55 return [] | 54 return [] |
56 | 55 |
57 platform_studies = fieldtrial_to_struct.ConfigToStudies(config, platform) | 56 field_trials = [] |
58 | |
59 studies = [] | |
60 params = [] | 57 params = [] |
61 enable_features = [] | 58 enable_features = [] |
62 disable_features = [] | 59 disable_features = [] |
63 | 60 for trial, groups in variations.iteritems(): |
64 for study in platform_studies: | 61 if not len(groups): |
65 study_name = study['name'] | 62 continue |
66 experiments = study['experiments'] | 63 # For now, only take the first group. |
67 # For now, only take the first experiment. | 64 group = groups[0] |
68 experiment = experiments[0] | 65 trial_group = [trial, group['group_name']] |
69 selected_study = [study_name, experiment['name']] | 66 field_trials.extend(trial_group) |
70 studies.extend(selected_study) | |
71 param_list = [] | 67 param_list = [] |
72 if 'params' in experiment: | 68 if 'params' in group: |
73 for param in experiment['params']: | 69 for key, value in group['params'].iteritems(): |
74 param_list.append(param['key']) | 70 param_list.append(key) |
75 param_list.append(param['value']) | 71 param_list.append(value) |
76 if len(param_list): | 72 if len(param_list): |
77 # Escape the variables for the command-line. | 73 # Escape the variables for the command-line. |
78 selected_study = [_escape(x) for x in selected_study] | 74 trial_group = [_escape(x) for x in trial_group] |
79 param_list = [_escape(x) for x in param_list] | 75 param_list = [_escape(x) for x in param_list] |
80 param = '%s:%s' % ('.'.join(selected_study), '/'.join(param_list)) | 76 param = '%s:%s' % ('.'.join(trial_group), '/'.join(param_list)) |
81 params.append(param) | 77 params.append(param) |
82 if 'enable_features' in experiment: | 78 if 'enable_features' in group: |
83 enable_features.extend(experiment['enable_features']) | 79 enable_features.extend(group['enable_features']) |
84 if 'disable_features' in experiment: | 80 if 'disable_features' in group: |
85 disable_features.extend(experiment['disable_features']) | 81 disable_features.extend(group['disable_features']) |
86 | 82 if not len(field_trials): |
87 if not len(studies): | |
88 return [] | 83 return [] |
89 _CheckForDuplicateFeatures(enable_features, disable_features) | 84 _CheckForDuplicateFeatures(enable_features, disable_features) |
90 args = ['--force-fieldtrials=%s' % '/'.join(studies)] | 85 args = ['--force-fieldtrials=%s' % '/'.join(field_trials)] |
91 if len(params): | 86 if len(params): |
92 args.append('--force-fieldtrial-params=%s' % ','.join(params)) | 87 args.append('--force-fieldtrial-params=%s' % ','.join(params)) |
93 if len(enable_features): | 88 if len(enable_features): |
94 args.append('--enable-features=%s' % ','.join(enable_features)) | 89 args.append('--enable-features=%s' % ','.join(enable_features)) |
95 if len(disable_features): | 90 if len(disable_features): |
96 args.append('--disable-features=%s' % ','.join(disable_features)) | 91 args.append('--disable-features=%s' % ','.join(disable_features)) |
97 return args | 92 return args |
98 | 93 |
99 def main(): | 94 def main(): |
100 if len(sys.argv) < 3: | 95 if len(sys.argv) < 3: |
101 print 'Usage: fieldtrial_util.py [config_path] [platform]' | 96 print 'Usage: fieldtrial_util.py [base_config_path] [platform_config_path]' |
102 exit(-1) | 97 exit(-1) |
103 | |
104 supported_platforms = ['android', 'chromeos', 'ios', 'linux', 'mac', 'win'] | |
105 if sys.argv[2] not in supported_platforms: | |
106 print ('\'%s\' is an unknown platform. Supported platforms: %s' % | |
107 (sys.argv[2], supported_platforms)) | |
108 exit(-1) | |
109 | |
110 print GenerateArgs(sys.argv[1], sys.argv[2]) | 98 print GenerateArgs(sys.argv[1], sys.argv[2]) |
111 | 99 |
112 if __name__ == '__main__': | 100 if __name__ == '__main__': |
113 main() | 101 main() |
OLD | NEW |