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.
36 from webkitpy.style_references import detect_checkout
37 from webkitpy.common.system.logutils import configure_logging
38 from webkitpy.style.checker import ProcessorBase
39 from webkitpy.style.filereader import TextFileReader
40 from webkitpy.style.main import change_directory
42 _inspector_directory = "Source/WebCore/inspector/front-end"
43 _localized_strings = "Source/WebCore/English.lproj/localizedStrings.js"
45 _log = logging.getLogger("check-inspector-strings")
47 class StringsExtractor(ProcessorBase):
48 def __init__(self, patterns):
49 self._patterns = patterns
51 for p in self._patterns:
52 self.strings.append([])
54 def should_process(self, file_path):
55 return file_path.endswith(".js") and (not file_path.endswith("InjectedScript.js"))
57 def decode_unicode_escapes(self, s):
58 return eval("ur\"" + s + "\"")
60 def process(self, lines, file_path, line_numbers=None):
62 comment_start = line.find("//")
63 if comment_start != -1:
64 line = line[:comment_start]
66 for pattern in self._patterns:
67 line_strings = re.findall(pattern, line)
68 for string in line_strings:
69 self.strings[index].append(self.decode_unicode_escapes(string))
72 class LocalizedStringsExtractor:
74 self.localized_strings = []
76 def process_file(self, file_path):
77 localized_strings_file = codecs.open(file_path, encoding="utf-16", mode="r")
79 contents = localized_strings_file.read()
80 lines = contents.split("\n")
82 match = re.match(r"localizedStrings\[\"((?:[^\"\\]|\\.)*?)\"", line)
84 self.localized_strings.append(match.group(1))
86 localized_strings_file.close()
88 if __name__ == "__main__":
91 checkout = detect_checkout()
93 _log.error("WebKit checkout not found: You must run this script "
94 "from within a WebKit checkout.")
96 checkout_root = checkout.root_path()
97 _log.debug("WebKit checkout found with root: %s" % checkout_root)
98 change_directory(checkout_root=checkout_root, paths=None)
100 strings_extractor = StringsExtractor([r"WebInspector\.(?:UIString|formatLocalized)\(\"((?:[^\"\\]|\\.)*?)\"", r"\"((?:[^\"\\]|\\.)*?)\""])
101 file_reader = TextFileReader(strings_extractor)
102 file_reader.process_paths([_inspector_directory])
103 localized_strings_extractor = LocalizedStringsExtractor()
104 localized_strings_extractor.process_file(_localized_strings)
105 ui_strings = frozenset(strings_extractor.strings[0])
106 strings = frozenset(strings_extractor.strings[1])
107 localized_strings = frozenset(localized_strings_extractor.localized_strings)
109 new_strings = ui_strings - localized_strings
110 for s in new_strings:
111 _log.info("New: \"%s\"" % (s))
112 old_strings = localized_strings - ui_strings
113 suspicious_strings = strings & old_strings
114 for s in suspicious_strings:
115 _log.info("Suspicious: \"%s\"" % (s))
116 unused_strings = old_strings - strings
117 for s in unused_strings:
118 _log.info("Unused: \"%s\"" % (s))
120 localized_strings_duplicates = {}
121 for s in localized_strings_extractor.localized_strings:
122 if s in localized_strings_duplicates:
123 _log.info("Duplicate: \"%s\"" % (s))
125 localized_strings_duplicates.setdefault(s)