Home | History | Annotate | Download | only in common
      1 # Copyright (c) 2012 Google Inc. All rights reserved.
      2 #
      3 # Redistribution and use in source and binary forms, with or without
      4 # modification, are permitted provided that the following conditions are
      5 # met:
      6 #
      7 #     * Redistributions of source code must retain the above copyright
      8 # notice, this list of conditions and the following disclaimer.
      9 #     * Redistributions in binary form must reproduce the above
     10 # copyright notice, this list of conditions and the following disclaimer
     11 # in the documentation and/or other materials provided with the
     12 # distribution.
     13 #     * Neither the name of Google Inc. nor the names of its
     14 # contributors may be used to endorse or promote products derived from
     15 # this software without specific prior written permission.
     16 #
     17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28 
     29 import os
     30 import sys
     31 
     32 
     33 class WebKitFinder(object):
     34     def __init__(self, filesystem):
     35         self._filesystem = filesystem
     36         self._dirsep = filesystem.sep
     37         self._sys_path = sys.path
     38         self._env_path = os.environ['PATH'].split(os.pathsep)
     39         self._webkit_base = None
     40         self._depot_tools = None
     41 
     42     def webkit_base(self):
     43         """Returns the absolute path to the top of the WebKit tree.
     44 
     45         Raises an AssertionError if the top dir can't be determined."""
     46         # Note: This code somewhat duplicates the code in
     47         # scm.find_checkout_root(). However, that code only works if the top
     48         # of the SCM repository also matches the top of the WebKit tree. Some SVN users
     49         # (the chromium test bots, for example), might only check out subdirectories like
     50         # Tools/Scripts. This code will also work if there is no SCM system at all.
     51         if not self._webkit_base:
     52             self._webkit_base = self._webkit_base
     53             module_path = self._filesystem.abspath(self._filesystem.path_to_module(self.__module__))
     54             tools_index = module_path.rfind('Tools')
     55             assert tools_index != -1, "could not find location of this checkout from %s" % module_path
     56             self._webkit_base = self._filesystem.normpath(module_path[0:tools_index - 1])
     57         return self._webkit_base
     58 
     59     def path_from_webkit_base(self, *comps):
     60         return self._filesystem.join(self.webkit_base(), *comps)
     61 
     62     def path_to_script(self, script_name):
     63         """Returns the relative path to the script from the top of the WebKit tree."""
     64         # This is intentionally relative in order to force callers to consider what
     65         # their current working directory is (and change to the top of the tree if necessary).
     66         return self._filesystem.join("Tools", "Scripts", script_name)
     67 
     68     def layout_tests_dir(self):
     69         return self.path_from_webkit_base('LayoutTests')
     70 
     71     def perf_tests_dir(self):
     72         return self.path_from_webkit_base('PerformanceTests')
     73 
     74     def depot_tools_base(self):
     75         if not self._depot_tools:
     76             # This basically duplicates src/tools/find_depot_tools.py without the side effects
     77             # (adding the directory to sys.path and importing breakpad).
     78             self._depot_tools = (self._check_paths_for_depot_tools(self._sys_path) or
     79                                  self._check_paths_for_depot_tools(self._env_path) or
     80                                  self._check_upward_for_depot_tools())
     81         return self._depot_tools
     82 
     83     def _check_paths_for_depot_tools(self, paths):
     84         for path in paths:
     85             if path.rstrip(self._dirsep).endswith('depot_tools'):
     86                 return path
     87         return None
     88 
     89     def _check_upward_for_depot_tools(self):
     90         fs = self._filesystem
     91         prev_dir = ''
     92         current_dir = fs.dirname(self._webkit_base())
     93         while current_dir != prev_dir:
     94             if fs.exists(fs.join(current_dir, 'depot_tools', 'pylint.py')):
     95                 return fs.join(current_dir, 'depot_tools')
     96             prev_dir = current_dir
     97             current_dir = fs.dirname(current_dir)
     98 
     99     def path_from_depot_tools_base(self, *comps):
    100         return self._filesystem.join(self.depot_tools_base(), *comps)
    101