Add ICU message format support
[chromium-blink-merge.git] / tools / variations / fieldtrial_util.py
blobe76d4cdabcdc935364db12b49a938b20a12c72f4
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.
5 import json
6 import sys
9 def _hex(ch):
10 hv = hex(ord(ch)).replace('0x', '')
11 hv.zfill(2)
12 return hv.upper()
14 # URL escapes the delimiter characters from the output. urllib.quote is not
15 # used because it cannot escape '.'.
16 def _escape(str):
17 result = str
18 # Must perform replace on '%' first before the others.
19 for c in '%:/.,':
20 result = result.replace(c, '%' + _hex(c))
21 return result
23 # Generate a list of command-line switches to enable field trials defined in
24 # fieldtrial_testing_config_*.json.
25 def GenerateArgs(config_path):
26 try:
27 with open(config_path, 'r') as base_file:
28 variations = json.load(base_file)
29 except (IOError, ValueError):
30 return []
32 field_trials = []
33 params = []
34 for trial, groups in variations.iteritems():
35 if not len(groups):
36 continue
37 # For now, only take the first group.
38 group = groups[0]
39 trial_group = [trial, group['group_name']]
40 field_trials.extend(trial_group)
41 param_list = []
42 if 'params' in group:
43 for key, value in group['params'].iteritems():
44 param_list.append(key)
45 param_list.append(value)
46 if len(param_list):
47 # Escape the variables for the command-line.
48 trial_group = [_escape(x) for x in trial_group]
49 param_list = [_escape(x) for x in param_list]
50 param = '%s:%s' % ('.'.join(trial_group), '/'.join(param_list))
51 params.append(param)
52 if not len(field_trials):
53 return []
54 args = ['--force-fieldtrials=%s' % '/'.join(field_trials)]
55 if len(params):
56 args.append('--force-fieldtrial-params=%s' % ','.join(params))
57 return args
59 def main():
60 if len(sys.argv) < 3:
61 print 'Usage: fieldtrial_util.py [base_config_path] [platform_config_path]'
62 exit(-1)
63 print GenerateArgs(sys.argv[1], sys.argv[2])
65 if __name__ == '__main__':
66 main()