2020-06-22 18:57:04 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
set -o nounset
|
|
|
|
set -o errexit
|
|
|
|
set -o pipefail
|
|
|
|
|
2021-07-13 22:08:51 +00:00
|
|
|
USAGE=$(
|
|
|
|
cat << 'END_OF_LINE'
|
2021-08-04 13:30:18 +00:00
|
|
|
Configure a development environment for this repository.
|
2020-06-22 18:57:04 +00:00
|
|
|
|
|
|
|
It does the following:
|
|
|
|
- Verifies pyenv and pyenv-virtualenv are installed.
|
|
|
|
- Creates a Python virtual environment.
|
|
|
|
- Configures the activation of the virtual enviroment for the repo directory.
|
2020-06-22 20:55:01 +00:00
|
|
|
- Installs the requirements needed for development.
|
2020-06-22 18:57:04 +00:00
|
|
|
- Installs git pre-commit hooks.
|
|
|
|
- Configures git upstream remote "lineage" repositories.
|
|
|
|
|
2020-06-22 20:55:01 +00:00
|
|
|
Usage:
|
2020-06-22 21:18:19 +00:00
|
|
|
setup-env [options] [virt_env_name]
|
2020-06-22 20:55:01 +00:00
|
|
|
setup-env (-h | --help)
|
|
|
|
|
|
|
|
Options:
|
|
|
|
-f --force Delete virtual enviroment if it already exists.
|
|
|
|
-h --help Show this message.
|
|
|
|
-i --install-hooks Install hook environments for all environments in the
|
|
|
|
pre-commit config file.
|
2020-06-22 18:57:04 +00:00
|
|
|
|
|
|
|
END_OF_LINE
|
|
|
|
)
|
|
|
|
|
|
|
|
# Flag to force deletion and creation of virtual environment
|
|
|
|
FORCE=0
|
|
|
|
|
|
|
|
# Positional parameters
|
|
|
|
PARAMS=""
|
|
|
|
|
|
|
|
# Parse command line arguments
|
2021-07-13 22:08:51 +00:00
|
|
|
while (("$#")); do
|
2020-06-22 18:57:04 +00:00
|
|
|
case "$1" in
|
2021-07-13 22:08:51 +00:00
|
|
|
-f | --force)
|
2020-06-22 18:57:04 +00:00
|
|
|
FORCE=1
|
|
|
|
shift
|
|
|
|
;;
|
2021-07-13 22:08:51 +00:00
|
|
|
-h | --help)
|
2020-06-22 18:57:04 +00:00
|
|
|
echo "${USAGE}"
|
|
|
|
exit 0
|
|
|
|
;;
|
2021-07-13 22:08:51 +00:00
|
|
|
-i | --install-hooks)
|
2020-06-22 21:05:19 +00:00
|
|
|
INSTALL_HOOKS=1
|
|
|
|
shift
|
|
|
|
;;
|
2020-06-22 18:57:04 +00:00
|
|
|
-*) # unsupported flags
|
2021-01-15 15:34:34 +00:00
|
|
|
echo "Error: Unsupported flag $1" >&2
|
|
|
|
exit 1
|
|
|
|
;;
|
2020-06-22 18:57:04 +00:00
|
|
|
*) # preserve positional arguments
|
2021-01-15 15:34:34 +00:00
|
|
|
PARAMS="$PARAMS $1"
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
esac
|
2020-06-22 18:57:04 +00:00
|
|
|
done
|
|
|
|
|
|
|
|
# set positional arguments in their proper place
|
|
|
|
eval set -- "$PARAMS"
|
|
|
|
|
|
|
|
# Check to see if pyenv is installed
|
2020-06-22 21:02:03 +00:00
|
|
|
if [ -z "$(command -v pyenv)" ] || [ -z "$(command -v pyenv-virtualenv)" ]; then
|
2020-06-22 18:57:04 +00:00
|
|
|
echo "pyenv and pyenv-virtualenv are required."
|
|
|
|
if [[ "$OSTYPE" == "darwin"* ]]; then
|
|
|
|
cat << 'END_OF_LINE'
|
|
|
|
|
2020-06-22 21:13:51 +00:00
|
|
|
On the Mac, we recommend installing brew, https://brew.sh/. Then installation
|
|
|
|
is as simple as `brew install pyenv pyenv-virtualenv` and adding this to your
|
|
|
|
profile:
|
2020-06-22 18:57:04 +00:00
|
|
|
|
|
|
|
eval "$(pyenv init -)"
|
|
|
|
eval "$(pyenv virtualenv-init -)"
|
|
|
|
|
|
|
|
END_OF_LINE
|
|
|
|
|
|
|
|
fi
|
|
|
|
cat << 'END_OF_LINE'
|
|
|
|
For Linux, Windows Subsystem for Linux (WSL), or on the Mac (if you don't want
|
|
|
|
to use "brew") you can use https://github.com/pyenv/pyenv-installer to install
|
|
|
|
the necessary tools. Before running this ensure that you have installed the
|
|
|
|
prerequisites for your platform according to the pyenv wiki page,
|
|
|
|
https://github.com/pyenv/pyenv/wiki/common-build-problems.
|
|
|
|
|
|
|
|
On WSL you should treat your platform as whatever Linux distribution you've
|
|
|
|
chosen to install.
|
|
|
|
|
|
|
|
Once you have installed "pyenv" you will need to add the following lines to
|
|
|
|
your ".bashrc":
|
|
|
|
|
|
|
|
export PATH="$PATH:$HOME/.pyenv/bin"
|
|
|
|
eval "$(pyenv init -)"
|
|
|
|
eval "$(pyenv virtualenv-init -)"
|
|
|
|
END_OF_LINE
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
set +o nounset
|
|
|
|
# Determine the virtual environment name
|
|
|
|
if [ "$1" ]; then
|
|
|
|
# Use the user-provided environment name
|
|
|
|
env_name=$1
|
|
|
|
else
|
|
|
|
# Set the environment name to the last part of the working directory.
|
|
|
|
env_name=${PWD##*/}
|
|
|
|
fi
|
|
|
|
set -o nounset
|
|
|
|
|
|
|
|
# Remove any lingering local configuration.
|
|
|
|
if [ $FORCE -ne 0 ]; then
|
|
|
|
rm -f .python-version
|
|
|
|
pyenv virtualenv-delete --force "${env_name}" || true
|
|
|
|
elif [[ -f .python-version ]]; then
|
|
|
|
cat << 'END_OF_LINE'
|
|
|
|
An existing .python-version file was found. Either remove this file yourself
|
|
|
|
or re-run with --force option to have it deleted along with the associated
|
|
|
|
virtual environment.
|
|
|
|
|
|
|
|
rm .python-version
|
|
|
|
|
|
|
|
END_OF_LINE
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2020-06-22 19:15:18 +00:00
|
|
|
# Create a new virtual environment for this project
|
2020-06-22 18:57:04 +00:00
|
|
|
if ! pyenv virtualenv "${env_name}"; then
|
|
|
|
cat << END_OF_LINE
|
|
|
|
An existing virtual environment named $env_name was found. Either delete this
|
|
|
|
environment yourself or re-run with --force option to have it deleted.
|
|
|
|
|
|
|
|
pyenv virtualenv-delete ${env_name}
|
|
|
|
|
|
|
|
END_OF_LINE
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2020-06-22 20:30:46 +00:00
|
|
|
# Set the local application-specific Python version(s) by writing the
|
|
|
|
# version name to a file named `.python-version'.
|
2020-06-22 18:57:04 +00:00
|
|
|
pyenv local "${env_name}"
|
|
|
|
|
|
|
|
# Upgrade pip and friends
|
2020-06-22 20:34:53 +00:00
|
|
|
python3 -m pip install --upgrade pip setuptools wheel
|
2020-06-22 18:57:04 +00:00
|
|
|
|
|
|
|
# Find a requirements file (if possible) and install
|
|
|
|
for req_file in "requirements-dev.txt" "requirements-test.txt" "requirements.txt"; do
|
2020-06-22 20:50:42 +00:00
|
|
|
if [[ -f $req_file ]]; then
|
|
|
|
pip install --requirement $req_file
|
2020-06-22 18:57:04 +00:00
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2020-06-22 20:55:34 +00:00
|
|
|
# Install git pre-commit hooks now or later.
|
|
|
|
pre-commit install ${INSTALL_HOOKS:+"--install-hooks"}
|
2020-06-22 18:57:04 +00:00
|
|
|
|
|
|
|
# Setup git remotes from lineage configuration
|
|
|
|
# This could fail if the remotes are already setup, but that is ok.
|
|
|
|
set +o errexit
|
|
|
|
|
2021-07-13 22:08:51 +00:00
|
|
|
eval "$(
|
|
|
|
python3 << 'END_OF_LINE'
|
2020-06-22 18:57:04 +00:00
|
|
|
from pathlib import Path
|
|
|
|
import yaml
|
|
|
|
import sys
|
|
|
|
|
|
|
|
LINEAGE_CONFIG = Path(".github/lineage.yml")
|
|
|
|
|
|
|
|
if not LINEAGE_CONFIG.exists():
|
2020-06-22 21:37:34 +00:00
|
|
|
print("No lineage configuration found.", file=sys.stderr)
|
2020-06-22 18:57:04 +00:00
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
with LINEAGE_CONFIG.open("r") as f:
|
|
|
|
lineage = yaml.safe_load(stream=f)
|
|
|
|
|
|
|
|
if lineage["version"] == "1":
|
|
|
|
for parent_name, v in lineage["lineage"].items():
|
|
|
|
remote_url = v["remote-url"]
|
|
|
|
print(f"git remote add {parent_name} {remote_url};")
|
|
|
|
print(f"git remote set-url --push {parent_name} no_push;")
|
|
|
|
else:
|
|
|
|
print(f'Unsupported lineage version: {lineage["version"]}', file=sys.stderr)
|
|
|
|
END_OF_LINE
|
|
|
|
)"
|
|
|
|
|
|
|
|
# Qapla
|
|
|
|
echo "Success!"
|