Fix gn bootstrap.
[chromium-blink-merge.git] / testing / variations / PRESUBMIT.py
blob9e565e03d7b6b938b3efc3ce546ac779ec84634b
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 """Presubmit script validating field trial configs.
7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
8 for more details on the presubmit API built into depot_tools.
9 """
11 def ValidateData(json_data, file_path, message_type):
12 """Validates the format of a fieldtrial configuration.
14 Args:
15 json_data: Parsed JSON object representing the fieldtrial config.
16 file_path: String representing the path to the JSON file.
17 message_type: Type of message from |output_api| to return in the case of
18 errors/warnings.
20 Returns:
21 A list of |message_type| messages. In the case of all tests passing with no
22 warnings/errors, this will return [].
23 """
24 if not isinstance(json_data, dict):
25 return [message_type(
26 'Malformed config file %s: Expecting dict' % file_path)]
27 for (study, groups) in json_data.iteritems():
28 if not isinstance(study, unicode):
29 return [message_type(
30 'Malformed config file %s: Expecting keys to be string, got %s'
31 % (file_path, type(study)))]
32 if not isinstance(groups, list):
33 return [message_type(
34 'Malformed config file %s: Expecting list for study %s'
35 % (file_path, study))]
36 for group in groups:
37 if not isinstance(group, dict):
38 return [message_type(
39 'Malformed config file %s: Expecting dict for group in '
40 'Study[%s]' % (file_path, study))]
41 if not 'group_name' in group or not isinstance(group['group_name'],
42 unicode):
43 return [message_type(
44 'Malformed config file %s: Missing valid group_name for group'
45 ' in Study[%s]' % (file_path, study))]
46 if 'params' in group:
47 params = group['params']
48 if not isinstance(params, dict):
49 return [message_type(
50 'Malformed config file %s: Invalid params for Group[%s]'
51 ' in Study[%s]' % (file_path, group['group_name'],
52 study))]
53 for (key, value) in params.iteritems():
54 if not isinstance(key, unicode) or not isinstance(value,
55 unicode):
56 return [message_type(
57 'Malformed config file %s: Invalid params for Group[%s]'
58 ' in Study[%s]' % (file_path, group['group_name'],
59 study))]
60 return []
62 def CommonChecks(input_api, output_api):
63 affected_files = input_api.AffectedFiles(
64 include_deletes=False,
65 file_filter=lambda x: x.LocalPath().endswith('.json'))
66 for f in affected_files:
67 contents = input_api.ReadFile(f)
68 try:
69 json_data = input_api.json.loads(contents)
70 result = ValidateData(json_data, f.LocalPath(),
71 output_api.PresubmitError)
72 if len(result):
73 return result
74 except ValueError:
75 return [output_api.PresubmitError(
76 'Malformed JSON file: %s' % f.LocalPath())]
77 return []
79 def CheckChangeOnUpload(input_api, output_api):
80 return CommonChecks(input_api, output_api)
82 def CheckChangeOnCommit(input_api, output_api):
83 return CommonChecks(input_api, output_api)