1#!/usr/bin/env python
2# Copyright (C) 2010 Google 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
30"""Chromium Mac implementation of the Port interface."""
31
32import os
33import platform
34import signal
35import subprocess
36
37import chromium
38
39
40class ChromiumMacPort(chromium.ChromiumPort):
41    """Chromium Mac implementation of the Port class."""
42
43    def __init__(self, port_name=None, options=None):
44        if port_name is None:
45            port_name = 'chromium-mac'
46        chromium.ChromiumPort.__init__(self, port_name, options)
47
48    def baseline_search_path(self):
49        return [self.baseline_path(),
50                self._webkit_baseline_path('mac' + self.version()),
51                self._webkit_baseline_path('mac')]
52
53    def check_sys_deps(self):
54        # We have no specific platform dependencies.
55        return True
56
57    def num_cores(self):
58        return int(subprocess.Popen(['sysctl','-n','hw.ncpu'],
59                                    stdout=subprocess.PIPE).stdout.read())
60
61    def test_platform_name(self):
62        # We use 'mac' instead of 'chromium-mac'
63        return 'mac'
64
65    def version(self):
66        os_version_string = platform.mac_ver()[0]  # e.g. "10.5.6"
67        if not os_version_string:
68            return '-leopard'
69        release_version = int(os_version_string.split('.')[1])
70        # we don't support 'tiger' or earlier releases
71        if release_version == 5:
72            return '-leopard'
73        elif release_version == 6:
74            return '-snowleopard'
75        return ''
76
77    #
78    # PROTECTED METHODS
79    #
80
81    def _build_path(self, *comps):
82        return self.path_from_chromium_base('xcodebuild', self._options.target,
83                                            *comps)
84
85    def _lighttpd_path(self, *comps):
86        return self.path_from_chromium_base('third_party', 'lighttpd',
87                                            'mac', *comps)
88
89    def _kill_process(self, pid):
90        """Forcefully kill the process.
91
92        Args:
93            pid: The id of the process to be killed.
94        """
95        os.kill(pid, signal.SIGKILL)
96
97    def _kill_all_process(self, process_name):
98        """Kill any processes running under this name."""
99        # On Mac OS X 10.6, killall has a new constraint: -SIGNALNAME or
100        # -SIGNALNUMBER must come first.  Example problem:
101        #   $ killall -u $USER -TERM lighttpd
102        #   killall: illegal option -- T
103        # Use of the earlier -TERM placement is just fine on 10.5.
104        null = open(os.devnull)
105        subprocess.call(['killall', '-TERM', '-u', os.getenv('USER'),
106                        process_name], stderr=null)
107        null.close()
108
109    def _path_to_apache(self):
110        return '/usr/sbin/httpd'
111
112    def _path_to_apache_config_file(self):
113        return os.path.join(self.layout_tests_dir(), 'http', 'conf',
114                            'apache2-httpd.conf')
115
116    def _path_to_lighttpd(self):
117        return self._lighttp_path('bin', 'lighttp')
118
119    def _path_to_lighttpd_modules(self):
120        return self._lighttp_path('lib')
121
122    def _path_to_lighttpd_php(self):
123        return self._lighttpd_path('bin', 'php-cgi')
124
125    def _path_to_driver(self):
126        # TODO(pinkerton): make |target| happy with case-sensitive file
127        # systems.
128        return self._build_path('TestShell.app', 'Contents', 'MacOS',
129                                'TestShell')
130
131    def _path_to_helper(self):
132        return self._build_path('layout_test_helper')
133
134    def _path_to_image_diff(self):
135        return self._build_path('image_diff')
136
137    def _path_to_wdiff(self):
138        return 'wdiff'
139
140    def _shut_down_http_server(self, server_pid):
141        """Shut down the lighttpd web server. Blocks until it's fully
142        shut down.
143
144        Args:
145            server_pid: The process ID of the running server.
146        """
147        # server_pid is not set when "http_server.py stop" is run manually.
148        if server_pid is None:
149            # TODO(mmoss) This isn't ideal, since it could conflict with
150            # lighttpd processes not started by http_server.py,
151            # but good enough for now.
152            self._kill_all_process('lighttpd')
153            self._kill_all_process('httpd')
154        else:
155            try:
156                os.kill(server_pid, signal.SIGTERM)
157                # TODO(mmoss) Maybe throw in a SIGKILL just to be sure?
158            except OSError:
159                # Sometimes we get a bad PID (e.g. from a stale httpd.pid
160                # file), so if kill fails on the given PID, just try to
161                # 'killall' web servers.
162                self._shut_down_http_server(None)
163