Create a Python runtime environment :return: shebang with path to the python executable
(source_dir, args)
| 34 | |
| 35 | |
| 36 | def create_python_environment(source_dir, args): |
| 37 | """ Create a Python runtime environment |
| 38 | :return: shebang with path to the python executable |
| 39 | """ |
| 40 | interp = args.python_interp |
| 41 | pyver = subprocess.check_output(interp + " -c \"import sys; print ','.join(map(str, list(sys.version_info[" |
| 42 | "0:3])))\"", shell=True).strip().split(",") |
| 43 | pyver = tuple(map(int, pyver)) |
| 44 | |
| 45 | if pyver < (2, 7, 3): |
| 46 | raise Exception("Python >= 2.7.3 is required for installation.") |
| 47 | |
| 48 | # system python -- just return interp |
| 49 | if args.python == "system": |
| 50 | return "#!" + interp |
| 51 | |
| 52 | if not args.python_venv_dir: |
| 53 | raise Exception("Please specify a virtualenv target installation directory.") |
| 54 | |
| 55 | if args.python_venv_dir_force: |
| 56 | try: |
| 57 | shutil.rmtree(args.python_venv_dir) |
| 58 | except: |
| 59 | pass |
| 60 | |
| 61 | if os.path.exists(args.python_venv_dir) and not args.python_venv_dir_update: |
| 62 | raise Exception("The virtual environment directory already exists.") |
| 63 | |
| 64 | virtualenv_tempdir = tempfile.mkdtemp(prefix="virtualenv", dir=args.scratch_path) |
| 65 | try: |
| 66 | ve_tgz = os.path.join(source_dir, "external", "virtualenv-12.0.7.tar.gz") |
| 67 | to_run = "cd %s && tar xzf %s" % (virtualenv_tempdir, ve_tgz) |
| 68 | print >>sys.stderr, to_run |
| 69 | subprocess.check_call(to_run, shell=True) |
| 70 | |
| 71 | ve_exec = os.path.join(virtualenv_tempdir, "virtualenv-12.0.7", "virtualenv.py") |
| 72 | to_run = "%s -p %s %s" % (ve_exec, interp, args.python_venv_dir) |
| 73 | print >>sys.stderr, to_run |
| 74 | subprocess.check_call(to_run, shell=True) |
| 75 | finally: |
| 76 | if not args.keep_scratch: |
| 77 | try: |
| 78 | shutil.rmtree(virtualenv_tempdir) |
| 79 | except: |
| 80 | pass |
| 81 | |
| 82 | # install requirements |
| 83 | ve_python = os.path.join(args.python_venv_dir, "bin", "python") |
| 84 | ve_pip = os.path.join(args.python_venv_dir, "bin", "pip") |
| 85 | |
| 86 | deleteme = None |
| 87 | try: |
| 88 | cmds = [ve_pip, "install", "--no-cache-dir"] |
| 89 | |
| 90 | if args.fix_cert: |
| 91 | response = urllib2.urlopen('http://curl.haxx.se/ca/cacert.pem') |
| 92 | certdata = response.read() |
| 93 | f = tempfile.NamedTemporaryFile(delete=False) |