annotate distribute_setup.py @ 654:5c5cf5bca240

plugin XEP-0085: improvement for sending "composing" state
author souliane <souliane@mailoo.org>
date Thu, 26 Sep 2013 10:44:57 +0200
parents 6a718ede8be1
children efd92a645220
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
231
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!python
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
2 """Bootstrap distribute installation
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
3
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
4 If you want to use setuptools in your package's setup.py, just include this
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
5 file in the same directory with it, and add this to the top of your setup.py::
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
6
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
7 from distribute_setup import use_setuptools
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
8 use_setuptools()
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
9
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
10 If you want to require a specific version of setuptools, set a download
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
11 mirror, or use an alternate download directory, you can do so by supplying
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
12 the appropriate options to ``use_setuptools()``.
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
13
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
14 This file can also be run as a script to install or upgrade setuptools.
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
15 """
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
16 import os
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
17 import sys
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
18 import time
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
19 import fnmatch
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
20 import tempfile
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
21 import tarfile
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from distutils import log
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
23
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
24 try:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from site import USER_SITE
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
26 except ImportError:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
27 USER_SITE = None
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
28
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
29 try:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
30 import subprocess
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
31
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
32 def _python_cmd(*args):
586
6a718ede8be1 Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 231
diff changeset
33 args = (sys.executable, ) + args
231
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
34 return subprocess.call(args) == 0
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
35
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
36 except ImportError:
586
6a718ede8be1 Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 231
diff changeset
37
231
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
38 # will be used for python 2.3
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
39 def _python_cmd(*args):
586
6a718ede8be1 Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 231
diff changeset
40 args = (sys.executable, ) + args
231
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
41 # quoting arguments if windows
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
42 if sys.platform == 'win32':
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
43 def quote(arg):
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
44 if ' ' in arg:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
45 return '"%s"' % arg
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
46 return arg
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
47 args = [quote(arg) for arg in args]
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
48 return os.spawnl(os.P_WAIT, sys.executable, *args) == 0
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
49
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
50 DEFAULT_VERSION = "0.6.14"
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
51 DEFAULT_URL = "http://pypi.python.org/packages/source/d/distribute/"
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
52 SETUPTOOLS_FAKED_VERSION = "0.6c11"
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
53
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
54 SETUPTOOLS_PKG_INFO = """\
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
55 Metadata-Version: 1.0
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
56 Name: setuptools
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
57 Version: %s
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
58 Summary: xxxx
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
59 Home-page: xxx
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
60 Author: xxx
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
61 Author-email: xxx
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
62 License: xxx
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
63 Description: xxx
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
64 """ % SETUPTOOLS_FAKED_VERSION
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
65
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
66
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
67 def _install(tarball):
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
68 # extracting the tarball
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
69 tmpdir = tempfile.mkdtemp()
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
70 log.warn('Extracting in %s', tmpdir)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
71 old_wd = os.getcwd()
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
72 try:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
73 os.chdir(tmpdir)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
74 tar = tarfile.open(tarball)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
75 _extractall(tar)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
76 tar.close()
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
77
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
78 # going in the directory
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
79 subdir = os.path.join(tmpdir, os.listdir(tmpdir)[0])
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
80 os.chdir(subdir)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
81 log.warn('Now working in %s', subdir)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
82
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
83 # installing
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
84 log.warn('Installing Distribute')
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
85 if not _python_cmd('setup.py', 'install'):
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
86 log.warn('Something went wrong during the installation.')
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
87 log.warn('See the error message above.')
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
88 finally:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
89 os.chdir(old_wd)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
90
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
91
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
92 def _build_egg(egg, tarball, to_dir):
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
93 # extracting the tarball
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
94 tmpdir = tempfile.mkdtemp()
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
95 log.warn('Extracting in %s', tmpdir)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
96 old_wd = os.getcwd()
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
97 try:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
98 os.chdir(tmpdir)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
99 tar = tarfile.open(tarball)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
100 _extractall(tar)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
101 tar.close()
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
102
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
103 # going in the directory
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
104 subdir = os.path.join(tmpdir, os.listdir(tmpdir)[0])
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
105 os.chdir(subdir)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
106 log.warn('Now working in %s', subdir)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
107
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
108 # building an egg
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
109 log.warn('Building a Distribute egg in %s', to_dir)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
110 _python_cmd('setup.py', '-q', 'bdist_egg', '--dist-dir', to_dir)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
111
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
112 finally:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
113 os.chdir(old_wd)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
114 # returning the result
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
115 log.warn(egg)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
116 if not os.path.exists(egg):
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
117 raise IOError('Could not build the egg.')
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
118
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
119
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
120 def _do_download(version, download_base, to_dir, download_delay):
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
121 egg = os.path.join(to_dir, 'distribute-%s-py%d.%d.egg'
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
122 % (version, sys.version_info[0], sys.version_info[1]))
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
123 if not os.path.exists(egg):
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
124 tarball = download_setuptools(version, download_base,
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
125 to_dir, download_delay)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
126 _build_egg(egg, tarball, to_dir)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
127 sys.path.insert(0, egg)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
128 import setuptools
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
129 setuptools.bootstrap_install_from = egg
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
130
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
131
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
132 def use_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL,
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
133 to_dir=os.curdir, download_delay=15, no_fake=True):
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
134 # making sure we use the absolute path
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
135 to_dir = os.path.abspath(to_dir)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
136 was_imported = 'pkg_resources' in sys.modules or \
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
137 'setuptools' in sys.modules
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
138 try:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
139 try:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
140 import pkg_resources
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
141 if not hasattr(pkg_resources, '_distribute'):
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
142 if not no_fake:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
143 _fake_setuptools()
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
144 raise ImportError
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
145 except ImportError:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
146 return _do_download(version, download_base, to_dir, download_delay)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
147 try:
586
6a718ede8be1 Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 231
diff changeset
148 pkg_resources.require("distribute>=" + version)
231
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
149 return
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
150 except pkg_resources.VersionConflict:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
151 e = sys.exc_info()[1]
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
152 if was_imported:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
153 sys.stderr.write(
586
6a718ede8be1 Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 231
diff changeset
154 "The required version of distribute (>=%s) is not available,\n"
6a718ede8be1 Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 231
diff changeset
155 "and can't be installed while this script is running. Please\n"
6a718ede8be1 Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 231
diff changeset
156 "install a more recent version first, using\n"
6a718ede8be1 Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 231
diff changeset
157 "'easy_install -U distribute'."
6a718ede8be1 Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 231
diff changeset
158 "\n\n(Currently using %r)\n" % (version, e.args[0]))
231
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
159 sys.exit(2)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
160 else:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
161 del pkg_resources, sys.modules['pkg_resources'] # reload ok
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
162 return _do_download(version, download_base, to_dir,
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
163 download_delay)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
164 except pkg_resources.DistributionNotFound:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
165 return _do_download(version, download_base, to_dir,
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
166 download_delay)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
167 finally:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
168 if not no_fake:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
169 _create_fake_setuptools_pkg_info(to_dir)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
170
586
6a718ede8be1 Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 231
diff changeset
171
231
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
172 def download_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL,
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
173 to_dir=os.curdir, delay=15):
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
174 """Download distribute from a specified location and return its filename
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
175
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
176 `version` should be a valid distribute version number that is available
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
177 as an egg for download under the `download_base` URL (which should end
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
178 with a '/'). `to_dir` is the directory where the egg will be downloaded.
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
179 `delay` is the number of seconds to pause before an actual download
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
180 attempt.
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
181 """
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
182 # making sure we use the absolute path
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
183 to_dir = os.path.abspath(to_dir)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
184 try:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
185 from urllib.request import urlopen
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
186 except ImportError:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
187 from urllib2 import urlopen
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
188 tgz_name = "distribute-%s.tar.gz" % version
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
189 url = download_base + tgz_name
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
190 saveto = os.path.join(to_dir, tgz_name)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
191 src = dst = None
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
192 if not os.path.exists(saveto): # Avoid repeated downloads
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
193 try:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
194 log.warn("Downloading %s", url)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
195 src = urlopen(url)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
196 # Read/write all in one block, so we don't create a corrupt file
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
197 # if the download is interrupted.
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
198 data = src.read()
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
199 dst = open(saveto, "wb")
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
200 dst.write(data)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
201 finally:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
202 if src:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
203 src.close()
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
204 if dst:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
205 dst.close()
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
206 return os.path.realpath(saveto)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
207
586
6a718ede8be1 Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 231
diff changeset
208
231
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
209 def _no_sandbox(function):
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
210 def __no_sandbox(*args, **kw):
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
211 try:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
212 from setuptools.sandbox import DirectorySandbox
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
213 if not hasattr(DirectorySandbox, '_old'):
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
214 def violation(*args):
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
215 pass
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
216 DirectorySandbox._old = DirectorySandbox._violation
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
217 DirectorySandbox._violation = violation
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
218 patched = True
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
219 else:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
220 patched = False
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
221 except ImportError:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
222 patched = False
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
223
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
224 try:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
225 return function(*args, **kw)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
226 finally:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
227 if patched:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
228 DirectorySandbox._violation = DirectorySandbox._old
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
229 del DirectorySandbox._old
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
230
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
231 return __no_sandbox
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
232
586
6a718ede8be1 Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 231
diff changeset
233
231
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
234 def _patch_file(path, content):
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
235 """Will backup the file then patch it"""
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
236 existing_content = open(path).read()
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
237 if existing_content == content:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
238 # already patched
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
239 log.warn('Already patched.')
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
240 return False
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
241 log.warn('Patching...')
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
242 _rename_path(path)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
243 f = open(path, 'w')
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
244 try:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
245 f.write(content)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
246 finally:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
247 f.close()
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
248 return True
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
249
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
250 _patch_file = _no_sandbox(_patch_file)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
251
586
6a718ede8be1 Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 231
diff changeset
252
231
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
253 def _same_content(path, content):
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
254 return open(path).read() == content
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
255
586
6a718ede8be1 Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 231
diff changeset
256
231
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
257 def _rename_path(path):
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
258 new_name = path + '.OLD.%s' % time.time()
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
259 log.warn('Renaming %s into %s', path, new_name)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
260 os.rename(path, new_name)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
261 return new_name
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
262
586
6a718ede8be1 Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 231
diff changeset
263
231
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
264 def _remove_flat_installation(placeholder):
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
265 if not os.path.isdir(placeholder):
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
266 log.warn('Unkown installation at %s', placeholder)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
267 return False
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
268 found = False
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
269 for file in os.listdir(placeholder):
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
270 if fnmatch.fnmatch(file, 'setuptools*.egg-info'):
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
271 found = True
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
272 break
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
273 if not found:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
274 log.warn('Could not locate setuptools*.egg-info')
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
275 return
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
276
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
277 log.warn('Removing elements out of the way...')
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
278 pkg_info = os.path.join(placeholder, file)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
279 if os.path.isdir(pkg_info):
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
280 patched = _patch_egg_dir(pkg_info)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
281 else:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
282 patched = _patch_file(pkg_info, SETUPTOOLS_PKG_INFO)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
283
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
284 if not patched:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
285 log.warn('%s already patched.', pkg_info)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
286 return False
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
287 # now let's move the files out of the way
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
288 for element in ('setuptools', 'pkg_resources.py', 'site.py'):
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
289 element = os.path.join(placeholder, element)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
290 if os.path.exists(element):
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
291 _rename_path(element)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
292 else:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
293 log.warn('Could not find the %s element of the '
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
294 'Setuptools distribution', element)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
295 return True
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
296
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
297 _remove_flat_installation = _no_sandbox(_remove_flat_installation)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
298
586
6a718ede8be1 Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 231
diff changeset
299
231
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
300 def _after_install(dist):
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
301 log.warn('After install bootstrap.')
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
302 placeholder = dist.get_command_obj('install').install_purelib
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
303 _create_fake_setuptools_pkg_info(placeholder)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
304
586
6a718ede8be1 Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 231
diff changeset
305
231
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
306 def _create_fake_setuptools_pkg_info(placeholder):
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
307 if not placeholder or not os.path.exists(placeholder):
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
308 log.warn('Could not find the install location')
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
309 return
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
310 pyver = '%s.%s' % (sys.version_info[0], sys.version_info[1])
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
311 setuptools_file = 'setuptools-%s-py%s.egg-info' % \
586
6a718ede8be1 Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 231
diff changeset
312 (SETUPTOOLS_FAKED_VERSION, pyver)
231
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
313 pkg_info = os.path.join(placeholder, setuptools_file)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
314 if os.path.exists(pkg_info):
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
315 log.warn('%s already exists', pkg_info)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
316 return
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
317
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
318 log.warn('Creating %s', pkg_info)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
319 f = open(pkg_info, 'w')
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
320 try:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
321 f.write(SETUPTOOLS_PKG_INFO)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
322 finally:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
323 f.close()
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
324
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
325 pth_file = os.path.join(placeholder, 'setuptools.pth')
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
326 log.warn('Creating %s', pth_file)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
327 f = open(pth_file, 'w')
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
328 try:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
329 f.write(os.path.join(os.curdir, setuptools_file))
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
330 finally:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
331 f.close()
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
332
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
333 _create_fake_setuptools_pkg_info = _no_sandbox(_create_fake_setuptools_pkg_info)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
334
586
6a718ede8be1 Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 231
diff changeset
335
231
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
336 def _patch_egg_dir(path):
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
337 # let's check if it's already patched
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
338 pkg_info = os.path.join(path, 'EGG-INFO', 'PKG-INFO')
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
339 if os.path.exists(pkg_info):
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
340 if _same_content(pkg_info, SETUPTOOLS_PKG_INFO):
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
341 log.warn('%s already patched.', pkg_info)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
342 return False
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
343 _rename_path(path)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
344 os.mkdir(path)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
345 os.mkdir(os.path.join(path, 'EGG-INFO'))
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
346 pkg_info = os.path.join(path, 'EGG-INFO', 'PKG-INFO')
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
347 f = open(pkg_info, 'w')
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
348 try:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
349 f.write(SETUPTOOLS_PKG_INFO)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
350 finally:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
351 f.close()
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
352 return True
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
353
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
354 _patch_egg_dir = _no_sandbox(_patch_egg_dir)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
355
586
6a718ede8be1 Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 231
diff changeset
356
231
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
357 def _before_install():
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
358 log.warn('Before install bootstrap.')
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
359 _fake_setuptools()
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
360
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
361
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
362 def _under_prefix(location):
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
363 if 'install' not in sys.argv:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
364 return True
586
6a718ede8be1 Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 231
diff changeset
365 args = sys.argv[sys.argv.index('install') + 1:]
231
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
366 for index, arg in enumerate(args):
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
367 for option in ('--root', '--prefix'):
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
368 if arg.startswith('%s=' % option):
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
369 top_dir = arg.split('root=')[-1]
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
370 return location.startswith(top_dir)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
371 elif arg == option:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
372 if len(args) > index:
586
6a718ede8be1 Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 231
diff changeset
373 top_dir = args[index + 1]
231
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
374 return location.startswith(top_dir)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
375 if arg == '--user' and USER_SITE is not None:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
376 return location.startswith(USER_SITE)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
377 return True
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
378
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
379
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
380 def _fake_setuptools():
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
381 log.warn('Scanning installed packages')
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
382 try:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
383 import pkg_resources
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
384 except ImportError:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
385 # we're cool
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
386 log.warn('Setuptools or Distribute does not seem to be installed.')
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
387 return
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
388 ws = pkg_resources.working_set
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
389 try:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
390 setuptools_dist = ws.find(pkg_resources.Requirement.parse('setuptools',
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
391 replacement=False))
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
392 except TypeError:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
393 # old distribute API
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
394 setuptools_dist = ws.find(pkg_resources.Requirement.parse('setuptools'))
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
395
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
396 if setuptools_dist is None:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
397 log.warn('No setuptools distribution found')
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
398 return
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
399 # detecting if it was already faked
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
400 setuptools_location = setuptools_dist.location
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
401 log.warn('Setuptools installation detected at %s', setuptools_location)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
402
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
403 # if --root or --preix was provided, and if
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
404 # setuptools is not located in them, we don't patch it
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
405 if not _under_prefix(setuptools_location):
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
406 log.warn('Not patching, --root or --prefix is installing Distribute'
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
407 ' in another location')
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
408 return
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
409
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
410 # let's see if its an egg
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
411 if not setuptools_location.endswith('.egg'):
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
412 log.warn('Non-egg installation')
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
413 res = _remove_flat_installation(setuptools_location)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
414 if not res:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
415 return
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
416 else:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
417 log.warn('Egg installation')
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
418 pkg_info = os.path.join(setuptools_location, 'EGG-INFO', 'PKG-INFO')
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
419 if (os.path.exists(pkg_info) and
586
6a718ede8be1 Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 231
diff changeset
420 _same_content(pkg_info, SETUPTOOLS_PKG_INFO)):
231
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
421 log.warn('Already patched.')
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
422 return
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
423 log.warn('Patching...')
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
424 # let's create a fake egg replacing setuptools one
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
425 res = _patch_egg_dir(setuptools_location)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
426 if not res:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
427 return
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
428 log.warn('Patched done.')
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
429 _relaunch()
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
430
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
431
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
432 def _relaunch():
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
433 log.warn('Relaunching...')
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
434 # we have to relaunch the process
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
435 # pip marker to avoid a relaunch bug
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
436 if sys.argv[:3] == ['-c', 'install', '--single-version-externally-managed']:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
437 sys.argv[0] = 'setup.py'
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
438 args = [sys.executable] + sys.argv
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
439 sys.exit(subprocess.call(args))
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
440
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
441
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
442 def _extractall(self, path=".", members=None):
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
443 """Extract all members from the archive to the current working
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
444 directory and set owner, modification time and permissions on
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
445 directories afterwards. `path' specifies a different directory
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
446 to extract to. `members' is optional and must be a subset of the
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
447 list returned by getmembers().
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
448 """
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
449 import copy
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
450 import operator
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
451 from tarfile import ExtractError
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
452 directories = []
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
453
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
454 if members is None:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
455 members = self
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
456
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
457 for tarinfo in members:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
458 if tarinfo.isdir():
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
459 # Extract directories with a safe mode.
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
460 directories.append(tarinfo)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
461 tarinfo = copy.copy(tarinfo)
586
6a718ede8be1 Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 231
diff changeset
462 tarinfo.mode = 448 # decimal for oct 0700
231
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
463 self.extract(tarinfo, path)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
464
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
465 # Reverse sort directories.
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
466 if sys.version_info < (2, 4):
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
467 def sorter(dir1, dir2):
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
468 return cmp(dir1.name, dir2.name)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
469 directories.sort(sorter)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
470 directories.reverse()
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
471 else:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
472 directories.sort(key=operator.attrgetter('name'), reverse=True)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
473
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
474 # Set correct owner, mtime and filemode on directories.
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
475 for tarinfo in directories:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
476 dirpath = os.path.join(path, tarinfo.name)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
477 try:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
478 self.chown(tarinfo, dirpath)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
479 self.utime(tarinfo, dirpath)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
480 self.chmod(tarinfo, dirpath)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
481 except ExtractError:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
482 e = sys.exc_info()[1]
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
483 if self.errorlevel > 1:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
484 raise
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
485 else:
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
486 self._dbg(1, "tarfile: %s" % e)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
487
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
488
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
489 def main(argv, version=DEFAULT_VERSION):
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
490 """Install or upgrade setuptools and EasyInstall"""
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
491 tarball = download_setuptools()
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
492 _install(tarball)
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
493
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
494
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
495 if __name__ == '__main__':
f68a9a429c88 added distribute bootstrap file
Goffi <goffi@goffi.org>
parents:
diff changeset
496 main(sys.argv[1:])