|
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.28.tar.gz', 'local':"distribute-0.6.28.tar.gz", 'install': {'method': 'pip', 'option_str': None, 'dict_extra_env': None}}, |
|
20 'PYCRYPTO': {'setup': 'pycrypto', 'url':'https://ftp.dlitz.net/pub/dlitz/crypto/pycrypto/pycrypto-2.6.tar.gz', 'local':'pycrypto-2.6.tar.gz', 'install': {'method': 'pip', 'option_str': None, 'dict_extra_env': None}}, |
|
21 'PARAMIKO' : {'setup': 'paramiko', 'url':'https://github.com/paramiko/paramiko/archive/v1.9.0.tar.gz', 'local':'paramiko-1.9.0.tar.gz', 'install': {'method': 'pip', 'option_str': None, 'dict_extra_env': None}}, |
|
22 'FABRIC': {'setup': 'fabric', 'url':'https://github.com/fabric/fabric/tarball/1.5.3', 'local':'fabric-1.5.3.tar.gz', 'install': {'method': 'pip', 'option_str': None, 'dict_extra_env': None}}, |
|
23 'MERCURIAL': {'setup': 'mercurial', 'url':'http://mercurial.selenic.com/release/mercurial-2.5.1.tar.gz', 'local':'mercurial-2.5.1.tar.gz', 'install': {'method': 'pip', 'option_str': None, 'dict_extra_env': None}}, |
|
24 } |
|
25 |
|
26 class ResourcesEnv(object): |
|
27 |
|
28 def __init__(self, src_base, urls, normal_installs): |
|
29 self.src_base = src_base |
|
30 self.URLS = {} |
|
31 self.__init_url(urls) |
|
32 self.NORMAL_INSTALL = normal_installs |
|
33 |
|
34 def get_src_base_path(self, fpath): |
|
35 return os.path.abspath(os.path.join(self.src_base, fpath)).replace("\\","/") |
|
36 |
|
37 def __add_package_def(self, key, dict): |
|
38 self.URLS[key] = dict |
|
39 |
|
40 def __init_url(self, urls): |
|
41 for key, url_dict in urls.items(): |
|
42 url_dict_copy = url_dict.copy() |
|
43 if not url_dict['url'].startswith("http://"): |
|
44 url_dict_copy['url'] = self.get_src_base_path(url_dict['url']) |
|
45 url_dict_copy['local'] = self.get_src_base_path(url_dict['local']) |
|
46 |
|
47 self.__add_package_def(key, url_dict_copy ) |
|
48 |
|
49 def ensure_dir(dir, logger): |
|
50 if not os.path.exists(dir): |
|
51 logger.notify('Creating directory %s' % dir) |
|
52 os.makedirs(dir) |
|
53 |
|
54 def extend_parser(parser): |
|
55 parser.add_option( |
|
56 '--index-url', |
|
57 metavar='INDEX_URL', |
|
58 dest='index_url', |
|
59 default='http://pypi.python.org/simple/', |
|
60 help='base URL of Python Package Index') |
|
61 parser.add_option( |
|
62 '--type-install', |
|
63 metavar='type_install', |
|
64 dest='type_install', |
|
65 help='type install : local, url, setup - default : local') |
|
66 parser.add_option( |
|
67 '--ignore-packages', |
|
68 metavar='ignore_packages', |
|
69 dest='ignore_packages', |
|
70 default=None, |
|
71 help='list of comma separated keys for package to ignore') |
|
72 |
|
73 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): |
|
74 psycopg2_src = os.path.join(src_dir,"psycopg2.zip") |
|
75 shutil.copy(res_env.URLS['PSYCOPG2'][res_source_key], psycopg2_src) |
|
76 #extract psycopg2 |
|
77 zf = zipfile.ZipFile(psycopg2_src) |
|
78 psycopg2_base_path = os.path.join(src_dir,"psycopg2") |
|
79 zf.extractall(psycopg2_base_path) |
|
80 zf.close() |
|
81 |
|
82 psycopg2_src_path = os.path.join(psycopg2_base_path, os.listdir(psycopg2_base_path)[0]) |
|
83 shutil.copytree(os.path.join(psycopg2_src_path, 'psycopg2'), os.path.abspath(os.path.join(home_dir, 'Lib/site-packages', 'psycopg2'))) |
|
84 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'))) |
|
85 |
|
86 |
|
87 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): |
|
88 |
|
89 args = [os.path.abspath(os.path.join(home_dir, 'bin', 'pip')), 'install', res_env.URLS['MYSQL'][res_source_key]] |
|
90 if option_str : |
|
91 args.insert(4,option_str) |
|
92 call_subprocess(args, |
|
93 cwd=os.path.abspath(tmp_dir), |
|
94 filter_stdout=filter_python_develop, |
|
95 show_stdout=True, |
|
96 extra_env=extra_env) |
|
97 |
|
98 mysqlconfig_output = [] |
|
99 |
|
100 call_subprocess(['mysql_config', '--libmysqld-libs'], |
|
101 cwd=os.path.abspath(tmp_dir), |
|
102 filter_stdout=lambda line: mysqlconfig_output.append(line), |
|
103 show_stdout=True) |
|
104 |
|
105 mysqlconfig_output = "".join(mysqlconfig_output) |
|
106 m = re.search("\-L[\'\"]?([\w\/]+)[\'\"]?", mysqlconfig_output) |
|
107 if m: |
|
108 repdylibpath = m.group(1) |
|
109 else: |
|
110 repdylibpath = '/usr/local/mysql/lib' |
|
111 |
|
112 dyliblist = glob.glob(repdylibpath+"/libmysqlclient.*.dylib") |
|
113 def key_func(s): |
|
114 m = re.match(repdylibpath+"/libmysqlclient\.([\d]+)\.dylib", s) |
|
115 if m: |
|
116 return int(m.group(1)) |
|
117 else: |
|
118 return sys.maxint |
|
119 dyliblist.sort(key=key_func) |
|
120 |
|
121 if dyliblist: |
|
122 dylibpath = dyliblist[0] |
|
123 else: |
|
124 dylibpath = '/usr/local/mysql/lib/libmysqlclient.18.dylib' |
|
125 |
|
126 dylibname = os.path.basename(dylibpath) |
|
127 sopath = os.path.join(os.path.abspath(lib_dir), 'site-packages', '_mysql.so') |
|
128 |
|
129 call_subprocess(['install_name_tool', '-change', dylibname, dylibpath, sopath], |
|
130 cwd=os.path.abspath(tmp_dir), |
|
131 filter_stdout=filter_python_develop, |
|
132 show_stdout=True) |
|
133 |
|
134 |
|
135 def gen_install_comp_lib(lib_name, lib_key, configure_options=[]): |
|
136 |
|
137 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): |
|
138 lib_src = os.path.join(src_dir,lib_name+".tar.gz") |
|
139 shutil.copy(res_env.URLS[lib_key][res_source_key], lib_src) |
|
140 tf = tarfile.open(lib_src,'r:gz') |
|
141 lib_base_path = os.path.join(src_dir, lib_name) |
|
142 logger.notify("Extract %s to %s " % (lib_name,lib_base_path)) |
|
143 tf.extractall(lib_base_path) |
|
144 tf.close() |
|
145 |
|
146 lib_src_path = os.path.join(lib_base_path, os.listdir(lib_base_path)[0]) |
|
147 |
|
148 logger.notify(libname + " configure") |
|
149 call_subprocess(['configure', '--prefix='+os.path.abspath(home_dir)] + configure_options, |
|
150 cwd=os.path.abspath(lib_src_path), |
|
151 filter_stdout=filter_python_develop, |
|
152 show_stdout=True) |
|
153 |
|
154 logger.notify(libname + " make") |
|
155 call_subprocess(['make'], |
|
156 cwd=os.path.abspath(lib_src_path), |
|
157 filter_stdout=filter_python_develop, |
|
158 show_stdout=True) |
|
159 |
|
160 logger.notify(libname + "make install") |
|
161 call_subprocess(['make', 'install'], |
|
162 cwd=os.path.abspath(lib_src_path), |
|
163 filter_stdout=filter_python_develop, |
|
164 show_stdout=True) |
|
165 return install_lib |
|
166 |
|
167 install_libjpeg = gen_install_comp_lib("libjpeg", "LIBJPEG", ['--enable-shared']) |
|
168 install_zlib = gen_install_comp_lib("zlib", "ZLIB", []) |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 def lib_generate_install_methods(path_locations, src_base, Logger, call_subprocess, normal_installs, options_to_add=None, urls= None): |
|
175 |
|
176 all_urls = URLS.copy() |
|
177 if urls is not None: |
|
178 all_urls.update(urls) |
|
179 |
|
180 res_env = ResourcesEnv(src_base, all_urls, normal_installs) |
|
181 |
|
182 def filter_python_develop(line): |
|
183 if not line.strip(): |
|
184 return Logger.DEBUG |
|
185 for prefix in ['Searching for', 'Reading ', 'Best match: ', 'Processing ', |
|
186 'Moving ', 'Adding ', 'running ', 'writing ', 'Creating ', |
|
187 'creating ', 'Copying ']: |
|
188 if line.startswith(prefix): |
|
189 return Logger.DEBUG |
|
190 return Logger.NOTIFY |
|
191 |
|
192 |
|
193 def normal_install(key, method, option_str, extra_env, res_source_key, home_dir, tmp_dir, res_env, logger, call_subprocess): |
|
194 logger.notify("Install %s from %s with %s" % (key,res_env.URLS[key][res_source_key],method)) |
|
195 if method == 'pip': |
|
196 if sys.platform == 'win32': |
|
197 args = [os.path.abspath(os.path.join(home_dir, 'Scripts', 'pip')), 'install', res_env.URLS[key][res_source_key]] |
|
198 else: |
|
199 args = [os.path.abspath(os.path.join(home_dir, 'bin', 'pip')), 'install', res_env.URLS[key][res_source_key]] |
|
200 if option_str : |
|
201 args.insert(4,option_str) |
|
202 call_subprocess(args, |
|
203 cwd=os.path.abspath(tmp_dir), |
|
204 filter_stdout=filter_python_develop, |
|
205 show_stdout=True, |
|
206 extra_env=extra_env) |
|
207 else: |
|
208 if sys.platform == 'win32': |
|
209 args = [os.path.abspath(os.path.join(home_dir, 'Scripts', 'easy_install')), res_env.URLS[key][res_source_key]] |
|
210 else: |
|
211 args = [os.path.abspath(os.path.join(home_dir, 'bin', 'easy_install')), res_env.URLS[key][res_source_key]] |
|
212 if option_str : |
|
213 args.insert(1,option_str) |
|
214 call_subprocess(args, |
|
215 cwd=os.path.abspath(tmp_dir), |
|
216 filter_stdout=filter_python_develop, |
|
217 show_stdout=True, |
|
218 extra_env=extra_env) |
|
219 |
|
220 |
|
221 def after_install(options, home_dir): |
|
222 |
|
223 global logger |
|
224 |
|
225 verbosity = options.verbose - options.quiet |
|
226 logger = Logger([(Logger.level_for_integer(2-verbosity), sys.stdout)]) |
|
227 |
|
228 |
|
229 home_dir, lib_dir, inc_dir, bin_dir = path_locations(home_dir) |
|
230 base_dir = os.path.dirname(home_dir) |
|
231 src_dir = os.path.join(home_dir, 'src') |
|
232 tmp_dir = os.path.join(home_dir, 'tmp') |
|
233 ensure_dir(src_dir, logger) |
|
234 ensure_dir(tmp_dir, logger) |
|
235 system_str = platform.system() |
|
236 |
|
237 res_source_key = getattr(options, 'type_install') if hasattr(options, 'type_install') else 'local' #.get('type_install', 'local') |
|
238 if res_source_key is None: |
|
239 res_source_key = local |
|
240 |
|
241 ignore_packages = [] |
|
242 |
|
243 if system_str == 'Windows': |
|
244 default_install_options = {'method': 'easy_install', 'option_str': None, 'dict_extra_env': None} |
|
245 else: |
|
246 default_install_options = {'method': 'pip', 'option_str': None, 'dict_extra_env': None} |
|
247 |
|
248 if options.ignore_packages : |
|
249 ignore_packages = options.ignore_packages.split(",") |
|
250 |
|
251 logger.indent += 2 |
|
252 try: |
|
253 for key in res_env.NORMAL_INSTALL: |
|
254 if key not in res_env.URLS: |
|
255 logger.notify("%s not found in def : passing" % (key,)) |
|
256 install_options = res_env.URLS[key].get('install', None) |
|
257 if install_options is None: |
|
258 install_options = default_install_options |
|
259 method = install_options.get('method', default_install_options['method']) |
|
260 option_str = install_options.get('option_str', default_install_options['option_str']) |
|
261 extra_env = install_options.get('dict_extra_env', default_install_options['dict_extra_env']) |
|
262 #isinstance(lst, (list, tuple)) |
|
263 if key not in ignore_packages: |
|
264 if callable(method): |
|
265 method(option_str, extra_env, res_source_key, home_dir, lib_dir, tmp_dir, src_dir, res_env, logger, call_subprocess, filter_python_develop) |
|
266 elif method in globals() and callable(globals()[method]) and method not in ['pip', 'easy_install']: |
|
267 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) |
|
268 else: |
|
269 normal_install(key, method, option_str, extra_env, res_source_key, home_dir, tmp_dir, res_env, logger, call_subprocess) |
|
270 |
|
271 logger.notify("Clear source dir") |
|
272 shutil.rmtree(src_dir) |
|
273 |
|
274 finally: |
|
275 logger.indent -= 2 |
|
276 script_dir = join(base_dir, bin_dir) |
|
277 logger.notify('Run "%s Package" to install new packages that provide builds' |
|
278 % join(script_dir, 'easy_install')) |
|
279 |
|
280 def adjust_options(options, args): |
|
281 if not options_to_add: |
|
282 pass |
|
283 for opt in options_to_add: |
|
284 test_opt = opt.split('=',1)[0] |
|
285 if not hasattr(options,test_opt) or getattr(options, test_opt) is None: |
|
286 setattr(options, test_opt,opt.split('=',1)[1] if "=" in opt else True) |
|
287 |
|
288 return adjust_options, extend_parser, after_install |