Home | History | Annotate | Download | only in Scripts
      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 
     31 import codecs
     32 import logging
     33 import re
     34 import sys
     35 
     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
     41 
     42 _inspector_directory = "Source/WebCore/inspector/front-end"
     43 _devtools_directory = "Source/WebKit/chromium/src/js"
     44 _localized_strings = "Source/WebCore/English.lproj/localizedStrings.js"
     45 
     46 _log = logging.getLogger("check-inspector-strings")
     47 
     48 def decode_unicode_escapes(s):
     49     xNN_converted_to_u00NN = s.replace("\\x", "\\u00")
     50     return eval("ur\"" + xNN_converted_to_u00NN + "\"")
     51 
     52 class StringsExtractor(ProcessorBase):
     53     def __init__(self, patterns):
     54         self._patterns = patterns
     55         self.strings = []
     56         for p in self._patterns:
     57             self.strings.append([])
     58 
     59     def should_process(self, file_path):
     60         return file_path.endswith(".js") and (not file_path.endswith("InjectedScript.js"))
     61 
     62     def process(self, lines, file_path, line_numbers=None):
     63         for line in lines:
     64             comment_start = line.find("//")
     65             if comment_start != -1:
     66                 line = line[:comment_start]
     67             index = 0
     68             for pattern in self._patterns:
     69                 line_strings = re.findall(pattern, line)
     70                 for string in line_strings:
     71                     self.strings[index].append(decode_unicode_escapes(string))
     72                 index += 1
     73 
     74 class LocalizedStringsExtractor:
     75     def __init__(self):
     76         self.localized_strings = []
     77 
     78     def process_file(self, file_path):
     79         localized_strings_file = codecs.open(file_path, encoding="utf-16", mode="r")
     80         try:
     81             contents = localized_strings_file.read()
     82             lines = contents.split("\n")
     83             for line in lines:
     84                 match = re.match(r"localizedStrings\[\"((?:[^\"\\]|\\.)*?)\"", line)
     85                 if match:
     86                     self.localized_strings.append(decode_unicode_escapes(match.group(1)))
     87         finally:
     88             localized_strings_file.close()
     89 
     90 if __name__ == "__main__":
     91     configure_logging()
     92 
     93     checkout = detect_checkout()
     94     if checkout is None:
     95         _log.error("WebKit checkout not found: You must run this script "
     96                    "from within a WebKit checkout.")
     97         sys.exit(1)
     98     checkout_root = checkout.root_path()
     99     _log.debug("WebKit checkout found with root: %s" % checkout_root)
    100     change_directory(checkout_root=checkout_root, paths=None)
    101 
    102     strings_extractor = StringsExtractor([r"WebInspector\.(?:UIString|formatLocalized)\(\"((?:[^\"\\]|\\.)*?)\"", r"\"((?:[^\"\\]|\\.)*?)\""])
    103     file_reader = TextFileReader(strings_extractor)
    104     file_reader.process_paths([_inspector_directory, _devtools_directory])
    105     localized_strings_extractor = LocalizedStringsExtractor()
    106     localized_strings_extractor.process_file(_localized_strings)
    107     ui_strings = frozenset(strings_extractor.strings[0])
    108     strings = frozenset(strings_extractor.strings[1])
    109     localized_strings = frozenset(localized_strings_extractor.localized_strings)
    110 
    111     new_strings = ui_strings - localized_strings
    112     for s in new_strings:
    113         _log.info("New: \"%s\"" % (s))
    114     old_strings = localized_strings - ui_strings
    115     suspicious_strings = strings & old_strings
    116     for s in suspicious_strings:
    117         _log.info("Suspicious: \"%s\"" % (s))
    118     unused_strings = old_strings - strings
    119     for s in unused_strings:
    120         _log.info("Unused: \"%s\"" % (s))
    121 
    122     localized_strings_duplicates = {}
    123     for s in localized_strings_extractor.localized_strings:
    124         if s in localized_strings_duplicates:
    125             _log.info("Duplicate: \"%s\"" % (s))
    126         else:
    127             localized_strings_duplicates.setdefault(s)
    128