2018-05-16 13:01:18 -04:00
|
|
|
#!/usr/bin/env python3
|
2017-08-23 10:41:27 -04:00
|
|
|
|
|
|
|
import argparse
|
2018-06-04 18:31:53 -04:00
|
|
|
import glob
|
2021-05-28 05:00:04 -04:00
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import sys
|
2017-08-23 10:41:27 -04:00
|
|
|
|
2017-09-20 00:12:40 -04:00
|
|
|
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
|
2021-05-28 05:05:11 -04:00
|
|
|
ZULIP_BOTS_DIR = os.path.join(CURRENT_DIR, "..", "zulip_bots")
|
2017-09-20 00:12:40 -04:00
|
|
|
sys.path.append(ZULIP_BOTS_DIR)
|
|
|
|
|
2021-05-28 05:05:11 -04:00
|
|
|
red = "\033[91m"
|
|
|
|
green = "\033[92m"
|
|
|
|
end_format = "\033[0m"
|
|
|
|
bold = "\033[1m"
|
2017-11-23 11:46:03 -05:00
|
|
|
|
2021-05-28 05:03:46 -04:00
|
|
|
|
2017-08-23 10:41:27 -04:00
|
|
|
def main():
|
2017-08-24 15:36:04 -04:00
|
|
|
usage = """./tools/provision
|
|
|
|
|
|
|
|
Creates a Python virtualenv. Its Python version is equal to
|
|
|
|
the Python version this command is executed with."""
|
|
|
|
parser = argparse.ArgumentParser(usage=usage)
|
2021-05-28 05:03:46 -04:00
|
|
|
parser.add_argument(
|
2021-05-28 05:05:11 -04:00
|
|
|
"--python-interpreter",
|
|
|
|
"-p",
|
|
|
|
metavar="PATH_TO_PYTHON_INTERPRETER",
|
2021-05-28 05:03:46 -04:00
|
|
|
default=os.path.abspath(sys.executable),
|
2021-05-28 05:05:11 -04:00
|
|
|
help="Path to the Python interpreter to use when provisioning.",
|
2021-05-28 05:03:46 -04:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2021-05-28 05:05:11 -04:00
|
|
|
"--force", "-f", action="store_true", help="create venv even with outdated Python version."
|
2021-05-28 05:03:46 -04:00
|
|
|
)
|
2017-11-10 16:11:54 -05:00
|
|
|
options = parser.parse_args()
|
2017-08-23 10:41:27 -04:00
|
|
|
|
2021-05-28 05:05:11 -04:00
|
|
|
base_dir = os.path.abspath(os.path.join(__file__, "..", ".."))
|
2021-05-28 05:03:46 -04:00
|
|
|
py_version_output = subprocess.check_output(
|
2021-05-28 05:05:11 -04:00
|
|
|
[options.python_interpreter, "--version"], stderr=subprocess.STDOUT, universal_newlines=True
|
2021-05-28 05:03:46 -04:00
|
|
|
)
|
2017-11-23 11:39:54 -05:00
|
|
|
# The output has the format "Python 1.2.3"
|
2021-05-28 05:05:11 -04:00
|
|
|
py_version_list = py_version_output.split()[1].split(".")
|
2018-10-31 19:32:56 -04:00
|
|
|
py_version = tuple(int(num) for num in py_version_list[0:2])
|
2021-05-28 07:19:40 -04:00
|
|
|
venv_name = f"zulip-api-py{py_version[0]}-venv"
|
2017-08-23 10:41:27 -04:00
|
|
|
|
2017-12-31 09:50:34 -05:00
|
|
|
if py_version <= (3, 1) and (not options.force):
|
2021-05-28 05:03:46 -04:00
|
|
|
print(
|
|
|
|
red + "Provision failed: Cannot create venv with outdated Python version ({}).\n"
|
|
|
|
"Maybe try `python3 tools/provision`.".format(py_version_output.strip()) + end_format
|
|
|
|
)
|
2017-12-31 09:50:34 -05:00
|
|
|
sys.exit(1)
|
|
|
|
|
2017-08-23 10:41:27 -04:00
|
|
|
venv_dir = os.path.join(base_dir, venv_name)
|
|
|
|
if not os.path.isdir(venv_dir):
|
2017-09-26 20:35:21 -04:00
|
|
|
try:
|
2021-05-28 05:05:11 -04:00
|
|
|
return_code = subprocess.call([options.python_interpreter, "-m", "venv", venv_dir])
|
2017-09-26 20:35:21 -04:00
|
|
|
except OSError:
|
2021-05-28 05:03:46 -04:00
|
|
|
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
|
|
|
|
)
|
|
|
|
)
|
2021-02-24 21:20:49 -05:00
|
|
|
sys.exit(1)
|
2017-09-26 20:35:21 -04:00
|
|
|
raise
|
2017-08-23 10:41:27 -04:00
|
|
|
else:
|
2021-02-24 21:20:49 -05:00
|
|
|
# subprocess.call returns 0 if a script executed successfully
|
2017-09-26 20:35:21 -04:00
|
|
|
if return_code:
|
2021-05-28 05:03:46 -04:00
|
|
|
raise OSError(
|
|
|
|
"The command `{} -m venv {}` failed. Virtualenv not created!".format(
|
|
|
|
options.python_interpreter, venv_dir
|
|
|
|
)
|
|
|
|
)
|
2017-08-23 10:41:27 -04:00
|
|
|
print("New virtualenv created.")
|
|
|
|
else:
|
|
|
|
print("Virtualenv already exists.")
|
|
|
|
|
2021-05-28 05:05:11 -04:00
|
|
|
if os.path.isdir(os.path.join(venv_dir, "Scripts")):
|
2017-08-23 10:41:27 -04:00
|
|
|
# POSIX compatibility layer and Linux environment emulation for Windows
|
2021-02-24 21:20:49 -05:00
|
|
|
# venv uses /Scripts instead of /bin on Windows cmd and Power Shell.
|
|
|
|
# Read https://docs.python.org/3/library/venv.html
|
2021-05-28 05:05:11 -04:00
|
|
|
venv_exec_dir = "Scripts"
|
2017-08-23 10:41:27 -04:00
|
|
|
else:
|
2021-05-28 05:05:11 -04:00
|
|
|
venv_exec_dir = "bin"
|
2017-08-23 10:41:27 -04:00
|
|
|
|
2018-11-29 23:18:28 -05:00
|
|
|
# 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
|
2021-05-28 05:05:11 -04:00
|
|
|
if "__PYVENV_LAUNCHER__" in os.environ:
|
|
|
|
del os.environ["__PYVENV_LAUNCHER__"]
|
2018-11-29 23:18:28 -05:00
|
|
|
|
2017-08-23 10:41:27 -04:00
|
|
|
# In order to install all required packages for the venv, `pip` needs to be executed by
|
|
|
|
# the venv's Python interpreter. `--prefix venv_dir` ensures that all modules are installed
|
|
|
|
# in the right place.
|
2017-09-15 07:24:35 -04:00
|
|
|
def install_dependencies(requirements_filename):
|
2021-05-28 05:05:11 -04:00
|
|
|
pip_path = os.path.join(venv_dir, venv_exec_dir, "pip")
|
2017-09-28 15:08:42 -04:00
|
|
|
# We first install a modern version of pip that supports --prefix
|
2021-05-28 05:05:11 -04:00
|
|
|
subprocess.call([pip_path, "install", "pip>=9.0"])
|
2021-05-28 05:03:46 -04:00
|
|
|
if subprocess.call(
|
|
|
|
[
|
|
|
|
pip_path,
|
2021-05-28 05:05:11 -04:00
|
|
|
"install",
|
|
|
|
"--prefix",
|
2021-05-28 05:03:46 -04:00
|
|
|
venv_dir,
|
2021-05-28 05:05:11 -04:00
|
|
|
"-r",
|
2021-05-28 05:03:46 -04:00
|
|
|
os.path.join(base_dir, requirements_filename),
|
|
|
|
]
|
|
|
|
):
|
|
|
|
raise OSError(
|
|
|
|
"The command `pip install -r {}` failed. Dependencies not installed!".format(
|
|
|
|
os.path.join(base_dir, requirements_filename)
|
|
|
|
)
|
|
|
|
)
|
2017-09-15 07:24:35 -04:00
|
|
|
|
2021-05-28 05:05:11 -04:00
|
|
|
install_dependencies("requirements.txt")
|
2017-11-10 16:03:40 -05:00
|
|
|
|
2018-06-04 18:31:53 -04:00
|
|
|
# Install all requirements for all bots. get_bot_paths()
|
|
|
|
# has requirements that must be satisfied prior to calling
|
|
|
|
# it by setup().
|
|
|
|
current_dir = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
bots_dir = os.path.join(current_dir, "..", "zulip_bots", "zulip_bots", "bots")
|
|
|
|
req_paths = glob.glob(bots_dir + "/*/requirements.txt")
|
|
|
|
for req_path in req_paths:
|
|
|
|
path_split = req_path.split(os.path.sep)[-5:]
|
|
|
|
relative_path = os.path.join(*path_split)
|
|
|
|
install_dependencies(relative_path)
|
|
|
|
|
2021-05-28 05:05:11 -04:00
|
|
|
print(green + "Success!" + end_format)
|
2017-11-10 16:03:40 -05:00
|
|
|
|
2021-05-28 05:05:11 -04:00
|
|
|
activate_command = os.path.join(base_dir, venv_dir, venv_exec_dir, "activate")
|
2020-09-20 09:40:31 -04:00
|
|
|
# We make the path look like a Unix path, because most Windows users
|
|
|
|
# are likely to be running in a bash shell.
|
2021-05-28 05:05:11 -04:00
|
|
|
activate_command = activate_command.replace(os.sep, "/")
|
|
|
|
print("\nRun the following to enter into the virtualenv:\n")
|
|
|
|
print(bold + " source " + activate_command + end_format + "\n")
|
2017-11-10 16:03:40 -05:00
|
|
|
|
2017-08-23 10:41:27 -04:00
|
|
|
|
2021-05-28 05:05:11 -04:00
|
|
|
if __name__ == "__main__":
|
2017-08-23 10:41:27 -04:00
|
|
|
main()
|