2011-05-29 Yuta Kitamura <yutak@chromium.org>
[WebKit-https.git] / Tools / Scripts / webkitpy / thirdparty / __init__.py
1 # Copyright (C) 2010 Chris Jerdonek (cjerdonek@webkit.org)
2 #
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions
5 # are met:
6 # 1.  Redistributions of source code must retain the above copyright
7 #     notice, this list of conditions and the following disclaimer.
8 # 2.  Redistributions in binary form must reproduce the above copyright
9 #     notice, this list of conditions and the following disclaimer in the
10 #     documentation and/or other materials provided with the distribution.
11 #
12 # THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
13 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15 # DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR
16 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
18 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
19 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
20 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
21 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22
23 # This module is required for Python to treat this directory as a package.
24
25 """Autoinstalls third-party code required by WebKit."""
26
27 from __future__ import with_statement
28
29 import codecs
30 import os
31 import sys
32
33 from webkitpy.common.system.autoinstall import AutoInstaller
34 from webkitpy.common.system.filesystem import FileSystem
35
36 _THIRDPARTY_DIR = os.path.dirname(__file__)
37 _AUTOINSTALLED_DIR = os.path.join(_THIRDPARTY_DIR, "autoinstalled")
38
39 # Putting the autoinstall code into webkitpy/thirdparty/__init__.py
40 # ensures that no autoinstalling occurs until a caller imports from
41 # webkitpy.thirdparty.  This is useful if the caller wants to configure
42 # logging prior to executing autoinstall code.
43
44 # FIXME: If any of these servers is offline, webkit-patch breaks (and maybe
45 # other scripts do, too). See <http://webkit.org/b/42080>.
46
47 # We put auto-installed third-party modules in this directory--
48 #
49 #     webkitpy/thirdparty/autoinstalled
50 fs = FileSystem()
51 fs.maybe_make_directory(_AUTOINSTALLED_DIR)
52
53 init_path = fs.join(_AUTOINSTALLED_DIR, "__init__.py")
54 if not fs.exists(init_path):
55     fs.write_text_file(init_path, "")
56
57 readme_path = fs.join(_AUTOINSTALLED_DIR, "README")
58 if not fs.exists(readme_path):
59     fs.write_text_file(readme_path,
60         "This directory is auto-generated by WebKit and is "
61         "safe to delete.\nIt contains needed third-party Python "
62         "packages automatically downloaded from the web.")
63
64
65 class AutoinstallImportHook(object):
66     def __init__(self, filesystem=None):
67         self._fs = filesystem or FileSystem()
68
69     def find_module(self, fullname, path):
70         # This method will run before each import. See http://www.python.org/dev/peps/pep-0302/
71         if '.autoinstalled' not in fullname:
72             return
73
74         if '.mechanize' in fullname:
75             self._install_mechanize()
76         elif '.pep8' in fullname:
77             self._install_pep8()
78         elif '.eliza' in fullname:
79             self._install_eliza()
80         elif '.irc' in fullname:
81             self._install_irc()
82         elif '.pywebsocket' in fullname:
83             self._install_pywebsocket()
84
85     def _install_mechanize(self):
86         # The mechanize package uses ClientForm, for example, in _html.py.
87         # Since mechanize imports ClientForm in the following way,
88         #
89         # > import sgmllib, ClientForm
90         #
91         # the search path needs to include ClientForm.  We put ClientForm in
92         # its own directory so that we can include it in the search path
93         # without including other modules as a side effect.
94         clientform_dir = self._fs.join(_AUTOINSTALLED_DIR, "clientform")
95         installer = AutoInstaller(append_to_search_path=True,
96                                   target_dir=clientform_dir)
97         installer.install(url="http://pypi.python.org/packages/source/C/ClientForm/ClientForm-0.2.10.zip",
98                           url_subpath="ClientForm.py")
99
100         self._install("http://pypi.python.org/packages/source/m/mechanize/mechanize-0.2.4.zip",
101                       "mechanize")
102
103     def _install_pep8(self):
104         self._install("http://pypi.python.org/packages/source/p/pep8/pep8-0.5.0.tar.gz#md5=512a818af9979290cd619cce8e9c2e2b",
105                       "pep8-0.5.0/pep8.py")
106
107     def _install_eliza(self):
108         installer = AutoInstaller(target_dir=_AUTOINSTALLED_DIR)
109         installer.install(url="http://www.adambarth.com/webkit/eliza",
110                           target_name="eliza.py")
111
112     def _install_irc(self):
113         # Since irclib and ircbot are two top-level packages, we need to import
114         # them separately.  We group them into an irc package for better
115         # organization purposes.
116         irc_dir = self._fs.join(_AUTOINSTALLED_DIR, "irc")
117         installer = AutoInstaller(target_dir=irc_dir)
118         installer.install(url="http://downloads.sourceforge.net/project/python-irclib/python-irclib/0.4.8/python-irclib-0.4.8.zip",
119                           url_subpath="irclib.py")
120         installer.install(url="http://downloads.sourceforge.net/project/python-irclib/python-irclib/0.4.8/python-irclib-0.4.8.zip",
121                           url_subpath="ircbot.py")
122
123     def _install_pywebsocket(self):
124         pywebsocket_dir = self._fs.join(_AUTOINSTALLED_DIR, "pywebsocket")
125         installer = AutoInstaller(target_dir=pywebsocket_dir)
126         installer.install(url="http://pywebsocket.googlecode.com/files/mod_pywebsocket-0.6b1.tar.gz",
127                           url_subpath="pywebsocket-0.6b1/src/mod_pywebsocket")
128
129     def _install(self, url, url_subpath):
130         installer = AutoInstaller(target_dir=_AUTOINSTALLED_DIR)
131         installer.install(url=url, url_subpath=url_subpath)
132
133
134 sys.meta_path.append(AutoinstallImportHook())