|
1 import sys |
|
2 import os |
|
3 import os.path |
|
4 import shutil |
|
5 import tarfile |
|
6 import zipfile |
|
7 import urllib |
|
8 import platform |
|
9 import patch |
|
10 import struct |
|
11 import glob |
|
12 import re |
|
13 |
|
14 join = os.path.join |
|
15 system_str = platform.system() |
|
16 |
|
17 URLS = { |
|
18 #'': {'setup': '', 'url':'', 'local':''}, |
|
19 'DISTRIBUTE': {'setup': 'distribute', 'url':'http://pypi.python.org/packages/source/d/distribute/distribute-0.6.32.tar.gz', 'local':"distribute-0.6.32.tar.gz", 'install': {'method': 'pip', 'option_str': None, 'dict_extra_env': None}}, |
|
20 'ANYJSON': {'setup': 'anyjson', 'url':'http://pypi.python.org/packages/source/a/anyjson/anyjson-0.3.3.tar.gz', 'local':"anyjson-0.3.3.tar.gz", 'install': {'method': 'pip', 'option_str': None, 'dict_extra_env': None}}, |
|
21 'OAUTH2': { 'setup': 'python-oauth2', 'url':"https://github.com/simplegeo/python-oauth2/tarball/hudson-python-oauth2-211", 'local':"python-oauth2-1.5-211.tar.gz", 'install': {'method': 'pip', 'option_str': None, 'dict_extra_env': None}}, |
|
22 'HTTPLIB2': { 'setup': 'httplib2', 'url':'http://pypi.python.org/packages/source/h/httplib2/httplib2-0.7.7.tar.gz', 'local':"httplib2-0.7.7.tar.gz", 'install': {'method': 'pip', 'option_str': None, 'dict_extra_env': None}}, |
|
23 'LOCKFILE': {'setup': 'lockfile', 'url':'http://code.google.com/p/pylockfile/downloads/detail?name=lockfile-0.9.1.tar.gz', 'local':"lockfile-0.9.1.tar.gz", 'install': {'method': 'pip', 'option_str': None, 'dict_extra_env': None}}, |
|
24 'DAEMON': {'setup': 'python-daemon', 'url':'http://pypi.python.org/packages/source/p/python-daemon/python-daemon-1.5.5.tar.gz', 'local':"python-daemon-1.5.5.tar.gz", 'install': {'method': 'pip', 'option_str': None, 'dict_extra_env': None}}, |
|
25 'DATEUTIL': {'setup': 'python-dateutil', 'url':'http://pypi.python.org/packages/source/p/python-dateutil/python-dateutil-2.1.tar.gz', 'local':"python-dateutil-2.1.tar.gz", 'install': {'method': 'pip', 'option_str': None, 'dict_extra_env': None}}, |
|
26 'PYTZ': {'setup': 'pytz', 'url':'http://pypi.python.org/packages/source/p/pytz/pytz-2012h.tar.bz2', 'local':"pytz-2012h.tar.bz2", 'install': {'method': 'pip', 'option_str': None, 'dict_extra_env': None}}, |
|
27 'SIMPLEJSON': {'setup': 'simplejson', 'url':'http://pypi.python.org/packages/source/s/simplejson/simplejson-2.6.2.tar.gz', 'local':"simplejson-2.6.2.tar.gz", 'install': {'method': 'pip', 'option_str': None, 'dict_extra_env': None}}, |
|
28 'SQLALCHEMY': {'setup': 'sqlalchemy', 'url':'http://downloads.sourceforge.net/project/sqlalchemy/sqlalchemy/0.8.0b1/SQLAlchemy-0.8.0b1.tar.gz?r=http%3A%2F%2Fwww.sqlalchemy.org%2Fdownload.html&ts=1355091775&use_mirror=ignum', 'local':"SQLAlchemy-0.8.0b1.tar.gz", 'install': {'method': 'pip', 'option_str': None, 'dict_extra_env': None}}, |
|
29 'TWEEPY': {'setup': 'tweepy', 'url':'https://github.com/tweepy/tweepy/archive/1.12.tar.gz', 'local':"tweepy-1.12.tar.gz", 'install': {'method': 'pip', 'option_str': None, 'dict_extra_env': None}}, |
|
30 'TWITTER': {'setup': 'twitter', 'url':'http://pypi.python.org/packages/source/t/twitter/twitter-1.9.0.tar.gz', 'local':"twitter-1.9.0.tar.gz", 'install': {'method': 'pip', 'option_str': None, 'dict_extra_env': None}}, |
|
31 'TWITTER-TEXT': {'setup': 'twitter-text', 'url':'https://github.com/dryan/twitter-text-py/archive/master.tar.gz', 'local':"twitter-text-1.0.4.tar.gz", 'install': {'method': 'pip', 'option_str': None, 'dict_extra_env': None}}, |
|
32 'REQUESTS': {'setup': 'requests', 'url':'https://github.com/kennethreitz/requests/tarball/master', 'local':"requests-v0.14.2.tar.gz", 'install': {'method': 'pip', 'option_str': None, 'dict_extra_env': None}}, |
|
33 } |
|
34 |
|
35 if system_str == 'Windows': |
|
36 |
|
37 URLS.update({ |
|
38 'PSYCOPG2': {'setup': 'psycopg2','url': 'psycopg2-2.4.5.win32-py2.7-pg9.1.3-release.zip', 'local':"psycopg2-2.4.5.win32-py2.7-pg9.1.3-release.zip", 'install': {'method': 'install_psycopg2', 'option_str': None, 'dict_extra_env': None}}, |
|
39 'LXML': {'setup': 'lxml', 'url': 'http://pypi.python.org/packages/2.7/l/lxml/lxml-2.3-py2.7-win32.egg', 'local':"lxml-2.3-py2.7-win32.egg", 'install': {'method': 'easy_install', 'option_str': None, 'dict_extra_env': None}}, |
|
40 }) |
|
41 else: |
|
42 if system_str == "Darwin": |
|
43 lxml_options = {'STATIC_DEPS': 'true', 'LIBXML2_VERSION': '2.8.0', 'LIBXSLT_VERSION': '1.1.28', 'LIBICONV_VERSION': '1.14'} |
|
44 lxml_method = 'pip' |
|
45 else: |
|
46 lxml_options = None |
|
47 lxml_method = 'pip' |
|
48 |
|
49 URLS.update({ |
|
50 'PSYCOPG2': {'setup': 'psycopg2','url': 'http://www.psycopg.org/psycopg/tarballs/PSYCOPG-2-4/psycopg2-2.4.5.tar.gz', 'local':"psycopg2-2.4.5.tar.gz", 'install': {'method': 'pip', 'option_str': None, 'dict_extra_env': None}}, |
|
51 'LXML': {'setup': 'lxml', 'url':"lxml-3.0.1.tar.gz", 'local':"lxml-3.0.1.tar.gz", 'install': {'method': lxml_method, 'option_str': None, 'dict_extra_env': lxml_options}}, |
|
52 }) |
|
53 |
|
54 |
|
55 class ResourcesEnv(object): |
|
56 |
|
57 def __init__(self, src_base, urls, normal_installs): |
|
58 self.src_base = src_base |
|
59 self.URLS = {} |
|
60 self.__init_url(urls) |
|
61 self.NORMAL_INSTALL = normal_installs |
|
62 |
|
63 def get_src_base_path(self, fpath): |
|
64 return os.path.abspath(os.path.join(self.src_base, fpath)).replace("\\","/") |
|
65 |
|
66 def __add_package_def(self, key, dict): |
|
67 self.URLS[key] = dict |
|
68 |
|
69 def __init_url(self, urls): |
|
70 for key, url_dict in urls.items(): |
|
71 url_dict_copy = url_dict.copy() |
|
72 if not url_dict['url'].startswith("http://"): |
|
73 url_dict_copy['url'] = self.get_src_base_path(url_dict['url']) |
|
74 url_dict_copy['local'] = self.get_src_base_path(url_dict['local']) |
|
75 |
|
76 self.__add_package_def(key, url_dict_copy ) |
|
77 |
|
78 def ensure_dir(dir, logger): |
|
79 if not os.path.exists(dir): |
|
80 logger.notify('Creating directory %s' % dir) |
|
81 os.makedirs(dir) |
|
82 |
|
83 def extend_parser(parser): |
|
84 parser.add_option( |
|
85 '--index-url', |
|
86 metavar='INDEX_URL', |
|
87 dest='index_url', |
|
88 default='http://pypi.python.org/simple/', |
|
89 help='base URL of Python Package Index') |
|
90 parser.add_option( |
|
91 '--type-install', |
|
92 metavar='type_install', |
|
93 dest='type_install', |
|
94 help='type install : local, url, setup - default : local') |
|
95 parser.add_option( |
|
96 '--ignore-packages', |
|
97 metavar='ignore_packages', |
|
98 dest='ignore_packages', |
|
99 default=None, |
|
100 help='list of comma separated keys for package to ignore') |
|
101 |
|
102 def install_psycopg2(option_str, extra_env, res_source_key, home_dir, lib_dir, tmp_dir, src_dir, res_env, logger, call_subprocess, filter_python_develop): |
|
103 psycopg2_src = os.path.join(src_dir,"psycopg2.zip") |
|
104 shutil.copy(res_env.URLS['PSYCOPG2'][res_source_key], psycopg2_src) |
|
105 #extract psycopg2 |
|
106 zf = zipfile.ZipFile(psycopg2_src) |
|
107 psycopg2_base_path = os.path.join(src_dir,"psycopg2") |
|
108 zf.extractall(psycopg2_base_path) |
|
109 zf.close() |
|
110 |
|
111 psycopg2_src_path = os.path.join(psycopg2_base_path, os.listdir(psycopg2_base_path)[0]) |
|
112 shutil.copytree(os.path.join(psycopg2_src_path, 'psycopg2'), os.path.abspath(os.path.join(home_dir, 'Lib/site-packages', 'psycopg2'))) |
|
113 shutil.copy(os.path.join(psycopg2_src_path, 'psycopg2-2.4.5-py2.7.egg-info'), os.path.abspath(os.path.join(home_dir, 'Lib/site-packages', 'site-packages'))) |
|
114 |
|
115 |
|
116 def install_mysql(option_str, extra_env, res_source_key, home_dir, lib_dir, tmp_dir, src_dir, res_env, logger, call_subprocess, filter_python_develop): |
|
117 |
|
118 args = [os.path.abspath(os.path.join(home_dir, 'bin', 'pip')), 'install', res_env.URLS['MYSQL'][res_source_key]] |
|
119 if option_str : |
|
120 args.insert(4,option_str) |
|
121 call_subprocess(args, |
|
122 cwd=os.path.abspath(tmp_dir), |
|
123 filter_stdout=filter_python_develop, |
|
124 show_stdout=True, |
|
125 extra_env=extra_env) |
|
126 |
|
127 mysqlconfig_output = [] |
|
128 |
|
129 call_subprocess(['mysql_config', '--libmysqld-libs'], |
|
130 cwd=os.path.abspath(tmp_dir), |
|
131 filter_stdout=lambda line: mysqlconfig_output.append(line), |
|
132 show_stdout=True) |
|
133 |
|
134 mysqlconfig_output = "".join(mysqlconfig_output) |
|
135 m = re.search("\-L[\'\"]?([\w\/]+)[\'\"]?", mysqlconfig_output) |
|
136 if m: |
|
137 repdylibpath = m.group(1) |
|
138 else: |
|
139 repdylibpath = '/usr/local/mysql/lib' |
|
140 |
|
141 dyliblist = glob.glob(repdylibpath+"/libmysqlclient.*.dylib") |
|
142 def key_func(s): |
|
143 m = re.match(repdylibpath+"/libmysqlclient\.([\d]+)\.dylib", s) |
|
144 if m: |
|
145 return int(m.group(1)) |
|
146 else: |
|
147 return sys.maxint |
|
148 dyliblist.sort(key=key_func) |
|
149 |
|
150 if dyliblist: |
|
151 dylibpath = dyliblist[0] |
|
152 else: |
|
153 dylibpath = '/usr/local/mysql/lib/libmysqlclient.18.dylib' |
|
154 |
|
155 dylibname = os.path.basename(dylibpath) |
|
156 sopath = os.path.join(os.path.abspath(lib_dir), 'site-packages', '_mysql.so') |
|
157 |
|
158 call_subprocess(['install_name_tool', '-change', dylibname, dylibpath, sopath], |
|
159 cwd=os.path.abspath(tmp_dir), |
|
160 filter_stdout=filter_python_develop, |
|
161 show_stdout=True) |
|
162 |
|
163 |
|
164 def gen_install_comp_lib(lib_name, lib_key, configure_options=[]): |
|
165 |
|
166 def install_lib(option_str, extra_env, res_source_key, home_dir, lib_dir, tmp_dir, src_dir, res_env, logger, call_subprocess, filter_python_develop): |
|
167 lib_src = os.path.join(src_dir,lib_name+".tar.gz") |
|
168 shutil.copy(res_env.URLS[lib_key][res_source_key], lib_src) |
|
169 tf = tarfile.open(lib_src,'r:gz') |
|
170 lib_base_path = os.path.join(src_dir, lib_name) |
|
171 logger.notify("Extract %s to %s " % (lib_name,lib_base_path)) |
|
172 tf.extractall(lib_base_path) |
|
173 tf.close() |
|
174 |
|
175 lib_src_path = os.path.join(lib_base_path, os.listdir(lib_base_path)[0]) |
|
176 |
|
177 logger.notify(libname + " configure") |
|
178 call_subprocess(['configure', '--prefix='+os.path.abspath(home_dir)] + configure_options, |
|
179 cwd=os.path.abspath(lib_src_path), |
|
180 filter_stdout=filter_python_develop, |
|
181 show_stdout=True) |
|
182 |
|
183 logger.notify(libname + " make") |
|
184 call_subprocess(['make'], |
|
185 cwd=os.path.abspath(lib_src_path), |
|
186 filter_stdout=filter_python_develop, |
|
187 show_stdout=True) |
|
188 |
|
189 logger.notify(libname + "make install") |
|
190 call_subprocess(['make', 'install'], |
|
191 cwd=os.path.abspath(lib_src_path), |
|
192 filter_stdout=filter_python_develop, |
|
193 show_stdout=True) |
|
194 return install_lib |
|
195 |
|
196 install_libjpeg = gen_install_comp_lib("libjpeg", "LIBJPEG", ['--enable-shared']) |
|
197 install_zlib = gen_install_comp_lib("zlib", "ZLIB", []) |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 def lib_generate_install_methods(path_locations, src_base, Logger, call_subprocess, normal_installs, options_to_add=None, urls= None): |
|
204 |
|
205 all_urls = URLS.copy() |
|
206 if urls is not None: |
|
207 all_urls.update(urls) |
|
208 |
|
209 res_env = ResourcesEnv(src_base, all_urls, normal_installs) |
|
210 |
|
211 def filter_python_develop(line): |
|
212 if not line.strip(): |
|
213 return Logger.DEBUG |
|
214 for prefix in ['Searching for', 'Reading ', 'Best match: ', 'Processing ', |
|
215 'Moving ', 'Adding ', 'running ', 'writing ', 'Creating ', |
|
216 'creating ', 'Copying ']: |
|
217 if line.startswith(prefix): |
|
218 return Logger.DEBUG |
|
219 return Logger.NOTIFY |
|
220 |
|
221 |
|
222 def normal_install(key, method, option_str, extra_env, res_source_key, home_dir, tmp_dir, res_env, logger, call_subprocess): |
|
223 logger.notify("Install %s from %s with %s" % (key,res_env.URLS[key][res_source_key],method)) |
|
224 if method == 'pip': |
|
225 if sys.platform == 'win32': |
|
226 args = [os.path.abspath(os.path.join(home_dir, 'Scripts', 'pip')), 'install', res_env.URLS[key][res_source_key]] |
|
227 else: |
|
228 args = [os.path.abspath(os.path.join(home_dir, 'bin', 'pip')), 'install', res_env.URLS[key][res_source_key]] |
|
229 if option_str : |
|
230 args.insert(4,option_str) |
|
231 call_subprocess(args, |
|
232 cwd=os.path.abspath(tmp_dir), |
|
233 filter_stdout=filter_python_develop, |
|
234 show_stdout=True, |
|
235 extra_env=extra_env) |
|
236 else: |
|
237 if sys.platform == 'win32': |
|
238 args = [os.path.abspath(os.path.join(home_dir, 'Scripts', 'easy_install')), res_env.URLS[key][res_source_key]] |
|
239 else: |
|
240 args = [os.path.abspath(os.path.join(home_dir, 'bin', 'easy_install')), res_env.URLS[key][res_source_key]] |
|
241 if option_str : |
|
242 args.insert(1,option_str) |
|
243 call_subprocess(args, |
|
244 cwd=os.path.abspath(tmp_dir), |
|
245 filter_stdout=filter_python_develop, |
|
246 show_stdout=True, |
|
247 extra_env=extra_env) |
|
248 |
|
249 |
|
250 def after_install(options, home_dir): |
|
251 |
|
252 global logger |
|
253 |
|
254 verbosity = options.verbose - options.quiet |
|
255 logger = Logger([(Logger.level_for_integer(2-verbosity), sys.stdout)]) |
|
256 |
|
257 |
|
258 home_dir, lib_dir, inc_dir, bin_dir = path_locations(home_dir) |
|
259 base_dir = os.path.dirname(home_dir) |
|
260 src_dir = os.path.join(home_dir, 'src') |
|
261 tmp_dir = os.path.join(home_dir, 'tmp') |
|
262 ensure_dir(src_dir, logger) |
|
263 ensure_dir(tmp_dir, logger) |
|
264 system_str = platform.system() |
|
265 |
|
266 res_source_key = getattr(options, 'type_install') if hasattr(options, 'type_install') else 'local' #.get('type_install', 'local') |
|
267 if res_source_key is None: |
|
268 res_source_key = local |
|
269 |
|
270 ignore_packages = [] |
|
271 |
|
272 if system_str == 'Windows': |
|
273 default_install_options = {'method': 'easy_install', 'option_str': None, 'dict_extra_env': None} |
|
274 else: |
|
275 default_install_options = {'method': 'pip', 'option_str': None, 'dict_extra_env': None} |
|
276 |
|
277 if options.ignore_packages : |
|
278 ignore_packages = options.ignore_packages.split(",") |
|
279 |
|
280 logger.indent += 2 |
|
281 try: |
|
282 for key in res_env.NORMAL_INSTALL: |
|
283 if key not in res_env.URLS: |
|
284 logger.notify("%s not found in def : passing" % (key,)) |
|
285 install_options = res_env.URLS[key].get('install', None) |
|
286 if install_options is None: |
|
287 install_options = default_install_options |
|
288 method = install_options.get('method', default_install_options['method']) |
|
289 option_str = install_options.get('option_str', default_install_options['option_str']) |
|
290 extra_env = install_options.get('dict_extra_env', default_install_options['dict_extra_env']) |
|
291 #isinstance(lst, (list, tuple)) |
|
292 if key not in ignore_packages: |
|
293 if callable(method): |
|
294 method(option_str, extra_env, res_source_key, home_dir, lib_dir, tmp_dir, src_dir, res_env, logger, call_subprocess, filter_python_develop) |
|
295 elif method in globals() and callable(globals()[method]) and method not in ['pip', 'easy_install']: |
|
296 globals()[method](option_str, extra_env, res_source_key, home_dir, lib_dir, tmp_dir, src_dir, res_env, logger, call_subprocess, filter_python_develop) |
|
297 else: |
|
298 normal_install(key, method, option_str, extra_env, res_source_key, home_dir, tmp_dir, res_env, logger, call_subprocess) |
|
299 |
|
300 logger.notify("Clear source dir") |
|
301 shutil.rmtree(src_dir) |
|
302 |
|
303 finally: |
|
304 logger.indent -= 2 |
|
305 script_dir = join(base_dir, bin_dir) |
|
306 logger.notify('Run "%s Package" to install new packages that provide builds' |
|
307 % join(script_dir, 'easy_install')) |
|
308 |
|
309 def adjust_options(options, args): |
|
310 if not options_to_add: |
|
311 pass |
|
312 for opt in options_to_add: |
|
313 test_opt = opt.split('=',1)[0] |
|
314 if not hasattr(options,test_opt) or getattr(options, test_opt) is None: |
|
315 setattr(options, test_opt,opt.split('=',1)[1] if "=" in opt else True) |
|
316 |
|
317 return adjust_options, extend_parser, after_install |