diff options
author | Donald Stufft <donald@stufft.io> | 2016-03-05 20:13:55 -0500 |
---|---|---|
committer | Donald Stufft <donald@stufft.io> | 2016-03-05 20:13:55 -0500 |
commit | 388081d7fc615cd4b4bf87ab635173c50b86637f (patch) | |
tree | ca0e35f403f8efe6894ed4de3d161b537da1eea3 /virtualenv.py | |
parent | fd5d16bb8dcb2024debe5e88b9b60a04ff39a3b8 (diff) | |
parent | 6d0c1342319dd388683a863d4ae93c82579f7c08 (diff) | |
download | virtualenv-15.0.0.tar.gz |
Merge branch 'develop'15.0.0
Diffstat (limited to 'virtualenv.py')
-rwxr-xr-x | virtualenv.py | 151 |
1 files changed, 79 insertions, 72 deletions
diff --git a/virtualenv.py b/virtualenv.py index 0b4a94d..68a5d87 100755 --- a/virtualenv.py +++ b/virtualenv.py @@ -27,6 +27,7 @@ import struct import subprocess import pkgutil import tempfile +import textwrap from distutils.util import strtobool from os.path import join @@ -35,7 +36,7 @@ try: except ImportError: import configparser as ConfigParser -__version__ = "14.0.6" +__version__ = "15.0.0" virtualenv_version = __version__ # legacy if sys.version_info < (2, 6): @@ -353,22 +354,19 @@ def copyfile(src, dest, symlink=True): def writefile(dest, content, overwrite=True): if not os.path.exists(dest): logger.info('Writing %s', dest) - f = open(dest, 'wb') - f.write(content.encode('utf-8')) - f.close() + with open(dest, 'wb') as f: + f.write(content.encode('utf-8')) return else: - f = open(dest, 'rb') - c = f.read() - f.close() + with open(dest, 'rb') as f: + c = f.read() if c != content.encode("utf-8"): if not overwrite: logger.notify('File %s exists with different content; not overwriting', dest) return logger.notify('Overwriting %s with new content', dest) - f = open(dest, 'wb') - f.write(content.encode('utf-8')) - f.close() + with open(dest, 'wb') as f: + f.write(content.encode('utf-8')) else: logger.info('Content %s already in place', dest) @@ -709,7 +707,7 @@ def main(): def call_subprocess(cmd, show_stdout=True, filter_stdout=None, cwd=None, raise_on_returncode=True, extra_env=None, - remove_from_env=None): + remove_from_env=None, stdin=None): cmd_parts = [] for part in cmd: if len(part) > 45: @@ -739,7 +737,9 @@ def call_subprocess(cmd, show_stdout=True, env = None try: proc = subprocess.Popen( - cmd, stderr=subprocess.STDOUT, stdin=None, stdout=stdout, + cmd, stderr=subprocess.STDOUT, + stdin=None if stdin is None else subprocess.PIPE, + stdout=stdout, cwd=cwd, env=env) except Exception: e = sys.exc_info()[1] @@ -748,6 +748,10 @@ def call_subprocess(cmd, show_stdout=True, raise all_output = [] if stdout is not None: + if stdin is not None: + proc.stdin.write(stdin) + proc.stdin.close() + stdout = proc.stdout encoding = sys.getdefaultencoding() fs_encoding = sys.getfilesystemencoding() @@ -771,7 +775,7 @@ def call_subprocess(cmd, show_stdout=True, else: logger.info(line) else: - proc.communicate() + proc.communicate(stdin) proc.wait() if proc.returncode: if raise_on_returncode: @@ -839,48 +843,56 @@ def install_wheel(project_names, py_executable, search_dirs=None, return urljoin('file:', pathname2url(os.path.abspath(p))) findlinks = ' '.join(space_path2url(d) for d in search_dirs) - sys.path = pythonpath.split(os.pathsep) + sys.path - cert_data = pkgutil.get_data("pip._vendor.requests", "cacert.pem") + SCRIPT = textwrap.dedent(""" + import sys + import pkgutil + import tempfile + import os - if cert_data is not None: - cert_file = tempfile.NamedTemporaryFile(delete=False) - cert_file.write(cert_data) - cert_file.close() - else: - cert_file = None + import pip - try: - cmd = [ - py_executable, '-c', - 'import sys, pip; sys.exit(pip.main(["install", "--ignore-installed"] + sys.argv[1:]))', - ] + project_names - logger.start_progress('Installing %s...' % (', '.join(project_names))) - logger.indent += 2 - - env = { - "PYTHONPATH": pythonpath, - "JYTHONPATH": pythonpath, # for Jython < 3.x - "PIP_FIND_LINKS": findlinks, - "PIP_USE_WHEEL": "1", - "PIP_ONLY_BINARY": ":all:", - "PIP_PRE": "1", - "PIP_USER": "0", - } - - if not download: - env["PIP_NO_INDEX"] = "1" - - if cert_file is not None: - env["PIP_CERT"] = cert_file.name + cert_data = pkgutil.get_data("pip._vendor.requests", "cacert.pem") + if cert_data is not None: + cert_file = tempfile.NamedTemporaryFile(delete=False) + cert_file.write(cert_data) + cert_file.close() + else: + cert_file = None try: - call_subprocess(cmd, show_stdout=False, extra_env=env) + args = ["install", "--ignore-installed"] + if cert_file is not None: + args += ["--cert", cert_file.name] + args += sys.argv[1:] + + sys.exit(pip.main(args)) finally: - logger.indent -= 2 - logger.end_progress() + if cert_file is not None: + os.remove(cert_file.name) + """).encode("utf8") + + cmd = [py_executable, '-'] + project_names + logger.start_progress('Installing %s...' % (', '.join(project_names))) + logger.indent += 2 + + env = { + "PYTHONPATH": pythonpath, + "JYTHONPATH": pythonpath, # for Jython < 3.x + "PIP_FIND_LINKS": findlinks, + "PIP_USE_WHEEL": "1", + "PIP_ONLY_BINARY": ":all:", + "PIP_PRE": "1", + "PIP_USER": "0", + } + + if not download: + env["PIP_NO_INDEX"] = "1" + + try: + call_subprocess(cmd, show_stdout=False, extra_env=env, stdin=SCRIPT) finally: - if cert_file is not None: - os.remove(cert_file.name) + logger.indent -= 2 + logger.end_progress() def create_environment(home_dir, site_packages=False, clear=False, @@ -1583,16 +1595,14 @@ def fixup_scripts(home_dir, bin_dir): if not os.path.isfile(filename): # ignore subdirs, e.g. .svn ones. continue - f = open(filename, 'rb') - try: + lines = None + with open(filename, 'rb') as f: try: lines = f.read().decode('utf-8').splitlines() except UnicodeDecodeError: # This is probably a binary program instead # of a script, so just ignore it. continue - finally: - f.close() if not lines: logger.warn('Script %s is an empty file' % filename) continue @@ -1611,9 +1621,9 @@ def fixup_scripts(home_dir, bin_dir): continue logger.notify('Making script %s relative' % filename) script = relative_script([new_shebang] + lines[1:]) - f = open(filename, 'wb') - f.write('\n'.join(script).encode('utf-8')) - f.close() + with open(filename, 'wb') as f: + f.write('\n'.join(script).encode('utf-8')) + def relative_script(lines): "Return a script that'll work in a relocatable environment." @@ -1660,9 +1670,8 @@ def fixup_pth_and_egg_link(home_dir, sys_path=None): def fixup_pth_file(filename): lines = [] prev_lines = [] - f = open(filename) - prev_lines = f.readlines() - f.close() + with open(filename) as f: + prev_lines = f.readlines() for line in prev_lines: line = line.strip() if (not line or line.startswith('#') or line.startswith('import ') @@ -1677,22 +1686,19 @@ def fixup_pth_file(filename): logger.info('No changes to .pth file %s' % filename) return logger.notify('Making paths in .pth file %s relative' % filename) - f = open(filename, 'w') - f.write('\n'.join(lines) + '\n') - f.close() + with open(filename, 'w') as f: + f.write('\n'.join(lines) + '\n') def fixup_egg_link(filename): - f = open(filename) - link = f.readline().strip() - f.close() + with open(filename) as f: + link = f.readline().strip() if os.path.abspath(link) != link: logger.debug('Link in %s already relative' % filename) return new_link = make_relative_path(filename, link) logger.notify('Rewriting link %s in %s as %s' % (link, filename, new_link)) - f = open(filename, 'w') - f.write(new_link) - f.close() + with open(filename, 'w') as f: + f.write(new_link) def make_relative_path(source, dest, dest_is_directory=True): """ @@ -1775,9 +1781,8 @@ def create_bootstrap_script(extra_text, python_version=''): filename = __file__ if filename.endswith('.pyc'): filename = filename[:-1] - f = codecs.open(filename, 'r', encoding='utf-8') - content = f.read() - f.close() + with codecs.open(filename, 'r', encoding='utf-8') as f: + content = f.read() py_exe = 'python%s' % python_version content = (('#!/usr/bin/env %s\n' % py_exe) + '## WARNING: This file is generated\n' @@ -2297,7 +2302,9 @@ def mach_o_change(path, what, value): do_macho(file, 64, LITTLE_ENDIAN) assert(len(what) >= len(value)) - do_file(open(path, 'r+b')) + + with open(path, 'r+b') as f: + do_file(f) if __name__ == '__main__': |