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 detect_scm_system
39 from webkitpy.common.system.logutils import configure_logging
40 from webkitpy.style.checker import ProcessorBase
41 from webkitpy.style.filereader import TextFileReader
42 from webkitpy.style.main import change_directory
44 _inspector_directory = "Source/WebCore/inspector/front-end"
45 _devtools_directory = "Source/WebKit/chromium/src/js"
46 _localized_strings = "Source/WebCore/English.lproj/localizedStrings.js"
48 _log = logging.getLogger("check-inspector-strings")
50 def decode_unicode_escapes(s):
51 xNN_converted_to_u00NN = s.replace("\\x", "\\u00")
52 return eval("ur\"" + xNN_converted_to_u00NN + "\"")
54 class StringsExtractor(ProcessorBase):
55 def __init__(self, patterns):
56 self._patterns = patterns
58 for p in self._patterns:
59 self.strings.append([])
61 def should_process(self, file_path):
62 return file_path.endswith(".js") and (not file_path.endswith("InjectedScript.js"))
64 def process(self, lines, file_path, line_numbers=None):
66 comment_start = line.find("//")
67 if comment_start != -1:
68 line = line[:comment_start]
70 for pattern in self._patterns:
71 line_strings = re.findall(pattern, line)
72 for string in line_strings:
73 self.strings[index].append(decode_unicode_escapes(string))
76 class LocalizedStringsExtractor:
78 self.localized_strings = []
80 def process_file(self, file_path):
81 localized_strings_file = codecs.open(file_path, encoding="utf-16", mode="r")
83 contents = localized_strings_file.read()
84 lines = contents.split("\n")
86 match = re.match(r"localizedStrings\[\"((?:[^\"\\]|\\.)*?)\"", line)
88 self.localized_strings.append(decode_unicode_escapes(match.group(1)))
90 localized_strings_file.close()
92 if __name__ == "__main__":
95 cwd = os.path.abspath(os.curdir)
96 scm = detect_scm_system(cwd)
99 _log.error("WebKit checkout not found: You must run this script "
100 "from within a WebKit checkout.")
103 checkout_root = scm.checkout_root
104 _log.debug("WebKit checkout found with root: %s" % checkout_root)
105 change_directory(checkout_root=checkout_root, paths=None)
107 strings_extractor = StringsExtractor([r"WebInspector\.(?:UIString|formatLocalized)\(\"((?:[^\"\\]|\\.)*?)\"", r"\"((?:[^\"\\]|\\.)*?)\""])
108 file_reader = TextFileReader(strings_extractor)
109 file_reader.process_paths([_inspector_directory, _devtools_directory])
110 localized_strings_extractor = LocalizedStringsExtractor()
111 localized_strings_extractor.process_file(_localized_strings)
112 ui_strings = frozenset(strings_extractor.strings[0])
113 strings = frozenset(strings_extractor.strings[1])
114 localized_strings = frozenset(localized_strings_extractor.localized_strings)
116 new_strings = ui_strings - localized_strings
117 for s in new_strings:
118 _log.info("New: \"%s\"" % (s))
119 old_strings = localized_strings - ui_strings
120 suspicious_strings = strings & old_strings
121 for s in suspicious_strings:
122 _log.info("Suspicious: \"%s\"" % (s))
123 unused_strings = old_strings - strings
124 for s in unused_strings:
125 _log.info("Unused: \"%s\"" % (s))
127 localized_strings_duplicates = {}
128 for s in localized_strings_extractor.localized_strings:
129 if s in localized_strings_duplicates:
130 _log.info("Duplicate: \"%s\"" % (s))
132 localized_strings_duplicates.setdefault(s)