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
29import os
30import sys
31
32
33class 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._chromium_base = None
41        self._depot_tools = None
42
43    def webkit_base(self):
44        """Returns the absolute path to the top of the WebKit tree.
45
46        Raises an AssertionError if the top dir can't be determined."""
47        # Note: This code somewhat duplicates the code in
48        # scm.find_checkout_root(). However, that code only works if the top
49        # of the SCM repository also matches the top of the WebKit tree. Some SVN users
50        # (the chromium test bots, for example), might only check out subdirectories like
51        # Tools/Scripts. This code will also work if there is no SCM system at all.
52        if not self._webkit_base:
53            self._webkit_base = self._webkit_base
54            module_path = self._filesystem.abspath(self._filesystem.path_to_module(self.__module__))
55            tools_index = module_path.rfind('Tools')
56            assert tools_index != -1, "could not find location of this checkout from %s" % module_path
57            self._webkit_base = self._filesystem.normpath(module_path[0:tools_index - 1])
58        return self._webkit_base
59
60    def chromium_base(self):
61        if not self._chromium_base:
62            self._chromium_base = self._filesystem.dirname(self._filesystem.dirname(self.webkit_base()))
63        return self._chromium_base
64
65    def path_from_webkit_base(self, *comps):
66        return self._filesystem.join(self.webkit_base(), *comps)
67
68    def path_from_chromium_base(self, *comps):
69        return self._filesystem.join(self.chromium_base(), *comps)
70
71    def path_to_script(self, script_name):
72        """Returns the relative path to the script from the top of the WebKit tree."""
73        # This is intentionally relative in order to force callers to consider what
74        # their current working directory is (and change to the top of the tree if necessary).
75        return self._filesystem.join("Tools", "Scripts", script_name)
76
77    def layout_tests_dir(self):
78        return self.path_from_webkit_base('LayoutTests')
79
80    def perf_tests_dir(self):
81        return self.path_from_webkit_base('PerformanceTests')
82
83    def depot_tools_base(self):
84        if not self._depot_tools:
85            # This basically duplicates src/tools/find_depot_tools.py without the side effects
86            # (adding the directory to sys.path and importing breakpad).
87            self._depot_tools = (self._check_paths_for_depot_tools(self._sys_path) or
88                                 self._check_paths_for_depot_tools(self._env_path) or
89                                 self._check_upward_for_depot_tools())
90        return self._depot_tools
91
92    def _check_paths_for_depot_tools(self, paths):
93        for path in paths:
94            if path.rstrip(self._dirsep).endswith('depot_tools'):
95                return path
96        return None
97
98    def _check_upward_for_depot_tools(self):
99        fs = self._filesystem
100        prev_dir = ''
101        current_dir = fs.dirname(self._webkit_base)
102        while current_dir != prev_dir:
103            if fs.exists(fs.join(current_dir, 'depot_tools', 'pylint.py')):
104                return fs.join(current_dir, 'depot_tools')
105            prev_dir = current_dir
106            current_dir = fs.dirname(current_dir)
107
108    def path_from_depot_tools_base(self, *comps):
109        return self._filesystem.join(self.depot_tools_base(), *comps)
110