provision: Rewrite in Python.
This commit is contained in:
parent
5bc98bfdf7
commit
0984eae9b5
|
@ -1,48 +1,59 @@
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env python
|
||||||
|
|
||||||
BASEDIR=$(dirname `dirname $0`)
|
import os
|
||||||
VENVEXECDIR="bin"
|
import sys
|
||||||
PYVERSION=$(python -V | grep -Po '(?<=Python ).+')
|
import argparse
|
||||||
PYCUSTOMPATH_ARG=""
|
import pip
|
||||||
while getopts "hp:" arg; do
|
import six
|
||||||
case "$arg" in
|
import subprocess
|
||||||
h)
|
|
||||||
echo "USAGE: $0 [-p <path_to_python_version>]"
|
|
||||||
exit
|
|
||||||
;;
|
|
||||||
p)
|
|
||||||
PYCUSTOMPATH_ARG="-p $OPTARG"
|
|
||||||
PYVERSION=$("$OPTARG" "-V" 2>&1 | grep -Po '(?<=Python ).+')
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
done
|
|
||||||
VENVDIR="zulip-api-py$PYVERSION-venv"
|
|
||||||
|
|
||||||
if [ ! -d "$BASEDIR/$VENVDIR" ]; then
|
from importlib import import_module
|
||||||
virtualenv $PYCUSTOMPATH_ARG "$BASEDIR/$VENVDIR"
|
|
||||||
echo "Virtualenv created."
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ ! -d "$BASEDIR/$VENVDIR/$VENVEXECDIR" ]]; then
|
def main():
|
||||||
# POSIX compatibility layer and Linux environment emulation for Windows
|
parser = argparse.ArgumentParser("Creates a Python virtualenv. Its Python version is equal to "
|
||||||
# Virtual uses /Scripts instead of /bin on Windows.
|
"the Python version this command is executed with.")
|
||||||
# Read https://virtualenv.pypa.io/en/stable/userguide/
|
parser.parse_args()
|
||||||
VENVEXECDIR="Scripts"
|
|
||||||
fi
|
|
||||||
|
|
||||||
source "$BASEDIR/$VENVDIR/$VENVEXECDIR/activate"
|
base_dir = os.path.abspath(os.path.join(__file__, '..', '..'))
|
||||||
RVAL=$?
|
python_version = '.'.join(str(num) for num in sys.version_info[0:3])
|
||||||
if [ "$RVAL" -ne 0 ]; then
|
python_interpreter = os.path.abspath(sys.executable)
|
||||||
echo "Failed to activate virtualenv."
|
venv_name = 'zulip-api-py{}-venv'.format(python_version)
|
||||||
exit
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Install python dependencies if needed.
|
venv_dir = os.path.join(base_dir, venv_name)
|
||||||
cmp "$BASEDIR/$VENVDIR/installed-requirements.txt" requirements.txt 2>/dev/null
|
if not os.path.isdir(venv_dir):
|
||||||
RVAL=$? # Return value of the comparision. 0 means files are same.
|
if subprocess.call(['virtualenv', '-p', python_interpreter, venv_dir]):
|
||||||
if [ "$RVAL" -ne 0 ]; then
|
raise OSError("The command `virtualenv -p {} {}` failed. Virtualenv not created!"
|
||||||
pip install -r "$BASEDIR/requirements.txt"
|
.format(python_interpreter, venv_dir))
|
||||||
cp -a requirements.txt "$BASEDIR/$VENVDIR/installed-requirements.txt"
|
else:
|
||||||
echo "Requirements installed."
|
print("New virtualenv created.")
|
||||||
fi
|
else:
|
||||||
echo 'Success! Run `source' "$BASEDIR/$VENVDIR/$VENVEXECDIR/activate"'`' "to activate virtualenv."
|
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')))
|
||||||
|
|
||||||
|
print("Success! Run `source {}` to activate virtualenv.".format(
|
||||||
|
os.path.join(base_dir, venv_dir, venv_exec_dir, 'activate')))
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
|
|
Loading…
Reference in a new issue