f43c3acad89d1c181afb45c3916f2cf91b6fb859
[WebKit.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
28 import codecs
29 import os
30 import sys
31
32 from webkitpy.common.system.autoinstall import AutoInstaller
33 from webkitpy.common.system.filesystem import FileSystem
34
35 _THIRDPARTY_DIR = os.path.dirname(__file__)
36 _AUTOINSTALLED_DIR = os.path.join(_THIRDPARTY_DIR, "autoinstalled")
37
38 # Putting the autoinstall code into webkitpy/thirdparty/__init__.py
39 # ensures that no autoinstalling occurs until a caller imports from
40 # webkitpy.thirdparty.  This is useful if the caller wants to configure
41 # logging prior to executing autoinstall code.
42
43 # FIXME: If any of these servers is offline, webkit-patch breaks (and maybe
44 # other scripts do, too). See <http://webkit.org/b/42080>.
45
46 # We put auto-installed third-party modules in this directory--
47 #
48 #     webkitpy/thirdparty/autoinstalled
49 fs = FileSystem()
50 fs.maybe_make_directory(_AUTOINSTALLED_DIR)
51
52 init_path = fs.join(_AUTOINSTALLED_DIR, "__init__.py")
53 if not fs.exists(init_path):
54     fs.write_text_file(init_path, "")
55
56 readme_path = fs.join(_AUTOINSTALLED_DIR, "README")
57 if not fs.exists(readme_path):
58     fs.write_text_file(readme_path,
59         "This directory is auto-generated by WebKit and is "
60         "safe to delete.\nIt contains needed third-party Python "
61         "packages automatically downloaded from the web.")
62
63
64 class AutoinstallImportHook(object):
65     def __init__(self, filesystem=None):
66         self._fs = filesystem or FileSystem()
67
68     def _ensure_autoinstalled_dir_is_in_sys_path(self):
69         # Some packages require that the are being put somewhere under a directory in sys.path.
70         if not _AUTOINSTALLED_DIR in sys.path:
71             sys.path.insert(0, _AUTOINSTALLED_DIR)
72
73     def find_module(self, fullname, _):
74         # This method will run before each import. See http://www.python.org/dev/peps/pep-0302/
75         if '.autoinstalled' not in fullname:
76             return
77
78         # Note: all of the methods must follow the "_install_XXX" convention in
79         # order for autoinstall_everything(), below, to work properly.
80         if '.mechanize' in fullname:
81             self._install_mechanize()
82         elif '.pep8' in fullname:
83             self._install_pep8()
84         elif '.pylint' in fullname:
85             self._install_pylint()
86         elif '.coverage' in fullname:
87             self._install_coverage()
88         elif '.eliza' in fullname:
89             self._install_eliza()
90         elif '.buildbot' in fullname:
91             self._install_buildbot()
92         elif '.keyring' in fullname:
93             self._install_keyring()
94         elif '.twisted' in fullname:
95             self._install_twisted()
96
97     def _install_mechanize(self):
98         self._install("http://pypi.python.org/packages/source/m/mechanize/mechanize-0.2.5.tar.gz",
99                              "mechanize-0.2.5/mechanize")
100
101     def _install_keyring(self):
102         self._install("http://pypi.python.org/packages/source/k/keyring/keyring-4.0.zip",
103                              "keyring")
104
105     def _install_pep8(self):
106         self._install("http://pypi.python.org/packages/source/p/pep8/pep8-0.5.0.tar.gz#md5=512a818af9979290cd619cce8e9c2e2b",
107                              "pep8-0.5.0/pep8.py")
108
109     def _install_pylint(self):
110         self._ensure_autoinstalled_dir_is_in_sys_path()
111         if (not self._fs.exists(self._fs.join(_AUTOINSTALLED_DIR, "pylint")) or
112             not self._fs.exists(self._fs.join(_AUTOINSTALLED_DIR, "logilab/astng")) or
113             not self._fs.exists(self._fs.join(_AUTOINSTALLED_DIR, "logilab/common"))):
114             installer = AutoInstaller(target_dir=_AUTOINSTALLED_DIR)
115             files_to_remove = []
116             if sys.platform == 'win32':
117                 files_to_remove = ['test/data/write_protected_file.txt']
118             installer.install("http://pypi.python.org/packages/source/l/logilab-common/logilab-common-0.58.1.tar.gz#md5=77298ab2d8bb8b4af9219791e7cee8ce", url_subpath="logilab-common-0.58.1", target_name="logilab/common", files_to_remove=files_to_remove)
119             installer.install("http://pypi.python.org/packages/source/l/logilab-astng/logilab-astng-0.24.1.tar.gz#md5=ddaf66e4d85714d9c47a46d4bed406de", url_subpath="logilab-astng-0.24.1", target_name="logilab/astng")
120             installer.install('http://pypi.python.org/packages/source/p/pylint/pylint-0.25.1.tar.gz#md5=728bbc2b339bc3749af013709a7f87a5', url_subpath="pylint-0.25.1", target_name="pylint")
121
122     # autoinstalled.buildbot is used by BuildSlaveSupport/build.webkit.org-config/mastercfg_unittest.py
123     # and should ideally match the version of BuildBot used at build.webkit.org.
124     def _install_buildbot(self):
125         # The buildbot package uses jinja2, for example, in buildbot/status/web/base.py.
126         # buildbot imports jinja2 directly (as though it were installed on the system),
127         # so the search path needs to include jinja2.  We put jinja2 in
128         # its own directory so that we can include it in the search path
129         # without including other modules as a side effect.
130         jinja_dir = self._fs.join(_AUTOINSTALLED_DIR, "jinja2")
131         installer = AutoInstaller(append_to_search_path=True, target_dir=jinja_dir)
132         installer.install(url="http://pypi.python.org/packages/source/J/Jinja2/Jinja2-2.6.tar.gz#md5=1c49a8825c993bfdcf55bb36897d28a2",
133                                                 url_subpath="Jinja2-2.6/jinja2")
134
135         SQLAlchemy_dir = self._fs.join(_AUTOINSTALLED_DIR, "sqlalchemy")
136         installer = AutoInstaller(append_to_search_path=True, target_dir=SQLAlchemy_dir)
137         installer.install(url="http://pypi.python.org/packages/source/S/SQLAlchemy/SQLAlchemy-0.7.7.tar.gz#md5=ddf6df7e014cea318fa981364f3f93b9",
138                                                  url_subpath="SQLAlchemy-0.7.7/lib/sqlalchemy")
139
140         twisted_dir = self._fs.join(_AUTOINSTALLED_DIR, "twisted")
141         installer = AutoInstaller(prepend_to_search_path=True, target_dir=twisted_dir)
142         installer.install(url="https://pypi.python.org/packages/source/T/Twisted/Twisted-12.1.0.tar.bz2#md5=f396f1d6f5321e869c2f89b2196a9eb5", url_subpath="Twisted-12.1.0/twisted")
143
144         self._install("http://pypi.python.org/packages/source/b/buildbot/buildbot-0.8.6p1.tar.gz#md5=b6727d2810c692062c657492bcbeac6a", "buildbot-0.8.6p1/buildbot")
145
146     def _install_coverage(self):
147         self._ensure_autoinstalled_dir_is_in_sys_path()
148         self._install(url="http://pypi.python.org/packages/source/c/coverage/coverage-3.5.1.tar.gz#md5=410d4c8155a4dab222f2bc51212d4a24", url_subpath="coverage-3.5.1/coverage")
149
150     def _install_eliza(self):
151         self._install(url="http://www.adambarth.com/webkit/eliza", target_name="eliza.py")
152
153     def _install_twisted(self):
154         twisted_dir = self._fs.join(_AUTOINSTALLED_DIR, "twisted")
155         installer = AutoInstaller(prepend_to_search_path=True, target_dir=twisted_dir)
156         installer.install(url="https://pypi.python.org/packages/source/T/Twisted/Twisted-12.1.0.tar.bz2#md5=f396f1d6f5321e869c2f89b2196a9eb5", url_subpath="Twisted-12.1.0/twisted")
157
158     def _install(self, url, url_subpath=None, target_name=None):
159         installer = AutoInstaller(target_dir=_AUTOINSTALLED_DIR)
160         installer.install(url=url, url_subpath=url_subpath, target_name=target_name)
161
162
163 _hook = AutoinstallImportHook()
164 sys.meta_path.append(_hook)
165
166
167 def autoinstall_everything():
168     install_methods = [method for method in dir(_hook.__class__) if method.startswith('_install_')]
169     for method in install_methods:
170         getattr(_hook, method)()