check-inspector-strings 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #!/usr/bin/env python
  2. #
  3. # Copyright (C) 2011 Google Inc. All rights reserved.
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions are
  7. # met:
  8. #
  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
  14. # distribution.
  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.
  18. #
  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.
  30. import codecs
  31. import logging
  32. import os
  33. import os.path
  34. import re
  35. import sys
  36. from webkitpy.common.checkout.scm import SCMDetector
  37. from webkitpy.common.system.filesystem import FileSystem
  38. from webkitpy.common.system.executive import Executive
  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
  43. _inspector_directory = "Source/WebCore/inspector/front-end"
  44. _localized_strings = "Source/WebCore/English.lproj/localizedStrings.js"
  45. _log = logging.getLogger("check-inspector-strings")
  46. class StringsExtractor(ProcessorBase):
  47. def __init__(self, patterns):
  48. self._patterns = patterns
  49. self.strings = []
  50. for p in self._patterns:
  51. self.strings.append([])
  52. def should_process(self, file_path):
  53. return file_path.endswith(".js") and (not file_path.endswith("InjectedScript.js"))
  54. def process(self, lines, file_path, line_numbers=None):
  55. for line in lines:
  56. comment_start = line.find("//")
  57. if comment_start != -1:
  58. line = line[:comment_start]
  59. index = 0
  60. for pattern in self._patterns:
  61. line_strings = re.findall(pattern, line)
  62. for string in line_strings:
  63. self.strings[index].append(string)
  64. index += 1
  65. class LocalizedStringsExtractor:
  66. def __init__(self):
  67. self.localized_strings = []
  68. def process_file(self, file_path):
  69. localized_strings_file = codecs.open(file_path, encoding="utf-8", mode="r")
  70. try:
  71. contents = localized_strings_file.read()
  72. lines = contents.split("\n")
  73. for line in lines:
  74. match = re.match(r"localizedStrings\[\"((?:[^\"\\]|\\.)*?)\"", line)
  75. if match:
  76. self.localized_strings.append(match.group(1))
  77. finally:
  78. localized_strings_file.close()
  79. def extract_ui_strings(str, out):
  80. line_unrecognized = False
  81. idx = 0
  82. while idx < len(str):
  83. idx = str.find("WebInspector.UIString(", idx)
  84. if idx == -1:
  85. break
  86. idx = idx + len("WebInspector.UIString(")
  87. balance = 1
  88. item_recognized = False
  89. while idx < len(str):
  90. if str[idx] == ')':
  91. balance = balance - 1
  92. if balance == 0:
  93. break
  94. elif str[idx] == '(':
  95. balance = balance + 1
  96. elif balance == 1:
  97. if str[idx] == ',':
  98. break
  99. elif str[idx] == '"':
  100. str_idx = idx + 1
  101. while str_idx < len(str):
  102. if str[str_idx] == '\\':
  103. str_idx = str_idx + 1
  104. elif str[str_idx] == '"':
  105. out.add(str[idx + 1 : str_idx])
  106. idx = str_idx
  107. item_recognized = True
  108. break
  109. str_idx = str_idx + 1
  110. idx = idx + 1
  111. if not item_recognized:
  112. line_unrecognized = True
  113. if line_unrecognized:
  114. _log.info("Unrecognized: %s" % str)
  115. if __name__ == "__main__":
  116. configure_logging()
  117. cwd = os.path.abspath(os.curdir)
  118. filesystem = FileSystem()
  119. scm = SCMDetector(filesystem, Executive()).detect_scm_system(cwd)
  120. if scm is None:
  121. _log.error("WebKit checkout not found: You must run this script "
  122. "from within a WebKit checkout.")
  123. sys.exit(1)
  124. checkout_root = scm.checkout_root
  125. _log.debug("WebKit checkout found with root: %s" % checkout_root)
  126. change_directory(filesystem, checkout_root=checkout_root, paths=None)
  127. strings_extractor = StringsExtractor([r"(WebInspector\.UIString\(.*)", r"\"((?:[^\"\\]|\\.)*?)\""])
  128. file_reader = TextFileReader(filesystem, strings_extractor)
  129. file_reader.process_paths([_inspector_directory])
  130. localized_strings_extractor = LocalizedStringsExtractor()
  131. localized_strings_extractor.process_file(_localized_strings)
  132. raw_ui_strings = frozenset(strings_extractor.strings[0])
  133. ui_strings = set()
  134. for s in raw_ui_strings:
  135. extract_ui_strings(s, ui_strings)
  136. strings = frozenset(strings_extractor.strings[1])
  137. localized_strings = frozenset(localized_strings_extractor.localized_strings)
  138. new_strings = ui_strings - localized_strings
  139. for s in new_strings:
  140. _log.info("New: \"%s\"" % (s))
  141. old_strings = localized_strings - ui_strings
  142. suspicious_strings = strings & old_strings
  143. for s in suspicious_strings:
  144. _log.info("Suspicious: \"%s\"" % (s))
  145. unused_strings = old_strings - strings
  146. for s in unused_strings:
  147. _log.info("Unused: \"%s\"" % (s))
  148. localized_strings_duplicates = {}
  149. for s in localized_strings_extractor.localized_strings:
  150. if s in localized_strings_duplicates:
  151. _log.info("Duplicate: \"%s\"" % (s))
  152. else:
  153. localized_strings_duplicates.setdefault(s)