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.scm.detection import SCMDetector
35from webkitpy.common.memoized import memoized
36from webkitpy.common.net import buildbot, web
37from webkitpy.common.net.buildbot.chromiumbuildbot import ChromiumBuildBot
38from webkitpy.common.system.systemhost import SystemHost
39from webkitpy.layout_tests.port.factory import PortFactory
40
41
42_log = logging.getLogger(__name__)
43
44
45class Host(SystemHost):
46    def __init__(self):
47        SystemHost.__init__(self)
48        self.web = web.Web()
49
50        self._scm = None
51
52        # Everything below this line is WebKit-specific and belongs on a higher-level object.
53        self.buildbot = buildbot.BuildBot()
54
55        # FIXME: Unfortunately Port objects are currently the central-dispatch objects of the NRWT world.
56        # In order to instantiate a port correctly, we have to pass it at least an executive, user, scm, and filesystem
57        # so for now we just pass along the whole Host object.
58        # FIXME: PortFactory doesn't belong on this Host object if Port is going to have a Host (circular dependency).
59        self.port_factory = PortFactory(self)
60
61        self._engage_awesome_locale_hacks()
62
63    # We call this from the Host constructor, as it's one of the
64    # earliest calls made for all webkitpy-based programs.
65    def _engage_awesome_locale_hacks(self):
66        # To make life easier on our non-english users, we override
67        # the locale environment variables inside webkitpy.
68        # If we don't do this, programs like SVN will output localized
69        # messages and svn.py will fail to parse them.
70        # FIXME: We should do these overrides *only* for the subprocesses we know need them!
71        # This hack only works in unix environments.
72        os.environ['LANGUAGE'] = 'en'
73        os.environ['LANG'] = 'en_US.UTF-8'
74        os.environ['LC_MESSAGES'] = 'en_US.UTF-8'
75        os.environ['LC_ALL'] = ''
76
77    # FIXME: This is a horrible, horrible hack for WinPort and should be removed.
78    # Maybe this belongs in SVN in some more generic "find the svn binary" codepath?
79    # Or possibly Executive should have a way to emulate shell path-lookups?
80    # FIXME: Unclear how to test this, since it currently mutates global state on SVN.
81    def _engage_awesome_windows_hacks(self):
82        try:
83            self.executive.run_command(['svn', 'help'])
84        except OSError, e:
85            try:
86                self.executive.run_command(['svn.bat', 'help'])
87                # The Win port uses the depot_tools package, which contains a number
88                # of development tools, including Python and svn. Instead of using a
89                # real svn executable, depot_tools indirects via a batch file, called
90                # svn.bat. This batch file allows depot_tools to auto-update the real
91                # svn executable, which is contained in a subdirectory.
92                #
93                # That's all fine and good, except that subprocess.popen can detect
94                # the difference between a real svn executable and batch file when we
95                # don't provide use shell=True. Rather than use shell=True on Windows,
96                # We hack the svn.bat name into the SVN class.
97                _log.debug('Engaging svn.bat Windows hack.')
98                from webkitpy.common.checkout.scm.svn import SVN
99                SVN.executable_name = 'svn.bat'
100            except OSError, e:
101                _log.debug('Failed to engage svn.bat Windows hack.')
102        try:
103            self.executive.run_command(['git', 'help'])
104        except OSError, e:
105            try:
106                self.executive.run_command(['git.bat', 'help'])
107                # The Win port uses the depot_tools package, which contains a number
108                # of development tools, including Python and git. Instead of using a
109                # real git executable, depot_tools indirects via a batch file, called
110                # git.bat. This batch file allows depot_tools to auto-update the real
111                # git executable, which is contained in a subdirectory.
112                #
113                # That's all fine and good, except that subprocess.popen can detect
114                # the difference between a real git executable and batch file when we
115                # don't provide use shell=True. Rather than use shell=True on Windows,
116                # We hack the git.bat name into the SVN class.
117                _log.debug('Engaging git.bat Windows hack.')
118                from webkitpy.common.checkout.scm.git import Git
119                Git.executable_name = 'git.bat'
120            except OSError, e:
121                _log.debug('Failed to engage git.bat Windows hack.')
122
123    def initialize_scm(self, patch_directories=None):
124        if sys.platform == "win32":
125            self._engage_awesome_windows_hacks()
126        detector = SCMDetector(self.filesystem, self.executive)
127        self._scm = detector.default_scm(patch_directories)
128
129    def scm(self):
130        return self._scm
131
132    def scm_for_path(self, path):
133        # FIXME: make scm() be a wrapper around this, and clean up the way
134        # callers call initialize_scm() (to remove patch_directories) and scm().
135        if sys.platform == "win32":
136            self._engage_awesome_windows_hacks()
137        return SCMDetector(self.filesystem, self.executive).detect_scm_system(path)
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