Initialize Tizen 2.3
[framework/web/webkit-efl.git] / Tools / Scripts / check-inspector-strings
1 #!/usr/bin/env python
2 #
3 # Copyright (C) 2011 Google Inc. All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
7 # met:
8 #
9 #    * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 #    * Redistributions in binary form must reproduce the above
12 # copyright notice, this list of conditions and the following disclaimer
13 # in the documentation and/or other materials provided with the
14 # distribution.
15 #    * Neither the name of Google Inc. nor the names of its
16 # contributors may be used to endorse or promote products derived from
17 # this software without specific prior written permission.
18 #
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 import codecs
32 import logging
33 import os
34 import os.path
35 import re
36 import sys
37
38 from webkitpy.common.checkout.scm import SCMDetector
39 from webkitpy.common.system.filesystem import FileSystem
40 from webkitpy.common.system.executive import Executive
41 from webkitpy.common.system.logutils import configure_logging
42 from webkitpy.style.checker import ProcessorBase
43 from webkitpy.style.filereader import TextFileReader
44 from webkitpy.style.main import change_directory
45
46 _inspector_directory = "Source/WebCore/inspector/front-end"
47 _localized_strings = "Source/WebCore/English.lproj/localizedStrings.js"
48
49 _log = logging.getLogger("check-inspector-strings")
50
51 class StringsExtractor(ProcessorBase):
52     def __init__(self, patterns):
53         self._patterns = patterns
54         self.strings = []
55         for p in self._patterns:
56             self.strings.append([])
57
58     def should_process(self, file_path):
59         return file_path.endswith(".js") and (not file_path.endswith("InjectedScript.js"))
60
61     def process(self, lines, file_path, line_numbers=None):
62         for line in lines:
63             comment_start = line.find("//")
64             if comment_start != -1:
65                 line = line[:comment_start]
66             index = 0
67             for pattern in self._patterns:
68                 line_strings = re.findall(pattern, line)
69                 for string in line_strings:
70                     self.strings[index].append(string)
71                 index += 1
72
73 class LocalizedStringsExtractor:
74     def __init__(self):
75         self.localized_strings = []
76
77     def process_file(self, file_path):
78         localized_strings_file = codecs.open(file_path, encoding="utf-8", mode="r")
79         try:
80             contents = localized_strings_file.read()
81             lines = contents.split("\n")
82             for line in lines:
83                 match = re.match(r"localizedStrings\[\"((?:[^\"\\]|\\.)*?)\"", line)
84                 if match:
85                     self.localized_strings.append(match.group(1))
86         finally:
87             localized_strings_file.close()
88
89 def extract_ui_strings(str, out):
90     line_unrecognized = False
91     idx = 0
92     while idx < len(str):
93         idx = str.find("WebInspector.UIString(", idx)
94         if idx == -1:
95             break
96         idx = idx + len("WebInspector.UIString(")
97         balance = 1
98         item_recognized = False
99         while idx < len(str):
100             if str[idx] == ')':
101                 balance = balance - 1
102                 if balance == 0:
103                     break
104             elif str[idx] == '(':
105                 balance = balance + 1
106             elif balance == 1:
107                 if str[idx] == ',':
108                     break
109                 elif str[idx] == '"':
110                     str_idx = idx + 1
111                     while str_idx < len(str):
112                         if str[str_idx] == '\\':
113                             str_idx = str_idx + 1
114                         elif str[str_idx] == '"':
115                             out.add(str[idx + 1 : str_idx])
116                             idx = str_idx
117                             item_recognized = True
118                             break
119                         str_idx = str_idx + 1
120             idx = idx + 1
121         if not item_recognized:
122             line_unrecognized = True
123     if line_unrecognized:
124         _log.info("Unrecognized: %s" % str)
125
126 if __name__ == "__main__":
127     configure_logging()
128
129     cwd = os.path.abspath(os.curdir)
130     filesystem = FileSystem()
131     scm = SCMDetector(filesystem, Executive()).detect_scm_system(cwd)
132
133     if scm is None:
134         _log.error("WebKit checkout not found: You must run this script "
135                    "from within a WebKit checkout.")
136         sys.exit(1)
137
138     checkout_root = scm.checkout_root
139     _log.debug("WebKit checkout found with root: %s" % checkout_root)
140     change_directory(filesystem, checkout_root=checkout_root, paths=None)
141
142     strings_extractor = StringsExtractor([r"(WebInspector\.UIString\(.*)", r"\"((?:[^\"\\]|\\.)*?)\""])
143     file_reader = TextFileReader(filesystem, strings_extractor)
144     file_reader.process_paths([_inspector_directory])
145     localized_strings_extractor = LocalizedStringsExtractor()
146     localized_strings_extractor.process_file(_localized_strings)
147     raw_ui_strings = frozenset(strings_extractor.strings[0])
148     ui_strings = set()
149     for s in raw_ui_strings:
150         extract_ui_strings(s, ui_strings)
151     strings = frozenset(strings_extractor.strings[1])
152     localized_strings = frozenset(localized_strings_extractor.localized_strings)
153
154     new_strings = ui_strings - localized_strings
155     for s in new_strings:
156         _log.info("New: \"%s\"" % (s))
157     old_strings = localized_strings - ui_strings
158     suspicious_strings = strings & old_strings
159     for s in suspicious_strings:
160         _log.info("Suspicious: \"%s\"" % (s))
161     unused_strings = old_strings - strings
162     for s in unused_strings:
163         _log.info("Unused: \"%s\"" % (s))
164
165     localized_strings_duplicates = {}
166     for s in localized_strings_extractor.localized_strings:
167         if s in localized_strings_duplicates:
168             _log.info("Duplicate: \"%s\"" % (s))
169         else:
170             localized_strings_duplicates.setdefault(s)