3 # Copyright (C) 2011 Google Inc. All rights reserved.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
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
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.
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.
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
46 _inspector_directory = "Source/WebCore/inspector/front-end"
47 _localized_strings = "Source/WebCore/English.lproj/localizedStrings.js"
49 _log = logging.getLogger("check-inspector-strings")
51 class StringsExtractor(ProcessorBase):
52 def __init__(self, patterns):
53 self._patterns = patterns
55 for p in self._patterns:
56 self.strings.append([])
58 def should_process(self, file_path):
59 return file_path.endswith(".js") and (not file_path.endswith("InjectedScript.js"))
61 def process(self, lines, file_path, line_numbers=None):
63 comment_start = line.find("//")
64 if comment_start != -1:
65 line = line[:comment_start]
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)
73 class LocalizedStringsExtractor:
75 self.localized_strings = []
77 def process_file(self, file_path):
78 localized_strings_file = codecs.open(file_path, encoding="utf-8", mode="r")
80 contents = localized_strings_file.read()
81 lines = contents.split("\n")
83 match = re.match(r"localizedStrings\[\"((?:[^\"\\]|\\.)*?)\"", line)
85 self.localized_strings.append(match.group(1))
87 localized_strings_file.close()
89 def extract_ui_strings(str, out):
90 line_unrecognized = False
93 idx = str.find("WebInspector.UIString(", idx)
96 idx = idx + len("WebInspector.UIString(")
98 item_recognized = False
101 balance = balance - 1
104 elif str[idx] == '(':
105 balance = balance + 1
109 elif str[idx] == '"':
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])
117 item_recognized = True
119 str_idx = str_idx + 1
121 if not item_recognized:
122 line_unrecognized = True
123 if line_unrecognized:
124 _log.info("Unrecognized: %s" % str)
126 if __name__ == "__main__":
129 cwd = os.path.abspath(os.curdir)
130 filesystem = FileSystem()
131 scm = SCMDetector(filesystem, Executive()).detect_scm_system(cwd)
134 _log.error("WebKit checkout not found: You must run this script "
135 "from within a WebKit checkout.")
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)
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])
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)
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))
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))
170 localized_strings_duplicates.setdefault(s)