This repository was archived by the owner on Mar 31, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathpythoninstaller.py
More file actions
540 lines (477 loc) · 24.4 KB
/
pythoninstaller.py
File metadata and controls
540 lines (477 loc) · 24.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
import ctypes
import os
import sys
import shutil
import mimetypes
import multiprocessing
import re
import subprocess
from pythonz.installer.versions import versions
from pythonz.util import symlink, makedirs, Package, is_url, Link,\
unlink, is_html, Subprocess, rm_r, is_python26, is_python27,\
extract_downloadfile, is_archive_file, path_to_fileurl, is_file,\
fileurl_to_path, is_python30, is_python31, is_python32,\
get_macosx_deployment_target, Version, is_python25, is_python24, is_python33
from pythonz.define import PATH_BUILD, PATH_DISTS, PATH_PYTHONS, PATH_LOG, \
PATH_PATCHES_ALL, PATH_PATCHES_OSX
from pythonz.downloader import Downloader, DownloadError, sha256
from pythonz.log import logger
class AlreadyInstalledError(Exception): pass
class PythonInstaller(object):
@staticmethod
def get_installer(version, options):
type = options.type.lower()
if type == 'cpython':
return CPythonInstaller(version, options)
elif type == 'stackless':
return StacklessInstaller(version, options)
elif type == 'pypy':
return PyPyInstaller(version, options)
elif type == 'pypy3':
return PyPy3Installer(version, options)
elif type == 'jython':
return JythonInstaller(version, options)
raise RuntimeError('invalid type specified: %s' % type)
class Installer(object):
def __init__(self, version, options):
# create directories
makedirs(PATH_BUILD)
makedirs(PATH_DISTS)
makedirs(PATH_LOG)
if options.file is not None:
if not (is_archive_file(options.file) and os.path.isfile(options.file)):
logger.error('invalid file specified: %s' % options.file)
raise RuntimeError
self.download_url = path_to_fileurl(options.file)
elif options.url is not None:
if not is_url(options.url):
logger.error('invalid URL specified: %s' % options.url)
raise RuntimeError
self.download_url = options.url
else:
self.download_url = self.get_version_url(version)
if version not in self.supported_versions:
logger.warning("Unsupported Python version: `%s`, trying with the following URL anyway: %s" % (version, self.download_url))
self.pkg = Package(version, options.type)
if options.external_path:
if not os.path.isabs(options.external_path):
options.external_path = os.path.join(
os.path.abspath(os.path.curdir),
options.external_path)
self.install_dir = os.path.join(options.external_path,
self.pkg.name)
else:
self.install_dir = os.path.join(PATH_PYTHONS, self.pkg.name)
self.build_dir = os.path.join(PATH_BUILD, self.pkg.name)
filename = Link(self.download_url).filename
self.download_file = os.path.join(PATH_DISTS, filename)
# cleanup
if os.path.isdir(self.build_dir):
shutil.rmtree(self.build_dir)
if os.path.isdir(self.install_dir):
if options.reinstall:
shutil.rmtree(self.install_dir)
else:
raise AlreadyInstalledError("You have already installed `%s`" % self.pkg.name)
self.options = options
self.logfile = os.path.join(PATH_LOG, 'build.log')
self.patches = []
self.configure_options = []
@classmethod
def get_version_url(cls, version):
raise NotImplementedError
@property
def expected_sha256(self):
return self.supported_versions.get(self.pkg.version)
def download(self):
if os.path.isfile(self.download_file) and sha256(self.download_file) == self.expected_sha256:
logger.info("Use the previously fetched %s" % (self.download_file))
else:
base_url = Link(self.download_url).base_url
logger.info("Downloading %s as %s" % (base_url, self.download_file))
try:
Downloader.fetch(self.download_url, self.download_file, self.expected_sha256)
except DownloadError:
logger.error("Failed to download.\n%s" % (sys.exc_info()[1]))
sys.exit(1)
def install(self):
raise NotImplementedError
class CPythonInstaller(Installer):
version_re = re.compile(r'(\d\.\d\d?(\.\d+)?)(.*)')
supported_versions = versions['cpython']
def __init__(self, version, options):
super(CPythonInstaller, self).__init__(version, options)
version = Version(self.pkg.version)
if version >= '3.1':
self.configure_options.append('--with-computed-gotos')
# fix for #109
if version < '2.7':
self.configure_options.append('SVNVERSION="Unversioned directory"')
# fix for #127
if sys.platform.startswith('linux'):
if version >= '2.5' and version < '3.0':
self.configure_options.append('--enable-unicode=ucs4')
elif version >= '3.0' and version < '3.3':
self.configure_options.append('--with-wide-unicode')
if sys.platform == "darwin":
# set configure options
target = get_macosx_deployment_target()
if target:
self.configure_options.append('MACOSX_DEPLOYMENT_TARGET=%s' % target)
# set build options
if options.framework and options.shared:
logger.error("Can't specify both framework and shared.")
raise Exception
if options.framework:
self.configure_options.append('--enable-framework=%s' % os.path.join(self.install_dir, 'Frameworks'))
if options.shared:
self.configure_options.append('--enable-shared')
if options.universal:
self.configure_options.append('--enable-universalsdk=/')
self.configure_options.append('--with-universal-archs=intel')
else:
if options.shared:
self.configure_options.append('--enable-shared')
@classmethod
def get_version_url(cls, version):
if version not in cls.supported_versions:
# Unsupported alpha, beta or rc versions
match = cls.version_re.match(version)
if match is not None:
groups = match.groups()
base_version = groups[0]
version = groups[0] + groups[2]
return 'http://www.python.org/ftp/python/%(base_version)s/Python-%(version)s.tgz' % {'base_version': base_version, 'version': version}
return 'http://www.python.org/ftp/python/%(version)s/Python-%(version)s.tgz' % {'version': version}
def _apply_patches(self):
try:
s = Subprocess(log=self.logfile, cwd=self.build_dir, verbose=self.options.verbose)
for patch in self.patches:
if type(patch) is dict:
for ed, source in patch.items():
s.shell('ed - %s < %s' % (source, ed))
else:
s.shell("patch -p0 < %s" % patch)
except:
logger.error("Failed to patch `%s`.\n%s" % (self.build_dir, sys.exc_info()[1]))
sys.exit(1)
def _append_patch(self, patch_dir, patch_files):
for patch in patch_files:
if type(patch) is dict:
tmp = patch
patch = {}
for key in tmp.keys():
patch[os.path.join(patch_dir, key)] = tmp[key]
self.patches.append(patch)
else:
self.patches.append(os.path.join(patch_dir, patch))
def install(self):
# get content type.
if is_file(self.download_url):
path = fileurl_to_path(self.download_url)
self.content_type = mimetypes.guess_type(path)[0]
else:
headerinfo = Downloader.read_head_info(self.download_url)
self.content_type = headerinfo['content-type']
if is_html(self.content_type):
# note: maybe got 404 or 503 http status code.
logger.error("Invalid content-type: `%s`" % self.content_type)
return
self.download_and_extract()
logger.info("\nThis could take a while. You can run the following command on another shell to track the status:")
logger.info(" tail -f %s\n" % self.logfile)
logger.info("Installing %s into %s" % (self.pkg.name, self.install_dir))
try:
self.patch()
self.configure()
self.make()
self.make_install()
except Exception:
import traceback
traceback.print_exc()
rm_r(self.install_dir)
logger.error("Failed to install %s. Check %s to see why." % (self.pkg.name, self.logfile))
sys.exit(1)
self.symlink()
logger.info("\nInstalled %(pkgname)s successfully." % {"pkgname": self.pkg.name})
def download_and_extract(self):
self.download()
if not extract_downloadfile(self.content_type, self.download_file, self.build_dir):
sys.exit(1)
def _patch(self):
version = Version(self.pkg.version)
common_patch_dir = os.path.join(PATH_PATCHES_ALL, "common")
if is_python24(version):
patch_dir = os.path.join(PATH_PATCHES_ALL, "python24")
self._append_patch(patch_dir, ['patch-setup.py.diff'])
elif is_python25(version):
patch_dir = os.path.join(PATH_PATCHES_ALL, "python25")
self._append_patch(patch_dir, ['patch-setup.py.diff', 'patch-svnversion.patch'])
elif is_python26(version):
self._append_patch(common_patch_dir, ['patch-setup.py.diff'])
patch_dir = os.path.join(PATH_PATCHES_ALL, "python26")
if version < '2.6.5':
self._append_patch(patch_dir, ['patch-nosslv2-1.diff'])
elif version < '2.6.6':
self._append_patch(patch_dir, ['patch-nosslv2-2.diff'])
elif version < '2.6.9':
self._append_patch(patch_dir, ['patch-nosslv2-3.diff'])
else:
self._append_patch(patch_dir, ['patch-nosslv23.diff'])
elif is_python27(version):
if version < '2.7.2':
self._append_patch(common_patch_dir, ['patch-setup.py.diff'])
elif is_python30(version):
patch_dir = os.path.join(PATH_PATCHES_ALL, "python30")
self._append_patch(patch_dir, ['patch-setup.py.diff',
'patch-nosslv2.diff'])
elif is_python31(version):
if version < '3.1.4':
self._append_patch(common_patch_dir, ['patch-setup.py.diff'])
elif is_python32(version):
if version == '3.2':
patch_dir = os.path.join(PATH_PATCHES_ALL, "python32")
self._append_patch(patch_dir, ['patch-setup.py.diff'])
def _patch_osx(self):
version = Version(self.pkg.version)
if is_python24(version):
PATH_PATCHES_OSX_PYTHON24 = os.path.join(PATH_PATCHES_OSX, "python24")
if version == '2.4':
self._append_patch(PATH_PATCHES_OSX_PYTHON24, ['patch240-configure',
'patch240-setup.py.diff',
'patch240-Mac-OSX-Makefile.in',
'patch240-gestaltmodule.c.diff',
'patch240-sysconfig.py.diff'])
elif version < '2.4.4':
self._append_patch(PATH_PATCHES_OSX_PYTHON24, ['patch241-configure',
'patch240-setup.py.diff',
'patch240-Mac-OSX-Makefile.in',
'patch240-gestaltmodule.c.diff'])
else:
self._append_patch(PATH_PATCHES_OSX_PYTHON24, ['patch244-configure',
'patch244-setup.py.diff',
'patch244-Mac-OSX-Makefile.in',
'patch244-gestaltmodule.c.diff'])
self._append_patch(PATH_PATCHES_OSX_PYTHON24, [
'patch-Makefile.pre.in',
'patch-Lib-cgi.py.diff',
'patch-Lib-site.py.diff',
'patch-Include-pyport.h',
'patch-configure-badcflags.diff',
'patch-macosmodule.diff',
'patch-mactoolboxglue.diff',
'patch-pymactoolbox.diff'])
elif is_python25(version):
PATH_PATCHES_OSX_PYTHON25 = os.path.join(PATH_PATCHES_OSX, "python25")
if version == '2.5':
self._append_patch(PATH_PATCHES_OSX_PYTHON25, ['patch250-setup.py.diff'])
elif version == '2.5.1':
self._append_patch(PATH_PATCHES_OSX_PYTHON25, ['patch251-setup.py.diff'])
else:
self._append_patch(PATH_PATCHES_OSX_PYTHON25, ['patch252-setup.py.diff'])
self._append_patch(PATH_PATCHES_OSX_PYTHON25, [
'patch-Makefile.pre.in.diff',
'patch-Lib-cgi.py.diff',
'patch-Lib-distutils-dist.py.diff',
'patch-configure-badcflags.diff',
'patch-configure-arch_only.diff',
'patch-64bit.diff',
'patch-pyconfig.h.in.diff',
'patch-gestaltmodule.c.diff',
{'_localemodule.c.ed': 'Modules/_localemodule.c'},
{'locale.py.ed': 'Lib/locale.py'}])
elif is_python26(version):
PATH_PATCHES_OSX_PYTHON26 = os.path.join(PATH_PATCHES_OSX, "python26")
self._append_patch(PATH_PATCHES_OSX_PYTHON26, [
'patch-Lib-cgi.py.diff',
'patch-Lib-distutils-dist.py.diff',
'patch-Mac-IDLE-Makefile.in.diff',
'patch-Mac-Makefile.in.diff',
'patch-Mac-PythonLauncher-Makefile.in.diff',
'patch-Mac-Tools-Doc-setup.py.diff',
'patch-setup.py-db46.diff',
'patch-Lib-ctypes-macholib-dyld.py.diff',
'patch-setup_no_tkinter.py.diff',
{'_localemodule.c.ed': 'Modules/_localemodule.c'},
{'locale.py.ed': 'Lib/locale.py'}])
if version < '2.6.9':
patch_dir = os.path.join(PATH_PATCHES_ALL, "python26")
self._append_patch(patch_dir, ['patch-nosslv2-3.diff'])
elif is_python27(version):
PATH_PATCHES_OSX_PYTHON27 = os.path.join(PATH_PATCHES_OSX, "python27")
if version < '2.7.4':
self._append_patch(PATH_PATCHES_OSX_PYTHON27, ['patch-Modules-posixmodule.diff'])
elif version == '2.7.6':
self._append_patch(PATH_PATCHES_OSX_PYTHON27, ['python-276-dtrace.diff'])
elif is_python33(version):
PATH_PATCHES_OSX_PYTHON33 = os.path.join(PATH_PATCHES_OSX, "python33")
if version == '3.3.4':
self._append_patch(PATH_PATCHES_OSX_PYTHON33, ['python-334-dtrace.diff'])
def patch(self):
if sys.platform == "darwin":
self._patch_osx()
else:
self._patch()
self._apply_patches()
def configure(self):
s = Subprocess(log=self.logfile, cwd=self.build_dir, verbose=self.options.verbose)
cmd = "./configure --prefix=%s %s %s" % (self.install_dir, self.options.configure, ' '.join(self.configure_options))
if self.options.verbose:
logger.log(cmd)
s.check_call(cmd)
def make(self):
try:
jobs = multiprocessing.cpu_count()
except NotImplementedError:
make = 'make'
else:
make = 'make -j%s' % jobs
s = Subprocess(log=self.logfile, cwd=self.build_dir, verbose=self.options.verbose)
s.check_call(make)
if self.options.run_tests:
if self.options.force:
# note: ignore tests failure error.
s.call("make test")
else:
s.check_call("make test")
def make_install(self):
s = Subprocess(log=self.logfile, cwd=self.build_dir, verbose=self.options.verbose)
s.check_call("make install")
def symlink(self):
install_dir = os.path.realpath(self.install_dir)
bin_dir = os.path.join(install_dir, 'bin')
if self.options.framework:
# create symlink bin -> /path/to/Frameworks/Python.framework/Versions/?.?/bin
if os.path.exists(bin_dir):
rm_r(bin_dir)
m = re.match(r'\d\.\d', self.pkg.version)
if m:
version = m.group(0)
symlink(os.path.join(install_dir, 'Frameworks', 'Python.framework', 'Versions', version, 'bin'), os.path.join(bin_dir))
path_python = os.path.join(bin_dir, 'python')
if not os.path.isfile(path_python):
for f in os.listdir(bin_dir):
if re.match(r'python\d\.\d$', f):
symlink(os.path.join(bin_dir, f), path_python)
break
class StacklessInstaller(CPythonInstaller):
supported_versions = versions['stackless']
def _patch_osx(self):
super(StacklessInstaller, self)._patch_osx()
version = Version(self.pkg.version)
if version in ('3.2.5', '3.3.5'):
PATH_PATCHES_OSX_PYTHON33 = os.path.join(PATH_PATCHES_OSX, "python33")
self._append_patch(PATH_PATCHES_OSX_PYTHON33, ['stackless-335-compile.diff'])
@classmethod
def get_version_url(cls, version):
return 'http://www.stackless.com/binaries/stackless-%(version)s-export.tar.bz2' % {'version': version.replace('.', '')}
class PyPyInstaller(Installer):
supported_versions = versions['pypy']
base_url = 'https://bitbucket.org/pypy/pypy/downloads/'
@classmethod
def get_version_url(cls, version):
if version >= '5.3':
prefix = 'pypy2-v'
else:
prefix = 'pypy-'
if sys.platform == 'darwin':
url = '%s%s-osx64.tar.bz2' % (prefix, version)
else:
# Linux
logger.warning("Linux binaries are dynamically linked, as is usual, and thus might not be usable due to the sad story of linux binary compatibility, check the PyPy website for more information")
arch = {4: '', 8: '64'}[ctypes.sizeof(ctypes.c_size_t)]
url = '%(prefix)s%(version)s-linux%(arch)s.tar.bz2' % {'arch': arch, 'prefix': prefix, 'version': version}
return cls.base_url + url
@property
def expected_sha256(self):
platform_checksums = self.supported_versions.get(self.pkg.version)
if platform_checksums is not None:
if sys.platform == 'darwin':
return platform_checksums['darwin']
elif 'linux' in sys.platform:
arch = {4: '', 8: '64'}[ctypes.sizeof(ctypes.c_size_t)]
return platform_checksums['linux' + arch]
else:
raise ValueError('Unsupported platform: ' + sys.platform)
def install(self):
# get content type.
if is_file(self.download_url):
path = fileurl_to_path(self.download_url)
self.content_type = mimetypes.guess_type(path)[0]
else:
headerinfo = Downloader.read_head_info(self.download_url)
self.content_type = headerinfo['content-type']
if is_html(self.content_type):
# note: maybe got 404 or 503 http status code.
logger.error("Invalid content-type: `%s`" % self.content_type)
return
self.download_and_extract()
logger.info("Installing %s into %s" % (self.pkg.name, self.install_dir))
shutil.copytree(self.build_dir, self.install_dir)
self.symlink()
logger.info("\nInstalled %(pkgname)s successfully." % {"pkgname": self.pkg.name})
def download_and_extract(self):
self.download()
if not extract_downloadfile(self.content_type, self.download_file, self.build_dir):
sys.exit(1)
def symlink(self):
install_dir = os.path.realpath(self.install_dir)
bin_dir = os.path.join(install_dir, 'bin')
symlink(os.path.join(bin_dir, 'pypy'), os.path.join(bin_dir, 'python'))
class PyPy3Installer(PyPyInstaller):
supported_versions = versions['pypy3']
@classmethod
def get_version_url(cls, version):
if sys.platform == 'darwin':
return 'https://bitbucket.org/pypy/pypy/downloads/pypy3-%(version)s-osx64.tar.bz2' % {'version': version}
else:
# Linux
logger.warning("Linux binaries are dynamically linked, as is usual, and thus might not be usable due to the sad story of linux binary compatibility, check the PyPy website for more information")
arch = {4: '', 8: '64'}[ctypes.sizeof(ctypes.c_size_t)]
return 'https://bitbucket.org/pypy/pypy/downloads/pypy3-%(version)s-linux%(arch)s.tar.bz2' % {'arch': arch, 'version': version}
class JythonInstaller(Installer):
supported_versions = versions['jython']
def __init__(self, version, options):
super(JythonInstaller, self).__init__(version, options)
filename = 'jython-installer-%s.jar' % version
self.download_file = os.path.join(PATH_DISTS, filename)
@classmethod
def get_version_url(cls, version):
if version in ('2.5.0', '2.5.1', '2.5.2'):
return 'https://downloads.sourceforge.net/project/jython/jython/%(version)s/jython_installer-%(version)s.jar' % {'version': version}
else:
return 'http://search.maven.org/remotecontent?filepath=org/python/jython-installer/%(version)s/jython-installer-%(version)s.jar' % {'version': version}
def install(self):
# check if java is installed
r = subprocess.call("command -v java > /dev/null", shell=True)
if r != 0:
logger.error("Jython requires Java to be installed, but the 'java' command was not found in the path.")
return
# get content type.
if is_file(self.download_url):
path = fileurl_to_path(self.download_url)
self.content_type = mimetypes.guess_type(path)[0]
else:
try:
headerinfo = Downloader.read_head_info(self.download_url)
except DownloadError:
self.content_type = None
else:
self.content_type = headerinfo['content-type']
if is_html(self.content_type):
# note: maybe got 404 or 503 http status code.
logger.error("Invalid content-type: `%s`" % self.content_type)
return
self.download()
logger.info("\nThis could take a while. You can run the following command on another shell to track the status:")
logger.info(" tail -f %s\n" % self.logfile)
logger.info("Installing %s into %s" % (self.pkg.name, self.install_dir))
cmd = 'java -jar %s -s -d %s' % (self.download_file, self.install_dir)
s = Subprocess(log=self.logfile, verbose=self.options.verbose)
s.check_call(cmd)
self.symlink()
logger.info("\nInstalled %(pkgname)s successfully." % {"pkgname": self.pkg.name})
def symlink(self):
install_dir = os.path.realpath(self.install_dir)
bin_dir = os.path.join(install_dir, 'bin')
symlink(os.path.join(bin_dir, 'jython'), os.path.join(bin_dir, 'python'))