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 process(self, lines, file_path, line_numbers=None):
59 comment_start = line.find("//")
60 if comment_start != -1:
61 line = line[:comment_start]
63 for pattern in self._patterns:
64 line_strings = re.findall(pattern, line)
65 for string in line_strings:
66 self.strings[index].append(string)
69 class LocalizedStringsExtractor:
71 self.localized_strings = []
73 def process_file(self, file_path):
74 localized_strings_file = codecs.open(file_path, encoding="utf-16", mode="r")
76 contents = localized_strings_file.read()
77 lines = contents.split("\n")
79 match = re.match(r"localizedStrings\[\"((?:[^\"\\]|\\.)*?)\"", line)
81 self.localized_strings.append(match.group(1))
83 localized_strings_file.close()
85 if __name__ == "__main__":
88 checkout = detect_checkout()
90 _log.error("WebKit checkout not found: You must run this script "
91 "from within a WebKit checkout.")
93 checkout_root = checkout.root_path()
94 _log.debug("WebKit checkout found with root: %s" % checkout_root)
95 change_directory(checkout_root=checkout_root, paths=None)
97 strings_extractor = StringsExtractor([r"WebInspector\.(?:UIString|formatLocalized)\(\"((?:[^\"\\]|\\.)*?)\"", r"\"((?:[^\"\\]|\\.)*?)\""])
98 file_reader = TextFileReader(strings_extractor)
99 file_reader.process_paths([_inspector_directory])
100 localized_strings_extractor = LocalizedStringsExtractor()
101 localized_strings_extractor.process_file(_localized_strings)
102 ui_strings = frozenset(strings_extractor.strings[0])
103 strings = frozenset(strings_extractor.strings[1])
104 localized_strings = frozenset(localized_strings_extractor.localized_strings)
106 new_strings = ui_strings - localized_strings
107 for s in new_strings:
108 _log.info("New: \"%s\"" % (s))
109 old_strings = localized_strings - ui_strings
110 suspicious_strings = strings & old_strings
111 for s in suspicious_strings:
112 _log.info("Suspicious: \"%s\"" % (s))
113 unused_strings = old_strings - strings
114 for s in unused_strings:
115 _log.info("Unused: \"%s\"" % (s))