2 """Bootstrap setuptools installation
4 If you want to use setuptools in your package's setup.py, just include this
5 file in the same directory with it, and add this to the top of your setup.py::
7 from ez_setup import use_setuptools
10 If you want to require a specific version of setuptools, set a download
11 mirror, or use an alternate download directory, you can do so by supplying
12 the appropriate options to ``use_setuptools()``.
14 This file can also be run as a script to install or upgrade setuptools.
25 from distutils import log
28 from site import USER_SITE
32 DEFAULT_VERSION = "1.4.2"
33 DEFAULT_URL = "https://pypi.python.org/packages/source/s/setuptools/"
35 def _python_cmd(*args):
36 args = (sys.executable,) + args
37 return subprocess.call(args) == 0
39 def _check_call_py24(cmd, *args, **kwargs):
40 res = subprocess.call(cmd, *args, **kwargs)
41 class CalledProcessError(Exception):
44 msg = "Command '%s' return non-zero exit status %d" % (cmd, res)
45 raise CalledProcessError(msg)
46 vars(subprocess).setdefault('check_call', _check_call_py24)
48 def _install(tarball, install_args=()):
49 # extracting the tarball
50 tmpdir = tempfile.mkdtemp()
51 log.warn('Extracting in %s', tmpdir)
55 tar = tarfile.open(tarball)
59 # going in the directory
60 subdir = os.path.join(tmpdir, os.listdir(tmpdir)[0])
62 log.warn('Now working in %s', subdir)
65 log.warn('Installing Setuptools')
66 if not _python_cmd('setup.py', 'install', *install_args):
67 log.warn('Something went wrong during the installation.')
68 log.warn('See the error message above.')
76 def _build_egg(egg, tarball, to_dir):
77 # extracting the tarball
78 tmpdir = tempfile.mkdtemp()
79 log.warn('Extracting in %s', tmpdir)
83 tar = tarfile.open(tarball)
87 # going in the directory
88 subdir = os.path.join(tmpdir, os.listdir(tmpdir)[0])
90 log.warn('Now working in %s', subdir)
93 log.warn('Building a Setuptools egg in %s', to_dir)
94 _python_cmd('setup.py', '-q', 'bdist_egg', '--dist-dir', to_dir)
99 # returning the result
101 if not os.path.exists(egg):
102 raise IOError('Could not build the egg.')
105 def _do_download(version, download_base, to_dir, download_delay):
106 egg = os.path.join(to_dir, 'setuptools-%s-py%d.%d.egg'
107 % (version, sys.version_info[0], sys.version_info[1]))
108 if not os.path.exists(egg):
109 tarball = download_setuptools(version, download_base,
110 to_dir, download_delay)
111 _build_egg(egg, tarball, to_dir)
112 sys.path.insert(0, egg)
114 # Remove previously-imported pkg_resources if present (see
115 # https://bitbucket.org/pypa/setuptools/pull-request/7/ for details).
116 if 'pkg_resources' in sys.modules:
117 del sys.modules['pkg_resources']
120 setuptools.bootstrap_install_from = egg
123 def use_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL,
124 to_dir=os.curdir, download_delay=15):
125 # making sure we use the absolute path
126 to_dir = os.path.abspath(to_dir)
127 was_imported = 'pkg_resources' in sys.modules or \
128 'setuptools' in sys.modules
132 return _do_download(version, download_base, to_dir, download_delay)
134 pkg_resources.require("setuptools>=" + version)
136 except pkg_resources.VersionConflict:
137 e = sys.exc_info()[1]
140 "The required version of setuptools (>=%s) is not available,\n"
141 "and can't be installed while this script is running. Please\n"
142 "install a more recent version first, using\n"
143 "'easy_install -U setuptools'."
144 "\n\n(Currently using %r)\n" % (version, e.args[0]))
147 del pkg_resources, sys.modules['pkg_resources'] # reload ok
148 return _do_download(version, download_base, to_dir,
150 except pkg_resources.DistributionNotFound:
151 return _do_download(version, download_base, to_dir,
154 def _clean_check(cmd, target):
156 Run the command to download target. If the command fails, clean up before
157 re-raising the error.
160 subprocess.check_call(cmd)
161 except subprocess.CalledProcessError:
162 if os.access(target, os.F_OK):
166 def download_file_powershell(url, target):
168 Download the file at url to target using Powershell (which will validate
169 trust). Raise an exception if the command cannot complete.
171 target = os.path.abspath(target)
175 "(new-object System.Net.WebClient).DownloadFile(%(url)r, %(target)r)" % vars(),
177 _clean_check(cmd, target)
179 def has_powershell():
180 if platform.system() != 'Windows':
182 cmd = ['powershell', '-Command', 'echo test']
183 devnull = open(os.path.devnull, 'wb')
186 subprocess.check_call(cmd, stdout=devnull, stderr=devnull)
193 download_file_powershell.viable = has_powershell
195 def download_file_curl(url, target):
196 cmd = ['curl', url, '--silent', '--output', target]
197 _clean_check(cmd, target)
200 cmd = ['curl', '--version']
201 devnull = open(os.path.devnull, 'wb')
204 subprocess.check_call(cmd, stdout=devnull, stderr=devnull)
211 download_file_curl.viable = has_curl
213 def download_file_wget(url, target):
214 cmd = ['wget', url, '--quiet', '--output-document', target]
215 _clean_check(cmd, target)
218 cmd = ['wget', '--version']
219 devnull = open(os.path.devnull, 'wb')
222 subprocess.check_call(cmd, stdout=devnull, stderr=devnull)
229 download_file_wget.viable = has_wget
231 def download_file_insecure(url, target):
233 Use Python to download the file, even though it cannot authenticate the
237 from urllib.request import urlopen
239 from urllib2 import urlopen
243 # Read/write all in one block, so we don't create a corrupt file
244 # if the download is interrupted.
246 dst = open(target, "wb")
254 download_file_insecure.viable = lambda: True
256 def get_best_downloader():
258 download_file_powershell,
261 download_file_insecure,
264 for dl in downloaders:
268 def download_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL,
269 to_dir=os.curdir, delay=15,
270 downloader_factory=get_best_downloader):
271 """Download setuptools from a specified location and return its filename
273 `version` should be a valid setuptools version number that is available
274 as an egg for download under the `download_base` URL (which should end
275 with a '/'). `to_dir` is the directory where the egg will be downloaded.
276 `delay` is the number of seconds to pause before an actual download
279 ``downloader_factory`` should be a function taking no arguments and
280 returning a function for downloading a URL to a target.
282 # making sure we use the absolute path
283 to_dir = os.path.abspath(to_dir)
284 tgz_name = "setuptools-%s.tar.gz" % version
285 url = download_base + tgz_name
286 saveto = os.path.join(to_dir, tgz_name)
287 if not os.path.exists(saveto): # Avoid repeated downloads
288 log.warn("Downloading %s", url)
289 downloader = downloader_factory()
290 downloader(url, saveto)
291 return os.path.realpath(saveto)
294 def _extractall(self, path=".", members=None):
295 """Extract all members from the archive to the current working
296 directory and set owner, modification time and permissions on
297 directories afterwards. `path' specifies a different directory
298 to extract to. `members' is optional and must be a subset of the
299 list returned by getmembers().
303 from tarfile import ExtractError
309 for tarinfo in members:
311 # Extract directories with a safe mode.
312 directories.append(tarinfo)
313 tarinfo = copy.copy(tarinfo)
314 tarinfo.mode = 448 # decimal for oct 0700
315 self.extract(tarinfo, path)
317 # Reverse sort directories.
318 if sys.version_info < (2, 4):
319 def sorter(dir1, dir2):
320 return cmp(dir1.name, dir2.name)
321 directories.sort(sorter)
322 directories.reverse()
324 directories.sort(key=operator.attrgetter('name'), reverse=True)
326 # Set correct owner, mtime and filemode on directories.
327 for tarinfo in directories:
328 dirpath = os.path.join(path, tarinfo.name)
330 self.chown(tarinfo, dirpath)
331 self.utime(tarinfo, dirpath)
332 self.chmod(tarinfo, dirpath)
334 e = sys.exc_info()[1]
335 if self.errorlevel > 1:
338 self._dbg(1, "tarfile: %s" % e)
341 def _build_install_args(options):
343 Build the arguments to 'python setup.py install' on the setuptools package
346 if options.user_install:
347 if sys.version_info < (2, 6):
348 log.warn("--user requires Python 2.6 or later")
350 install_args.append('--user')
355 Parse the command line for options
357 parser = optparse.OptionParser()
359 '--user', dest='user_install', action='store_true', default=False,
360 help='install in user site package (requires Python 2.6 or later)')
362 '--download-base', dest='download_base', metavar="URL",
364 help='alternative URL from where to download the setuptools package')
366 '--insecure', dest='downloader_factory', action='store_const',
367 const=lambda: download_file_insecure, default=get_best_downloader,
368 help='Use internal, non-validating downloader'
370 options, args = parser.parse_args()
371 # positional arguments are ignored
374 def main(version=DEFAULT_VERSION):
375 """Install or upgrade setuptools and EasyInstall"""
376 options = _parse_args()
377 tarball = download_setuptools(download_base=options.download_base,
378 downloader_factory=options.downloader_factory)
379 return _install(tarball, _build_install_args(options))
381 if __name__ == '__main__':