provision: Replace virtualenv with python native venv.

- Replace virtualenv with python 3's native venv feature. The venv used is native to
  python3.5+, so there's no need for a separate dependency.
- Remove redundant activation script. An activation script is required
  to use the pip and python in the virtual environment, but because we're
  calling the pip inside the venv, we don't need one.

Fixes #625.
This commit is contained in:
Debayan Ganguly 2021-02-25 07:50:49 +05:30 committed by GitHub
parent 4c75057de1
commit 6ac2165bf1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -6,8 +6,6 @@ import argparse
import subprocess
import glob
from importlib import import_module
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
ZULIP_BOTS_DIR = os.path.join(CURRENT_DIR, '..', 'zulip_bots')
sys.path.append(ZULIP_BOTS_DIR)
@ -48,16 +46,19 @@ the Python version this command is executed with."""
venv_dir = os.path.join(base_dir, venv_name)
if not os.path.isdir(venv_dir):
try:
return_code = subprocess.call(['virtualenv', '-p', options.python_interpreter, venv_dir])
return_code = subprocess.call([options.python_interpreter, '-m', 'venv', venv_dir])
except OSError:
if subprocess.call(['which', 'virtualenv']):
print("{red}Please install the virtualenv package and try again.{end_format}"
.format(red='\033[91m', end_format='\033[0m'))
sys.exit(1)
print("{red}Installation with venv failed. Probable errors are: "
"You are on Ubuntu and you haven't installed python3-venv,"
"or you are running an unsupported python version"
"or python is not installed properly{end_format}"
.format(red=red, end_format=end_format))
sys.exit(1)
raise
else:
# subprocess.call returns 0 if a script executed successfully
if return_code:
raise OSError("The command `virtualenv -p {} {}` failed. Virtualenv not created!"
raise OSError("The command `{} -m venv {}` failed. Virtualenv not created!"
.format(options.python_interpreter, venv_dir))
print("New virtualenv created.")
else:
@ -65,20 +66,12 @@ the Python version this command is executed with."""
if os.path.isdir(os.path.join(venv_dir, 'Scripts')):
# POSIX compatibility layer and Linux environment emulation for Windows
# Virtual uses /Scripts instead of /bin on Windows.
# Read https://virtualenv.pypa.io/en/stable/userguide/
# venv uses /Scripts instead of /bin on Windows cmd and Power Shell.
# Read https://docs.python.org/3/library/venv.html
venv_exec_dir = 'Scripts'
else:
venv_exec_dir = 'bin'
# In order to install all required packages for the venv, we need to activate it. Since
# the activation script sets environmental variables, it needs to be executed inline with
# `import_module`.
activate_module_dir = os.path.abspath(os.path.join(venv_dir, venv_exec_dir))
sys.path.append(activate_module_dir)
import_module('activate_this')
# On OS X, ensure we use the virtualenv version of the python binary for
# future subprocesses instead of the version that this script was launched with. See
# https://stackoverflow.com/questions/26323852/whats-the-meaning-of-pyvenv-launcher-environment-variable