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"""Factory method to retrieve the appropriate port implementation."""
31
32
33import sys
34
35
36def all_port_names():
37    """Return a list of all valid, fully-specified, "real" port names.
38
39    This is the list of directories that are used as actual baseline_paths()
40    by real ports. This does not include any "fake" names like "test"
41    or "mock-mac", and it does not include any directories that are not ."""
42    # FIXME: There's probably a better way to generate this list ...
43    return ['chromium-gpu-linux',
44            'chromium-gpu-mac-snowleopard', 'chromium-gpu-mac-leopard',
45            'chromium-gpu-win-xp', 'chromium-gpu-win-vista', 'chromium-gpu-win-win7',
46            'chromium-linux-x86_64', 'chromium-linux-x86',
47            'chromium-mac-leopard', 'chromium-mac-snowleopard',
48            'chromium-win-xp', 'chromium-win-vista', 'chromium-win-win7',
49            'google-chrome-linux32', 'google-chrome-linux64',
50            'gtk',
51            'mac-tiger', 'mac-leopard', 'mac-snowleopard', 'mac-wk2',
52            'qt-linux', 'qt-mac', 'qt-win', 'qt-wk2',
53            'win-xp', 'win', 'win-wk2',
54    ]
55
56
57def get(port_name=None, options=None, **kwargs):
58    """Returns an object implementing the Port interface. If
59    port_name is None, this routine attempts to guess at the most
60    appropriate port on this platform."""
61    # Wrapped for backwards-compatibility
62    if port_name:
63        kwargs['port_name'] = port_name
64    if options:
65        kwargs['options'] = options
66    return _get_kwargs(**kwargs)
67
68
69def _get_kwargs(**kwargs):
70    port_to_use = kwargs.get('port_name', None)
71    options = kwargs.get('options', None)
72    if port_to_use is None:
73        if sys.platform == 'win32' or sys.platform == 'cygwin':
74            if options and hasattr(options, 'chromium') and options.chromium:
75                port_to_use = 'chromium-win'
76            else:
77                port_to_use = 'win'
78        elif sys.platform == 'linux2':
79            port_to_use = 'chromium-linux'
80        elif sys.platform == 'darwin':
81            if options and hasattr(options, 'chromium') and options.chromium:
82                port_to_use = 'chromium-mac'
83            else:
84                port_to_use = 'mac'
85
86    if port_to_use is None:
87        raise NotImplementedError('unknown port; sys.platform = "%s"' %
88                                  sys.platform)
89
90    if port_to_use.startswith('test'):
91        import test
92        maker = test.TestPort
93    elif port_to_use.startswith('dryrun'):
94        import dryrun
95        maker = dryrun.DryRunPort
96    elif port_to_use.startswith('mock-'):
97        import mock_drt
98        maker = mock_drt.MockDRTPort
99    elif port_to_use.startswith('mac'):
100        import mac
101        maker = mac.MacPort
102    elif port_to_use.startswith('win'):
103        import win
104        maker = win.WinPort
105    elif port_to_use.startswith('gtk'):
106        import gtk
107        maker = gtk.GtkPort
108    elif port_to_use.startswith('qt'):
109        import qt
110        maker = qt.QtPort
111    elif port_to_use.startswith('chromium-gpu'):
112        import chromium_gpu
113        maker = chromium_gpu.get
114    elif port_to_use.startswith('chromium-mac'):
115        import chromium_mac
116        maker = chromium_mac.ChromiumMacPort
117    elif port_to_use.startswith('chromium-linux'):
118        import chromium_linux
119        maker = chromium_linux.ChromiumLinuxPort
120    elif port_to_use.startswith('chromium-win'):
121        import chromium_win
122        maker = chromium_win.ChromiumWinPort
123    elif port_to_use.startswith('google-chrome'):
124        import google_chrome
125        maker = google_chrome.GetGoogleChromePort
126    else:
127        raise NotImplementedError('unsupported port: %s' % port_to_use)
128    return maker(**kwargs)
129