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.
11 def ValidateData(json_data
, file_path
, message_type
):
12 """Validates the format of a fieldtrial configuration.
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
21 A list of |message_type| messages. In the case of all tests passing with no
22 warnings/errors, this will return [].
24 if not isinstance(json_data
, dict):
26 'Malformed config file %s: Expecting dict' % file_path
)]
27 for (study
, groups
) in json_data
.iteritems():
28 if not isinstance(study
, unicode):
30 'Malformed config file %s: Expecting keys to be string, got %s'
31 % (file_path
, type(study
)))]
32 if not isinstance(groups
, list):
34 'Malformed config file %s: Expecting list for study %s'
35 % (file_path
, study
))]
37 if not isinstance(group
, dict):
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'],
44 'Malformed config file %s: Missing valid group_name for group'
45 ' in Study[%s]' % (file_path
, study
))]
47 params
= group
['params']
48 if not isinstance(params
, dict):
50 'Malformed config file %s: Invalid params for Group[%s]'
51 ' in Study[%s]' % (file_path
, group
['group_name'],
53 for (key
, value
) in params
.iteritems():
54 if not isinstance(key
, unicode) or not isinstance(value
,
57 'Malformed config file %s: Invalid params for Group[%s]'
58 ' in Study[%s]' % (file_path
, group
['group_name'],
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
)
69 json_data
= input_api
.json
.loads(contents
)
70 result
= ValidateData(json_data
, f
.LocalPath(),
71 output_api
.PresubmitError
)
75 return [output_api
.PresubmitError(
76 'Malformed JSON file: %s' % f
.LocalPath())]
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
)