@task
def virtualenv_install():
  """Install the latest virtual environment if needed.
  We do nothing if the env is already the latest.
  We do install the new environment even if we are using the old style.
  This does not activate (symlink) the newly installed environment.
  """
  # Installs the latest virtual environment from the local prod-requirements.txt.
  prod_rev = latest_requirements_revision()
  assert re.match(r'[0-9a-f]+', prod_rev)

  active_env_rev = active_env()
  if prod_rev == active_env_rev:
    assert virtualenv_verify(prod_rev), 'Active environment is not valid'
    return

  env_dir = 'env.%s' % prod_rev
  package_dir = 'python-package.%s' % prod_rev
  requirements_file = 'prod-requirements.txt.%s' % prod_rev
  if exists(env_dir):
    fprint('prod-requirements (rev %s) already installed, but not active.' % prod_rev)
  else:
    fprint('installing environment from prod-requirements (rev %s)' % prod_rev)
    run('rm -rf ~/%s ~/%s ~/%s' % (env_dir, package_dir, requirements_file))
    rsync_project(local_dir='third_party/python-package/', remote_dir='~/%s/' % package_dir, ssh_opts='-o StrictHostKeyChecking=no')
    put('scripts/prod-requirements.txt', '~/%s' % requirements_file)
    run('python2.7 ~/%s/virtualenv.py --never-download ~/%s/viewfinder' % (package_dir, env_dir))

  # Let fabric surface the failure.
  run('~/%s/viewfinder/bin/pip install -f file://$HOME/%s --no-index -r ~/%s' %
      (env_dir, package_dir, requirements_file))
  # Do not delete the prod-requirements file when done as we may use it to verify the environment later.
dataset/ETHPy150Open viewfinderco/viewfinder/fabfile.py/virtualenv_install/original