Skip to content

Instantly share code, notes, and snippets.

@delijati
Last active November 25, 2016 14:02
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 delijati/86977a7cb0736ae9e29f3bdff5484fd3 to your computer and use it in GitHub Desktop.
Save delijati/86977a7cb0736ae9e29f3bdff5484fd3 to your computer and use it in GitHub Desktop.
virtualenv + pip self deployed
from __future__ import print_function
import sys
import shutil
import os
import subprocess
is_win = (sys.platform == 'win32')
req_file = "req.txt"
home_dir = os.path.dirname(os.path.abspath(__file__))
env_dir = os.path.join(home_dir, "env")
bin_dir = os.path.join(env_dir, "bin")
if is_win:
bin_dir = os.path.join(env_dir, 'Scripts')
static_dir = os.path.join(home_dir, "cosima", "static")
VENV_VERSION = "12.0.7"
PYPI_VENV_BASE = "http://pypi.python.org/packages/source/v/virtualenv"
PYTHON = "python2"
def shellcmd(cmd, echo=True, cwd=None):
""" Run 'cmd' in the shell and return its standard out.
"""
cmd = cmd.split()
if echo:
print('[cmd] {0}'.format(cmd))
if subprocess.call(cmd, cwd=cwd) != 0:
print("Aborting...")
sys.exit(1)
def virtualenv():
dirname = 'virtualenv-' + VENV_VERSION
tgz_file = dirname + '.tar.gz'
# Fetch virtualenv from PyPI
venv_url = PYPI_VENV_BASE + '/' + tgz_file
shellcmd('wget {0}'.format(venv_url))
# Untar
shellcmd('tar xzf {0}'.format(tgz_file))
# Create the initial env
shellcmd('{0} {1}/virtualenv.py {2}'.format(PYTHON, dirname, env_dir))
# Install the virtualenv package itself into the initial env
shellcmd('{0}/pip install {1}'.format(bin_dir, tgz_file))
# Cleanup
shutil.rmtree(dirname)
os.remove(tgz_file)
def main():
print("Creating virtualenv...")
virtualenv()
print("Installing eggs...")
shellcmd("{cmd} install -r {req}".format(
cmd=os.path.join(bin_dir, "pip"),
req=req_file))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment