Get the features.json files ready for open contributions
[WebKit-https.git] / Tools / Scripts / webkitpy / style / checkers / jsonchecker.py
1 # Copyright (C) 2011 Apple Inc. All rights reserved.
2 #
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions
5 # are met:
6 # 1.  Redistributions of source code must retain the above copyright
7 #     notice, this list of conditions and the following disclaimer.
8 # 2.  Redistributions in binary form must reproduce the above copyright
9 #     notice, this list of conditions and the following disclaimer in the
10 #     documentation and/or other materials provided with the distribution.
11 #
12 # THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
13 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15 # DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR
16 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
18 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
19 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
20 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
21 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22
23 """Checks WebKit style for JSON files."""
24
25 import json
26 import re
27 from sets import Set
28
29 class JSONChecker(object):
30     """Processes JSON lines for checking style."""
31
32     categories = set(('json/syntax',))
33
34     def __init__(self, file_path, handle_style_error):
35         self._handle_style_error = handle_style_error
36         self._handle_style_error.turn_off_line_filtering()
37
38     def check(self, lines):
39         try:
40             json.loads('\n'.join(lines) + '\n')
41         except ValueError, e:
42             self._handle_style_error(self.line_number_from_json_exception(e), 'json/syntax', 5, str(e))
43
44     @staticmethod
45     def line_number_from_json_exception(error):
46         match = re.search(r': line (?P<line>\d+) column \d+', str(error))
47         if not match:
48             return 0
49         return int(match.group('line'))
50
51
52 class JSONContributorsChecker(JSONChecker):
53     """Processes contributors.json lines"""
54
55     def check(self, lines):
56         super(JSONContributorsChecker, self).check(lines)
57         self._handle_style_error(0, 'json/syntax', 5, 'contributors.json should not be modified through the commit queue')
58
59
60 class JSONFeaturesChecker(JSONChecker):
61     """Processes the features.json lines"""
62
63     def check(self, lines):
64         super(JSONFeaturesChecker, self).check(lines)
65
66         try:
67             features_definition = json.loads('\n'.join(lines) + '\n')
68             if 'features' not in features_definition:
69                 self._handle_style_error(0, 'json/syntax', 5, '"features" key not found, the key is mandatory.')
70                 return
71
72             specification_name_set = Set()
73             if 'specification' in features_definition:
74                 previous_specification_name = ''
75                 for specification_object in features_definition['specification']:
76                     if 'name' not in specification_object or not specification_object['name']:
77                         self._handle_style_error(0, 'json/syntax', 5, 'The "name" field is mandatory for specifications.')
78                         continue
79                     name = specification_object['name']
80
81                     if name < previous_specification_name:
82                         self._handle_style_error(0, 'json/syntax', 5, 'The specifications should be sorted alphabetically by name, "%s" appears after "%s".' % (name, previous_specification_name))
83                     previous_specification_name = name
84
85                     specification_name_set.add(name)
86                     if 'url' not in specification_object or not specification_object['url']:
87                         self._handle_style_error(0, 'json/syntax', 5, 'The specifciation "%s" does not have an URL' % name)
88                         continue
89
90             features_list = features_definition['features']
91             previous_feature_name = ''
92             for i in xrange(len(features_list)):
93                 feature = features_list[i]
94                 feature_name = 'Feature %s' % i
95                 if 'name' not in feature or not feature['name']:
96                     self._handle_style_error(0, 'json/syntax', 5, 'The feature %d does not have the mandatory field "name".' % i)
97                 else:
98                     feature_name = feature['name']
99
100                     if feature_name < previous_feature_name:
101                         self._handle_style_error(0, 'json/syntax', 5, 'The features should be sorted alphabetically by name, "%s" appears after "%s".' % (feature_name, previous_feature_name))
102                     previous_feature_name = feature_name
103
104                 if 'status' not in feature or not feature['status']:
105                     self._handle_style_error(0, 'json/syntax', 5, 'The feature "%s" does not have the mandatory field "status".' % feature_name)
106                 if 'specification' in feature:
107                     if feature['specification'] not in specification_name_set:
108                         self._handle_style_error(0, 'json/syntax', 5, 'The feature "%s" has a specification field but no specification of that name exists.' % feature_name)
109         except:
110             pass