Skip to content

Instantly share code, notes, and snippets.

@apatrushev
Last active August 5, 2017 01:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apatrushev/c0fe0a9bd5cee61449416080b6725224 to your computer and use it in GitHub Desktop.
Save apatrushev/c0fe0a9bd5cee61449416080b6725224 to your computer and use it in GitHub Desktop.
from __future__ import print_function
from contextlib import closing, contextmanager
import os
import re
import sys
import tarfile
import tempfile
import shutil
import ssl
try:
from urllib2 import urlopen
from StringIO import StringIO
except ImportError:
from urllib.request import urlopen
from io import BytesIO as StringIO
PYPI_VENV_PAGE = 'https://pypi.python.org/pypi/virtualenv'
INITIAL_ENV = 'py-env0'
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
@contextmanager
def removable_tempdir():
target = tempfile.mkdtemp()
yield target
shutil.rmtree(target)
# sanity check
if os.path.exists(INITIAL_ENV) or ():
raise RuntimeError('env directory already exists')
elif len(sys.argv) > 1 and os.path.exists(sys.argv[1]):
raise RuntimeError('second stage env directory already exists')
# fetch venv url
with closing(urlopen(PYPI_VENV_PAGE, context=ctx)) as resp:
if resp.getcode() != 200:
raise RuntimeError('Wrong response receive from PyPI')
dist_url = None
for line in resp:
m = re.match(b'.*(https.*virtualenv.*tar.gz).*md5.*', line)
if m is not None:
dist_url = m.groups()[0]
if dist_url is None:
raise RuntimeError('virtualenv dist not found')
# Fetch virtualenv from PyPI
with closing(urlopen(dist_url.decode(), context=ctx)) as resp:
if resp.getcode() != 200:
raise RuntimeError('Wrong response receive from PyPI')
data = resp.read()
# Untar
data = StringIO(data)
with removable_tempdir() as temp_dir:
with closing(tarfile.open(fileobj=data)) as data:
data.extractall(temp_dir)
venv_py = os.path.join(
temp_dir,
os.listdir(temp_dir)[0],
'virtualenv.py'
)
# Create the initial env
os.system('{0} {1} {2}'.format(sys.executable, venv_py, INITIAL_ENV))
# Second stage
if len(sys.argv) > 1:
os.system('{0}/bin/pip install virtualenv'.format(INITIAL_ENV))
os.system('{0}/bin/virtualenv {1}'.format(INITIAL_ENV, sys.argv[1]))
shutil.rmtree(INITIAL_ENV)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment