|
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 logging |
|
11 |
|
12 class ResourcesEnv(object): |
|
13 |
|
14 def __init__(self, src_base): |
|
15 self.src_base = src_base |
|
16 self.URLS = {} |
|
17 self.__init_url() |
|
18 self.NORMAL_INSTALL = [] |
|
19 self.__init_normal_install() |
|
20 |
|
21 def get_src_base_path(self, fpath): |
|
22 return os.path.abspath(os.path.join(self.src_base, fpath)).replace("\\","/") |
|
23 |
|
24 def __add_package_def(self, key, setup, url, local): |
|
25 self.URLS[key] = {'setup':setup, 'url':url, 'local':self.get_src_base_path(local)} |
|
26 |
|
27 def __init_url(self): |
|
28 self.__add_package_def('DISTRIBUTE', setup='distribute', url='http://pypi.python.org/packages/source/d/distribute/distribute-0.6.14.tar.gz', local="distribute-0.6.14.tar.gz") |
|
29 self.__add_package_def('MERCURIAL', setup='mercurial', url='http://pypi.python.org/packages/source/d/mercurial/mercurial-1.7.5.tar.gz', local="mercurial-1.7.5.tar.gz") |
|
30 self.__add_package_def('SETUPTOOLS_HG', setup='mercurial_hg', url='http://pypi.python.org/packages/source/s/setuptools_hg/setuptools_hg-0.2.tar.gz', local="setuptools_hg-0.2.tar.gz") |
|
31 |
|
32 |
|
33 def __init_normal_install(self): |
|
34 system_str = platform.system() |
|
35 self.NORMAL_INSTALL = [ #(key,method, option_str, extra_env) |
|
36 ('DISTRIBUTE', 'pip', None, None), |
|
37 ('MERCURIAL', 'pip', None, None), |
|
38 ('SETUPTOOLS-HG', 'pip', None, None), |
|
39 ] |
|
40 |
|
41 def extend_parser(parser): |
|
42 |
|
43 parser.add_option( |
|
44 '--index-url', |
|
45 metavar='INDEX_URL', |
|
46 dest='index_url', |
|
47 default='http://pypi.python.org/simple/', |
|
48 help='base URL of Python Package Index') |
|
49 parser.add_option( |
|
50 '--type-install', |
|
51 metavar='type_install', |
|
52 dest='type_install', |
|
53 default='local', |
|
54 help='type install : local, url, setup') |
|
55 parser.add_option( |
|
56 '--ignore-packages', |
|
57 metavar='ignore_packages', |
|
58 dest='ignore_packages', |
|
59 default=None, |
|
60 help='list of comma separated keys for package to ignore') |
|
61 |
|
62 |
|
63 def adjust_options(options, args): |
|
64 pass |
|
65 |
|
66 |
|
67 def after_install(options, home_dir): |
|
68 global RES_ENV |
|
69 #global path_locations |
|
70 home_dir, lib_dir, inc_dir, bin_dir = path_locations(home_dir) |
|
71 base_dir = os.path.dirname(home_dir) |
|
72 src_dir = os.path.join(home_dir, 'src') |
|
73 tmp_dir = os.path.join(home_dir, 'tmp') |
|
74 ensure_dir(src_dir) |
|
75 ensure_dir(tmp_dir) |
|
76 system_str = platform.system() |
|
77 |
|
78 res_source_key = options.type_install |
|
79 |
|
80 ignore_packages = [] |
|
81 |
|
82 if options.ignore_packages : |
|
83 ignore_packages = options.ignore_packages.split(",") |
|
84 |
|
85 logger.indent += 2 |
|
86 try: |
|
87 |
|
88 if 'PYLUCENE' not in ignore_packages and system_str != "Windows": |
|
89 #get pylucene |
|
90 logger.notify("Get Pylucene from %s " % RES_ENV.URLS['PYLUCENE'][res_source_key]) |
|
91 pylucene_src = os.path.join(src_dir,"pylucene.tar.gz") |
|
92 if res_source_key == 'local': |
|
93 shutil.copy(RES_ENV.URLS['PYLUCENE'][res_source_key], pylucene_src) |
|
94 else: |
|
95 urllib.urlretrieve(RES_ENV.URLS['PYLUCENE'][res_source_key], pylucene_src) |
|
96 tf = tarfile.open(pylucene_src,'r:gz') |
|
97 pylucene_base_path = os.path.join(src_dir,"pylucene") |
|
98 logger.notify("Extract Pylucene to %s " % pylucene_base_path) |
|
99 tf.extractall(pylucene_base_path) |
|
100 tf.close() |
|
101 |
|
102 pylucene_src_path = os.path.join(pylucene_base_path, os.listdir(pylucene_base_path)[0]) |
|
103 jcc_src_path = os.path.abspath(os.path.join(pylucene_src_path,"jcc")) |
|
104 |
|
105 #install jcc |
|
106 |
|
107 #patch for linux |
|
108 if system_str == 'Linux' : |
|
109 olddir = os.getcwd() |
|
110 patch_dest_path = os.path.join(lib_dir,'site-packages','setuptools-0.6c11-py'+'%s.%s' % (sys.version_info[0], sys.version_info[1])+'.egg') |
|
111 if os.path.isfile(patch_dest_path): |
|
112 # must unzip egg |
|
113 # rename file and etract all |
|
114 shutil.move(patch_dest_path, patch_dest_path + ".zip") |
|
115 zf = zipfile.ZipFile(patch_dest_path + ".zip",'r') |
|
116 zf.extractall(patch_dest_path) |
|
117 os.remove(patch_dest_path + ".zip") |
|
118 logger.notify("Patch jcc : %s " % (patch_dest_path)) |
|
119 os.chdir(patch_dest_path) |
|
120 p = patch.fromfile(os.path.join(jcc_src_path,"jcc","patches","patch.43.0.6c11")) |
|
121 p.apply() |
|
122 os.chdir(olddir) |
|
123 |
|
124 logger.notify("Install jcc") |
|
125 call_subprocess([os.path.abspath(os.path.join(home_dir, 'bin', 'python')), 'setup.py', 'install'], |
|
126 cwd=jcc_src_path, |
|
127 filter_stdout=filter_python_develop, |
|
128 show_stdout=True) |
|
129 #install pylucene |
|
130 |
|
131 logger.notify("Install pylucene") |
|
132 #modify makefile |
|
133 makefile_path = os.path.join(pylucene_src_path,"Makefile") |
|
134 logger.notify("Modify makefile %s " % makefile_path) |
|
135 shutil.move( makefile_path, makefile_path+"~" ) |
|
136 |
|
137 destination= open( makefile_path, "w" ) |
|
138 source= open( makefile_path+"~", "r" ) |
|
139 destination.write("PREFIX_PYTHON="+os.path.abspath(home_dir)+"\\n") |
|
140 destination.write("ANT=ant\\n") |
|
141 destination.write("PYTHON=$(PREFIX_PYTHON)/bin/python\\n") |
|
142 |
|
143 if system_str == "Darwin": |
|
144 if sys.version_info >= (2,6): |
|
145 destination.write("JCC=$(PYTHON) -m jcc.__main__ --shared --arch x86_64 --arch i386\\n") |
|
146 else: |
|
147 destination.write("JCC=$(PYTHON) -m jcc --shared --arch x86_64 --arch i386\\n") |
|
148 destination.write("NUM_FILES=2\\n") |
|
149 elif system_str == "Windows": |
|
150 destination.write("JCC=$(PYTHON) -m jcc.__main__ --shared --arch x86_64 --arch i386\\n") |
|
151 destination.write("NUM_FILES=2\\n") |
|
152 else: |
|
153 if sys.version_info >= (2,6) and sys.version_info <= (2,7): |
|
154 destination.write("JCC=$(PYTHON) -m jcc.__main__ --shared\\n") |
|
155 else: |
|
156 destination.write("JCC=$(PYTHON) -m jcc --shared\\n") |
|
157 destination.write("NUM_FILES=2\\n") |
|
158 for line in source: |
|
159 destination.write( line ) |
|
160 source.close() |
|
161 destination.close() |
|
162 os.remove(makefile_path+"~" ) |
|
163 |
|
164 logger.notify("pylucene make") |
|
165 call_subprocess(['make'], |
|
166 cwd=os.path.abspath(pylucene_src_path), |
|
167 filter_stdout=filter_python_develop, |
|
168 show_stdout=True) |
|
169 |
|
170 logger.notify("pylucene make install") |
|
171 call_subprocess(['make', 'install'], |
|
172 cwd=os.path.abspath(pylucene_src_path), |
|
173 filter_stdout=filter_python_develop, |
|
174 show_stdout=True) |
|
175 |
|
176 |
|
177 if 'PSYCOPG2' not in ignore_packages and system_str == "Windows": |
|
178 #get psycopg2 |
|
179 psycopg2_src = os.path.join(src_dir,"psycopg2.zip") |
|
180 shutil.copy(RES_ENV.URLS['PSYCOPG2'][res_source_key], psycopg2_src) |
|
181 #extract psycopg2 |
|
182 zf = zipfile.ZipFile(psycopg2_src) |
|
183 psycopg2_base_path = os.path.join(src_dir,"psycopg2") |
|
184 zf.extractall(psycopg2_base_path) |
|
185 zf.close() |
|
186 |
|
187 psycopg2_src_path = os.path.join(psycopg2_base_path, os.listdir(psycopg2_base_path)[0]) |
|
188 shutil.copytree(os.path.join(psycopg2_src_path, 'psycopg2'), os.path.abspath(os.path.join(home_dir, 'Lib', 'psycopg2'))) |
|
189 shutil.copy(os.path.join(psycopg2_src_path, 'psycopg2-2.0.10-py2.6.egg-info'), os.path.abspath(os.path.join(home_dir, 'Lib', 'site-packages'))) |
|
190 |
|
191 for key, method, option_str, extra_env in RES_ENV.NORMAL_INSTALL: |
|
192 if key not in ignore_packages: |
|
193 normal_install(key, method, option_str, extra_env, res_source_key, home_dir, tmp_dir) |
|
194 |
|
195 logger.notify("Clear source dir") |
|
196 shutil.rmtree(src_dir) |
|
197 |
|
198 finally: |
|
199 logger.indent -= 2 |
|
200 script_dir = join(base_dir, bin_dir) |
|
201 logger.notify('Run "%s Package" to install new packages that provide builds' |
|
202 % join(script_dir, 'easy_install')) |
|
203 |
|
204 |
|
205 def normal_install(key, method, option_str, extra_env, res_source_key, home_dir, tmp_dir): |
|
206 global logger |
|
207 logger.notify("Install %s from %s with %s" % (key,RES_ENV.URLS[key][res_source_key],method)) |
|
208 if method == 'pip': |
|
209 if sys.platform == 'win32': |
|
210 args = [os.path.abspath(os.path.join(home_dir, 'Scripts', 'pip')), 'install', '-E', os.path.abspath(home_dir), RES_ENV.URLS[key][res_source_key]] |
|
211 else: |
|
212 args = [os.path.abspath(os.path.join(home_dir, 'bin', 'pip')), 'install', '-E', os.path.abspath(home_dir), RES_ENV.URLS[key][res_source_key]] |
|
213 if option_str : |
|
214 args.insert(4,option_str) |
|
215 call_subprocess(args, |
|
216 cwd=os.path.abspath(tmp_dir), |
|
217 filter_stdout=filter_python_develop, |
|
218 show_stdout=True, |
|
219 extra_env=extra_env) |
|
220 else: |
|
221 if sys.platform == 'win32': |
|
222 args = [os.path.abspath(os.path.join(home_dir, 'Scripts', 'easy_install')), RES_ENV.URLS[key][res_source_key]] |
|
223 else: |
|
224 args = [os.path.abspath(os.path.join(home_dir, 'bin', 'easy_install')), RES_ENV.URLS[key][res_source_key]] |
|
225 if option_str : |
|
226 args.insert(1,option_str) |
|
227 call_subprocess(args, |
|
228 cwd=os.path.abspath(tmp_dir), |
|
229 filter_stdout=filter_python_develop, |
|
230 show_stdout=True, |
|
231 extra_env=extra_env) |
|
232 |
|
233 |
|
234 def ensure_dir(dir): |
|
235 global logger |
|
236 if not os.path.exists(dir): |
|
237 logger.notify('Creating directory %s' % dir) |
|
238 os.makedirs(dir) |
|
239 |
|
240 def filter_python_develop(line): |
|
241 global logger |
|
242 if not line.strip(): |
|
243 return Logger.DEBUG |
|
244 for prefix in ['Searching for', 'Reading ', 'Best match: ', 'Processing ', |
|
245 'Moving ', 'Adding ', 'running ', 'writing ', 'Creating ', |
|
246 'creating ', 'Copying ']: |
|
247 if line.startswith(prefix): |
|
248 return Logger.DEBUG |
|
249 return Logger.NOTIFY |