Last active
April 4, 2019 13:24
-
-
Save Jinmo/5f402920220d07696ac25ffbf8284a6a to your computer and use it in GitHub Desktop.
virtualenv installer using builtin modules (python 2.7), also installs pip and wheel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from hashlib import sha256 | |
import os | |
import sys | |
import logging | |
import urllib2 | |
import tempfile | |
# extracted from https://pypi.org/simple/virtualenv/ | |
VIRTUALENV_URL = 'https://files.pythonhosted.org/packages/33/5d/' \ | |
'314c760d4204f64e4a968275182b7751bd5c3249094757b39ba987dcfb5a/virtualenv-16.4.3-py2.py3-none-any.whl' | |
HASH = '6aebaf4dd2568a0094225ebbca987859e369e3e5c22dc7d52e5406d504890417' | |
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG) | |
def _install_virtualenv(path): | |
logging.info('Downloading virtualenv from %r ...' % VIRTUALENV_URL) | |
data = urllib2.urlopen(VIRTUALENV_URL).read() | |
assert sha256(data).hexdigest() == HASH, 'hash error... MITM?' | |
with tempfile.NamedTemporaryFile('wb', suffix=".zip", delete=False) as zf: | |
zf.write(data) | |
zf.flush() | |
sys.path.insert(0, zf.name) | |
import virtualenv | |
logging.info('Creating environment using virtualenv...') | |
virtualenv.create_environment(path) | |
logging.info('Done!') | |
def prepare_virtualenv(path): | |
abspath = os.path.abspath(path) | |
sys.path.insert(0, abspath) | |
try: | |
activator_path = os.path.join(abspath, 'Scripts', 'activate_this.py') | |
if not os.path.isfile(activator_path): | |
raise ImportError() | |
execfile(activator_path, {'__file__': activator_path}) | |
except ImportError: | |
logging.info('Will install virtualenv at %r since the module is not found...' % path) | |
_install_virtualenv(path) | |
prepare_virtualenv(path) | |
if __name__ == '__main__': | |
prepare_virtualenv('./virtualenv_test') | |
import pip | |
print pip.__file__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment