| #!/usr/bin/env bash |
| |
| # This script sets up a Python virtualenv for the Web UI. This creates |
| # a new virtualenv and installs nodeenv inside the virtualenv. |
| |
| set -e |
| trap "exit 1" INT |
| |
| CURRDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" |
| |
| : ${VIRTUALENV_NAME:="linters"} |
| : ${VIRTUALENV_DIRECTORY:="${CURRDIR}/.virtualenv"} |
| |
| : ${PYTHON:="$(which python)"} |
| : ${VIRTUALENV:="$(which virtualenv)"} |
| |
| OLD_PYTHONPATH="${PYTHONPATH}" |
| PYTHONPATH="" |
| |
| # If we already have a virtual environment activated, |
| # bail out and advise the user to deactivate. |
| OLD_VIRTUAL_ENV="${VIRTUAL_ENV}" |
| if [ "${OLD_VIRTUAL_ENV}" != "" ]; then |
| echo "Please deactivate your current virtual environment in order to continue!" |
| echo "source deactivate" |
| exit 1 |
| fi |
| |
| # Verify that python is installed. |
| if [ "${PYTHON}" = "" ]; then |
| echo "You must have python installed in order to continue." |
| exit 1 |
| fi |
| |
| # Old versions of virtualenv do not remove the bin directory in the |
| # virtual environment even when using `--clear`. We thus remove the |
| # entire directory in case the virtual environment already exists. |
| # See https://github.com/pypa/virtualenv/issues/2 for more info. |
| rm -rf ${VIRTUALENV_DIRECTORY} |
| |
| PYTHON_MAJOR=$(${PYTHON} -c 'import sys; print(sys.version_info[0])') |
| PYTHON_MINOR=$(${PYTHON} -c 'import sys; print(sys.version_info[1])') |
| |
| if [ "${PYTHON_MAJOR}" = "3" ]; then |
| if [ "${PYTHON_MINOR}" -lt "6" ]; then |
| echo "You must be running python 3.6 or newer in order to continue." |
| echo "Consider running as 'PYTHON=python3 ${0}' or similar." |
| exit 1 |
| else |
| # Set up a virtual environment for the linters. |
| ${PYTHON} -m venv --prompt="${VIRTUALENV_NAME}" ${VIRTUALENV_DIRECTORY} |
| fi |
| elif [ "${PYTHON_MAJOR}" = "2" ]; then |
| if [ "${PYTHON_MINOR}" -lt "6" ]; then |
| echo "You must be running python 2.6 or 2.7 in order to continue." |
| echo "Consider running as 'PYTHON=python2 ${0} or similar." |
| exit 1 |
| else |
| if [ "${VIRTUALENV}" = "" ]; then |
| # Search for a locally installed virtualenv. |
| # See https://docs.python.org/2/library/site.html#site.USER_SITE for details. |
| VIRTUALENV=$(${PYTHON} -c "import site; print site.USER_SITE")/virtualenv.py |
| fi |
| |
| if [ ! -f "${VIRTUALENV}" ]; then |
| echo "You must have virtualenv installed in order to continue..." |
| exit 1 |
| fi |
| |
| # Set up a virtual environment for the linters. |
| ${PYTHON} ${VIRTUALENV} --python=${PYTHON} \ |
| --no-site-packages \ |
| --prompt="(${VIRTUALENV_NAME}) " \ |
| ${VIRTUALENV_DIRECTORY} |
| fi |
| else |
| echo "You must be running either python 2.6, 2.7," |
| echo "or python 3.6 or newer in order to continue." |
| echo "Consider running as 'PYTHON=python3 ${0}' or similar." |
| exit 1 |
| fi |
| |
| source ${VIRTUALENV_DIRECTORY}/bin/activate |
| pip install --upgrade pip |
| pip install -r ${CURRDIR}/pip-requirements.txt |
| |
| # For now we hard code pulling in the pip-requirements from the python |
| # CLI's virtualenv in addition to our own pip-requirements for the |
| # linter virtualenv. In the future, we need to make this more generic |
| # to support multiple lintable python projects each with their own |
| # virtualenvs (and potentially conflicting libraries). |
| # |
| # We have opened a JIRA ticket to track this: |
| # https://issues.apache.org/jira/browse/MESOS-8206 |
| pip install -r ${CURRDIR}/../src/python/cli_new/pip-requirements.txt |
| pip install -r ${CURRDIR}/../src/python/lib/requirements.in |
| |
| # Add Node.js virtual environment to the existing virtual environment. |
| nodeenv -p |
| |
| # Restart the virtual environment to then have npm available. |
| deactivate |
| source ${VIRTUALENV_DIRECTORY}/bin/activate |
| |
| # Install the JavaScript linter in the virtual environment. |
| npm install -g eslint@5.1.0 |
| deactivate |