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
3 # found in the LICENSE file.
8 import fieldtrial_to_struct
11 hv = hex(ord(ch)).replace('0x', '')
15 # URL escapes the delimiter characters from the output. urllib.quote is not
16 # used because it cannot escape '.'.
19 # Must perform replace on '%' first before the others.
21 result = result.replace(c, '%' + _hex(c))
24 def _FindDuplicates(entries):
34 def _CheckForDuplicateFeatures(enable_features, disable_features):
35 enable_features = [f.split('<')[0] for f in enable_features]
36 enable_features_set = set(enable_features)
37 if len(enable_features_set) != len(enable_features):
38 raise Exception('Duplicate feature(s) in enable_features: ' +
39 ', '.join(_FindDuplicates(enable_features)))
41 disable_features = [f.split('<')[0] for f in disable_features]
42 disable_features_set = set(disable_features)
43 if len(disable_features_set) != len(disable_features):
44 raise Exception('Duplicate feature(s) in disable_features: ' +
45 ', '.join(_FindDuplicates(disable_features)))
47 features_in_both = enable_features_set.intersection(disable_features_set)
48 if len(features_in_both) > 0:
49 raise Exception('Conflicting features set as both enabled and disabled: ' +
50 ', '.join(features_in_both))
52 # Generate a list of command-line switches to enable field trials for the
53 # provided config_path and platform.
54 def GenerateArgs(config_path, platform):
56 with open(config_path, 'r') as config_file:
57 config = json.load(config_file)
58 except (IOError, ValueError):
61 platform_studies = fieldtrial_to_struct.ConfigToStudies(config, platform)
68 for study in platform_studies:
69 study_name = study['name']
70 experiments = study['experiments']
71 # For now, only take the first experiment.
72 experiment = experiments[0]
73 selected_study = [study_name, experiment['name']]
74 studies.extend(selected_study)
76 if 'params' in experiment:
77 for param in experiment['params']:
78 param_list.append(param['key'])
79 param_list.append(param['value'])
81 # Escape the variables for the command-line.
82 selected_study = [_escape(x) for x in selected_study]
83 param_list = [_escape(x) for x in param_list]
84 param = '%s:%s' % ('.'.join(selected_study), '/'.join(param_list))
86 for feature in experiment.get('enable_features', []):
87 enable_features.append(feature + '<' + study_name)
88 for feature in experiment.get('disable_features', []):
89 disable_features.append(feature + '<' + study_name)
93 _CheckForDuplicateFeatures(enable_features, disable_features)
94 args = ['--force-fieldtrials=%s' % '/'.join(studies)]
96 args.append('--force-fieldtrial-params=%s' % ','.join(params))
97 if len(enable_features):
98 args.append('--enable-features=%s' % ','.join(enable_features))
99 if len(disable_features):
100 args.append('--disable-features=%s' % ','.join(disable_features))
104 if len(sys.argv) < 3:
105 print 'Usage: fieldtrial_util.py [config_path] [platform]'
106 print 'Optionally pass \'shell_cmd\' as an extra argument to print'
107 print 'quoted command line arguments.'
109 print_shell_cmd = len(sys.argv) >= 4 and sys.argv[3] == 'shell_cmd'
111 supported_platforms = ['android', 'chromeos', 'ios', 'linux', 'mac', 'win',
112 'freebsd', 'dragonfly']
113 if sys.argv[2] not in supported_platforms:
114 print ('\'%s\' is an unknown platform. Supported platforms: %s' %
115 (sys.argv[2], supported_platforms))
118 generated_args = GenerateArgs(sys.argv[1], sys.argv[2])
120 print " ".join(map((lambda arg: '"{0}"'.format(arg)), generated_args))
124 if __name__ == '__main__':