1926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)# Copyright (c) 2012 Google Inc. All rights reserved.
2926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)#
3926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)# Redistribution and use in source and binary forms, with or without
4926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)# modification, are permitted provided that the following conditions are
5926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)# met:
6926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)#
7926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)#     * Redistributions of source code must retain the above copyright
8926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)# notice, this list of conditions and the following disclaimer.
9926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)#     * Redistributions in binary form must reproduce the above
10926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)# copyright notice, this list of conditions and the following disclaimer
11926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)# in the documentation and/or other materials provided with the
12926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)# distribution.
13926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)#     * Neither the name of Google Inc. nor the names of its
14926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)# contributors may be used to endorse or promote products derived from
15926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)# this software without specific prior written permission.
16926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)#
17926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
297757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdochimport os
307757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdochimport sys
317757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch
32926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
33926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)class WebKitFinder(object):
34926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    def __init__(self, filesystem):
35926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        self._filesystem = filesystem
367757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch        self._dirsep = filesystem.sep
377757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch        self._sys_path = sys.path
387757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch        self._env_path = os.environ['PATH'].split(os.pathsep)
39926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        self._webkit_base = None
401e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        self._chromium_base = None
417757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch        self._depot_tools = None
42926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
43926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    def webkit_base(self):
44926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        """Returns the absolute path to the top of the WebKit tree.
45926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
46926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        Raises an AssertionError if the top dir can't be determined."""
47926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        # Note: This code somewhat duplicates the code in
48926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        # scm.find_checkout_root(). However, that code only works if the top
49926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        # of the SCM repository also matches the top of the WebKit tree. Some SVN users
50926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        # (the chromium test bots, for example), might only check out subdirectories like
51926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        # Tools/Scripts. This code will also work if there is no SCM system at all.
52926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        if not self._webkit_base:
53926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            self._webkit_base = self._webkit_base
545267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)            module_path = self._filesystem.abspath(self._filesystem.path_to_module(self.__module__))
55926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            tools_index = module_path.rfind('Tools')
56926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            assert tools_index != -1, "could not find location of this checkout from %s" % module_path
57926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            self._webkit_base = self._filesystem.normpath(module_path[0:tools_index - 1])
58926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        return self._webkit_base
59926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
601e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    def chromium_base(self):
611e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        if not self._chromium_base:
621e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)            self._chromium_base = self._filesystem.dirname(self._filesystem.dirname(self.webkit_base()))
631e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        return self._chromium_base
641e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
65926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    def path_from_webkit_base(self, *comps):
66926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        return self._filesystem.join(self.webkit_base(), *comps)
67926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
681e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    def path_from_chromium_base(self, *comps):
691e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        return self._filesystem.join(self.chromium_base(), *comps)
701e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
71926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    def path_to_script(self, script_name):
72926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        """Returns the relative path to the script from the top of the WebKit tree."""
73926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        # This is intentionally relative in order to force callers to consider what
74926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        # their current working directory is (and change to the top of the tree if necessary).
75926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        return self._filesystem.join("Tools", "Scripts", script_name)
76926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
77926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    def layout_tests_dir(self):
78926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        return self.path_from_webkit_base('LayoutTests')
79926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
80926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    def perf_tests_dir(self):
81926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        return self.path_from_webkit_base('PerformanceTests')
827757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch
837757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch    def depot_tools_base(self):
847757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch        if not self._depot_tools:
857757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch            # This basically duplicates src/tools/find_depot_tools.py without the side effects
867757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch            # (adding the directory to sys.path and importing breakpad).
877757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch            self._depot_tools = (self._check_paths_for_depot_tools(self._sys_path) or
887757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch                                 self._check_paths_for_depot_tools(self._env_path) or
897757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch                                 self._check_upward_for_depot_tools())
907757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch        return self._depot_tools
917757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch
927757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch    def _check_paths_for_depot_tools(self, paths):
937757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch        for path in paths:
947757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch            if path.rstrip(self._dirsep).endswith('depot_tools'):
957757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch                return path
967757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch        return None
977757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch
987757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch    def _check_upward_for_depot_tools(self):
997757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch        fs = self._filesystem
1007757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch        prev_dir = ''
1019bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        current_dir = fs.dirname(self._webkit_base)
1027757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch        while current_dir != prev_dir:
1037757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch            if fs.exists(fs.join(current_dir, 'depot_tools', 'pylint.py')):
1047757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch                return fs.join(current_dir, 'depot_tools')
1057757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch            prev_dir = current_dir
1067757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch            current_dir = fs.dirname(current_dir)
1077757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch
1087757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch    def path_from_depot_tools_base(self, *comps):
1097757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch        return self._filesystem.join(self.depot_tools_base(), *comps)
110