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
_devtools_directory = "Source/WebKit/chromium/src/js"
48
_localized_strings = "Source/WebCore/English.lproj/localizedStrings.js"
50
_log = logging.getLogger("check-inspector-strings")
52
class StringsExtractor(ProcessorBase):
53
def __init__(self, patterns):
54
self._patterns = patterns
56
for p in self._patterns:
57
self.strings.append([])
59
def should_process(self, file_path):
60
return file_path.endswith(".js") and (not file_path.endswith("InjectedScript.js"))
62
def process(self, lines, file_path, line_numbers=None):
64
comment_start = line.find("//")
65
if comment_start != -1:
66
line = line[:comment_start]
68
for pattern in self._patterns:
69
line_strings = re.findall(pattern, line)
70
for string in line_strings:
71
self.strings[index].append(string)
74
class LocalizedStringsExtractor:
76
self.localized_strings = []
78
def process_file(self, file_path):
79
localized_strings_file = codecs.open(file_path, encoding="utf-8", mode="r")
81
contents = localized_strings_file.read()
82
lines = contents.split("\n")
84
match = re.match(r"localizedStrings\[\"((?:[^\"\\]|\\.)*?)\"", line)
86
self.localized_strings.append(match.group(1))
88
localized_strings_file.close()
90
def extract_ui_strings(str, out):
91
line_unrecognized = False
94
idx = str.find("WebInspector.UIString(", idx)
97
idx = idx + len("WebInspector.UIString(")
99
item_recognized = False
100
while idx < len(str):
102
balance = balance - 1
105
elif str[idx] == '(':
106
balance = balance + 1
110
elif str[idx] == '"':
112
while str_idx < len(str):
113
if str[str_idx] == '\\':
114
str_idx = str_idx + 1
115
elif str[str_idx] == '"':
116
out.add(str[idx + 1 : str_idx])
118
item_recognized = True
120
str_idx = str_idx + 1
122
if not item_recognized:
123
line_unrecognized = True
124
if line_unrecognized:
125
_log.info("Unrecognized: %s" % str)
127
if __name__ == "__main__":
130
cwd = os.path.abspath(os.curdir)
131
filesystem = FileSystem()
132
scm = SCMDetector(filesystem, Executive()).detect_scm_system(cwd)
135
_log.error("WebKit checkout not found: You must run this script "
136
"from within a WebKit checkout.")
139
checkout_root = scm.checkout_root
140
_log.debug("WebKit checkout found with root: %s" % checkout_root)
141
change_directory(filesystem, checkout_root=checkout_root, paths=None)
143
strings_extractor = StringsExtractor([r"(WebInspector\.UIString\(.*)", r"\"((?:[^\"\\]|\\.)*?)\""])
144
file_reader = TextFileReader(filesystem, strings_extractor)
145
file_reader.process_paths([_inspector_directory, _devtools_directory])
146
localized_strings_extractor = LocalizedStringsExtractor()
147
localized_strings_extractor.process_file(_localized_strings)
148
raw_ui_strings = frozenset(strings_extractor.strings[0])
150
for s in raw_ui_strings:
151
extract_ui_strings(s, ui_strings)
152
strings = frozenset(strings_extractor.strings[1])
153
localized_strings = frozenset(localized_strings_extractor.localized_strings)
155
new_strings = ui_strings - localized_strings
156
for s in new_strings:
157
_log.info("New: \"%s\"" % (s))
158
old_strings = localized_strings - ui_strings
159
suspicious_strings = strings & old_strings
160
for s in suspicious_strings:
161
_log.info("Suspicious: \"%s\"" % (s))
162
unused_strings = old_strings - strings
163
for s in unused_strings:
164
_log.info("Unused: \"%s\"" % (s))
166
localized_strings_duplicates = {}
167
for s in localized_strings_extractor.localized_strings:
168
if s in localized_strings_duplicates:
169
_log.info("Duplicate: \"%s\"" % (s))
171
localized_strings_duplicates.setdefault(s)