| OLD | NEW |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | |
| 5 """Presubmit script validating field trial configs. | 4 """Presubmit script validating field trial configs. |
| 6 | 5 |
| 7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts | 6 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts |
| 8 for more details on the presubmit API built into depot_tools. | 7 for more details on the presubmit API built into depot_tools. |
| 9 """ | 8 """ |
| 10 | 9 |
| 10 import json |
| 11 import sys |
| 12 |
| 13 |
| 14 def PrettyPrint(contents): |
| 15 """Pretty prints a fieldtrial configuration. |
| 16 |
| 17 Args: |
| 18 contents: File contents as a string. |
| 19 |
| 20 Returns: |
| 21 Pretty printed file contents. |
| 22 """ |
| 23 return json.dumps(json.loads(contents), |
| 24 sort_keys=True, indent=4, |
| 25 separators=(',', ': ')) + '\n' |
| 26 |
| 11 def ValidateData(json_data, file_path, message_type): | 27 def ValidateData(json_data, file_path, message_type): |
| 12 """Validates the format of a fieldtrial configuration. | 28 """Validates the format of a fieldtrial configuration. |
| 13 | 29 |
| 14 Args: | 30 Args: |
| 15 json_data: Parsed JSON object representing the fieldtrial config. | 31 json_data: Parsed JSON object representing the fieldtrial config. |
| 16 file_path: String representing the path to the JSON file. | 32 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 | 33 message_type: Type of message from |output_api| to return in the case of |
| 18 errors/warnings. | 34 errors/warnings. |
| 19 | 35 |
| 20 Returns: | 36 Returns: |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 study))] | 68 study))] |
| 53 for (key, value) in params.iteritems(): | 69 for (key, value) in params.iteritems(): |
| 54 if not isinstance(key, unicode) or not isinstance(value, | 70 if not isinstance(key, unicode) or not isinstance(value, |
| 55 unicode): | 71 unicode): |
| 56 return [message_type( | 72 return [message_type( |
| 57 'Malformed config file %s: Invalid params for Group[%s]' | 73 'Malformed config file %s: Invalid params for Group[%s]' |
| 58 ' in Study[%s]' % (file_path, group['group_name'], | 74 ' in Study[%s]' % (file_path, group['group_name'], |
| 59 study))] | 75 study))] |
| 60 return [] | 76 return [] |
| 61 | 77 |
| 78 def CheckPretty(contents, file_path, message_type): |
| 79 """Validates the pretty printing of fieldtrial configuration. |
| 80 |
| 81 Args: |
| 82 contents: File contents as a string. |
| 83 file_path: String representing the path to the JSON file. |
| 84 message_type: Type of message from |output_api| to return in the case of |
| 85 errors/warnings. |
| 86 |
| 87 Returns: |
| 88 A list of |message_type| messages. In the case of all tests passing with no |
| 89 warnings/errors, this will return []. |
| 90 """ |
| 91 pretty = PrettyPrint(contents) |
| 92 if contents != pretty: |
| 93 return [message_type( |
| 94 'Pretty printing error: Run ' |
| 95 'python testing/variations/PRESUBMIT.py %s' % file_path)] |
| 96 return [] |
| 97 |
| 62 def CommonChecks(input_api, output_api): | 98 def CommonChecks(input_api, output_api): |
| 63 affected_files = input_api.AffectedFiles( | 99 affected_files = input_api.AffectedFiles( |
| 64 include_deletes=False, | 100 include_deletes=False, |
| 65 file_filter=lambda x: x.LocalPath().endswith('.json')) | 101 file_filter=lambda x: x.LocalPath().endswith('.json')) |
| 66 for f in affected_files: | 102 for f in affected_files: |
| 67 contents = input_api.ReadFile(f) | 103 contents = input_api.ReadFile(f) |
| 68 try: | 104 try: |
| 69 json_data = input_api.json.loads(contents) | 105 json_data = input_api.json.loads(contents) |
| 106 result = CheckPretty(contents, f.LocalPath(), output_api.PresubmitError) |
| 107 if len(result): |
| 108 return result |
| 70 result = ValidateData(json_data, f.LocalPath(), | 109 result = ValidateData(json_data, f.LocalPath(), |
| 71 output_api.PresubmitError) | 110 output_api.PresubmitError) |
| 72 if len(result): | 111 if len(result): |
| 73 return result | 112 return result |
| 74 except ValueError: | 113 except ValueError: |
| 75 return [output_api.PresubmitError( | 114 return [output_api.PresubmitError( |
| 76 'Malformed JSON file: %s' % f.LocalPath())] | 115 'Malformed JSON file: %s' % f.LocalPath())] |
| 77 return [] | 116 return [] |
| 78 | 117 |
| 79 def CheckChangeOnUpload(input_api, output_api): | 118 def CheckChangeOnUpload(input_api, output_api): |
| 80 return CommonChecks(input_api, output_api) | 119 return CommonChecks(input_api, output_api) |
| 81 | 120 |
| 82 def CheckChangeOnCommit(input_api, output_api): | 121 def CheckChangeOnCommit(input_api, output_api): |
| 83 return CommonChecks(input_api, output_api) | 122 return CommonChecks(input_api, output_api) |
| 123 |
| 124 |
| 125 def main(argv): |
| 126 content = open(argv[1]).read() |
| 127 pretty = PrettyPrint(content) |
| 128 open(argv[1],'w').write(pretty) |
| 129 |
| 130 if __name__ == "__main__": |
| 131 sys.exit(main(sys.argv)) |
| OLD | NEW |