Add UMA for ServiceWorkerURLRequestJob.
[chromium-blink-merge.git] / tools / variations / fieldtrial_util.py
blob4f2521538b79c8bb171bdc587256f832de8ce632
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(base_config_path, platform_config_path=None):
26 try:
27 with open(base_config_path, 'r') as base_file:
28 variations = json.load(base_file)
29 if platform_config_path:
30 try:
31 with open(platform_config_path, 'r') as platform_file:
32 platform_specifics = json.load(platform_file)
33 variations.update(platform_specifics)
34 except (IOError, ValueError):
35 pass
36 except (IOError, ValueError):
37 return []
39 field_trials = []
40 params = []
41 for trial, groups in variations.iteritems():
42 if not len(groups):
43 continue
44 # For now, only take the first group.
45 group = groups[0]
46 trial_group = [trial, group['group_name']]
47 field_trials.extend(trial_group)
48 param_list = []
49 if 'params' in group:
50 for key, value in group['params'].iteritems():
51 param_list.append(key)
52 param_list.append(value)
53 if len(param_list):
54 # Escape the variables for the command-line.
55 trial_group = [_escape(x) for x in trial_group]
56 param_list = [_escape(x) for x in param_list]
57 param = '%s:%s' % ('.'.join(trial_group), '/'.join(param_list))
58 params.append(param)
59 if not len(field_trials):
60 return []
61 args = ['--force-fieldtrials=%s' % '/'.join(field_trials)]
62 if len(params):
63 args.append('--force-fieldtrial-params=%s' % ','.join(params))
64 return args
66 def main():
67 if len(sys.argv) < 3:
68 print 'Usage: fieldtrial_util.py [base_config_path] [platform_config_path]'
69 exit(-1)
70 print GenerateArgs(sys.argv[1], sys.argv[2])
72 if __name__ == '__main__':
73 main()