2017-08-23 10:41:27 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import argparse
|
|
|
|
import pip
|
|
|
|
import six
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
from importlib import import_module
|
|
|
|
|
|
|
|
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)
|
2017-08-23 10:41:27 -04:00
|
|
|
parser.parse_args()
|
|
|
|
|
|
|
|
base_dir = os.path.abspath(os.path.join(__file__, '..', '..'))
|
|
|
|
python_interpreter = os.path.abspath(sys.executable)
|
2017-08-24 15:37:03 -04:00
|
|
|
venv_name = 'zulip-api-py{}-venv'.format(sys.version_info.major)
|
2017-08-23 10:41:27 -04:00
|
|
|
|
|
|
|
venv_dir = os.path.join(base_dir, venv_name)
|
|
|
|
if not os.path.isdir(venv_dir):
|
|
|
|
if subprocess.call(['virtualenv', '-p', python_interpreter, venv_dir]):
|
|
|
|
raise OSError("The command `virtualenv -p {} {}` failed. Virtualenv not created!"
|
|
|
|
.format(python_interpreter, venv_dir))
|
|
|
|
else:
|
|
|
|
print("New virtualenv created.")
|
|
|
|
else:
|
|
|
|
print("Virtualenv already exists.")
|
|
|
|
|
|
|
|
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_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')
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
if subprocess.call([os.path.join(venv_dir, venv_exec_dir, 'pip'),
|
|
|
|
'install', '--prefix', venv_dir, '-r', os.path.join(base_dir, 'requirements.txt')]):
|
|
|
|
raise OSError("The command `pip install -r {}` failed. Dependencies not installed!"
|
|
|
|
.format(os.path.join(base_dir, 'requirements.txt')))
|
|
|
|
|
2017-09-12 10:48:37 -04:00
|
|
|
print("{green}Success!{end_format} Run\n {bold}source '{activate}'{end_format}\nto activate virtualenv.".format(
|
2017-09-01 12:24:17 -04:00
|
|
|
green='\033[92m', bold='\033[1m', end_format='\033[0m',
|
|
|
|
activate=os.path.join(base_dir, venv_dir, venv_exec_dir, 'activate')))
|
2017-08-23 10:41:27 -04:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|