1# Copyright (c) 2010 Google Inc. All rights reserved.
2# Copyright (c) 2009 Apple Inc. All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met:
7#
8#     * Redistributions of source code must retain the above copyright
9# notice, this list of conditions and the following disclaimer.
10#     * Redistributions in binary form must reproduce the above
11# copyright notice, this list of conditions and the following disclaimer
12# in the documentation and/or other materials provided with the
13# distribution.
14#     * Neither the name of Google Inc. nor the names of its
15# contributors may be used to endorse or promote products derived from
16# this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30import logging
31import os
32import sys
33
34from webkitpy.common.checkout import Checkout
35from webkitpy.common.checkout.scm.detection import SCMDetector
36from webkitpy.common.memoized import memoized
37from webkitpy.common.net import buildbot, web
38from webkitpy.common.net.buildbot.chromiumbuildbot import ChromiumBuildBot
39from webkitpy.common.system.systemhost import SystemHost
40from webkitpy.layout_tests.port.factory import PortFactory
41
42
43_log = logging.getLogger(__name__)
44
45
46class Host(SystemHost):
47    def __init__(self):
48        SystemHost.__init__(self)
49        self.web = web.Web()
50
51        # FIXME: Checkout should own the scm object.
52        self._scm = None
53        self._checkout = None
54
55        # Everything below this line is WebKit-specific and belongs on a higher-level object.
56        self.buildbot = buildbot.BuildBot()
57
58        # FIXME: Unfortunately Port objects are currently the central-dispatch objects of the NRWT world.
59        # In order to instantiate a port correctly, we have to pass it at least an executive, user, scm, and filesystem
60        # so for now we just pass along the whole Host object.
61        # FIXME: PortFactory doesn't belong on this Host object if Port is going to have a Host (circular dependency).
62        self.port_factory = PortFactory(self)
63
64        self._engage_awesome_locale_hacks()
65
66    # We call this from the Host constructor, as it's one of the
67    # earliest calls made for all webkitpy-based programs.
68    def _engage_awesome_locale_hacks(self):
69        # To make life easier on our non-english users, we override
70        # the locale environment variables inside webkitpy.
71        # If we don't do this, programs like SVN will output localized
72        # messages and svn.py will fail to parse them.
73        # FIXME: We should do these overrides *only* for the subprocesses we know need them!
74        # This hack only works in unix environments.
75        os.environ['LANGUAGE'] = 'en'
76        os.environ['LANG'] = 'en_US.UTF-8'
77        os.environ['LC_MESSAGES'] = 'en_US.UTF-8'
78        os.environ['LC_ALL'] = ''
79
80    # FIXME: This is a horrible, horrible hack for WinPort and should be removed.
81    # Maybe this belongs in SVN in some more generic "find the svn binary" codepath?
82    # Or possibly Executive should have a way to emulate shell path-lookups?
83    # FIXME: Unclear how to test this, since it currently mutates global state on SVN.
84    def _engage_awesome_windows_hacks(self):
85        try:
86            self.executive.run_command(['svn', 'help'])
87        except OSError, e:
88            try:
89                self.executive.run_command(['svn.bat', 'help'])
90                # The Win port uses the depot_tools package, which contains a number
91                # of development tools, including Python and svn. Instead of using a
92                # real svn executable, depot_tools indirects via a batch file, called
93                # svn.bat. This batch file allows depot_tools to auto-update the real
94                # svn executable, which is contained in a subdirectory.
95                #
96                # That's all fine and good, except that subprocess.popen can detect
97                # the difference between a real svn executable and batch file when we
98                # don't provide use shell=True. Rather than use shell=True on Windows,
99                # We hack the svn.bat name into the SVN class.
100                _log.debug('Engaging svn.bat Windows hack.')
101                from webkitpy.common.checkout.scm.svn import SVN
102                SVN.executable_name = 'svn.bat'
103            except OSError, e:
104                _log.debug('Failed to engage svn.bat Windows hack.')
105        try:
106            self.executive.run_command(['git', 'help'])
107        except OSError, e:
108            try:
109                self.executive.run_command(['git.bat', 'help'])
110                # The Win port uses the depot_tools package, which contains a number
111                # of development tools, including Python and git. Instead of using a
112                # real git executable, depot_tools indirects via a batch file, called
113                # git.bat. This batch file allows depot_tools to auto-update the real
114                # git executable, which is contained in a subdirectory.
115                #
116                # That's all fine and good, except that subprocess.popen can detect
117                # the difference between a real git executable and batch file when we
118                # don't provide use shell=True. Rather than use shell=True on Windows,
119                # We hack the git.bat name into the SVN class.
120                _log.debug('Engaging git.bat Windows hack.')
121                from webkitpy.common.checkout.scm.git import Git
122                Git.executable_name = 'git.bat'
123            except OSError, e:
124                _log.debug('Failed to engage git.bat Windows hack.')
125
126    def initialize_scm(self, patch_directories=None):
127        if sys.platform == "win32":
128            self._engage_awesome_windows_hacks()
129        detector = SCMDetector(self.filesystem, self.executive)
130        self._scm = detector.default_scm(patch_directories)
131        self._checkout = Checkout(self.scm())
132
133    def scm(self):
134        return self._scm
135
136    def checkout(self):
137        return self._checkout
138
139    def buildbot_for_builder_name(self, name):
140        if self.port_factory.get_from_builder_name(name).is_chromium():
141            return self.chromium_buildbot()
142        return self.buildbot
143
144    @memoized
145    def chromium_buildbot(self):
146        return ChromiumBuildBot()
147