Skip to content

Instantly share code, notes, and snippets.

@jirutka
Created July 27, 2016 17:23
Show Gist options
  • Save jirutka/c5200ddcf00a86603314988fcad9532f to your computer and use it in GitHub Desktop.
Save jirutka/c5200ddcf00a86603314988fcad9532f to your computer and use it in GitHub Desktop.
Python Scripts To Rule Them All ™
#!/bin/sh
# vim: set ts=4:
#
# Ensures that Python 3.4+ is available and installs modules specified
# in requirements-dev.txt.
#
# Environment variables:
# PYTHON : Python executable to use (default is python3 or python on PATH).
#
# This script follows convention https://github.com/github/scripts-to-rule-them-all.
set -eu
cd "$(dirname "$0")/.."
. script/utils.sh
readonly ENV_DIR="$(pwd)/.venv"
if [ ! -f "$ENV_DIR/bin/python3" ]; then
info 'Initializing Python virtual environment...'
# Find Python executable.
for pybin in "${PYTHON:-}" python3 python NOT_FOUND; do
command -v "$pybin" 2>&1 >/dev/null && break
done
if [ "$pybin" = 'NOT_FOUND' ]; then
die 'Could not find python executable! Please install Python 3.5.'
fi
if ! "$pybin" -c 'import sys; exit(not sys.version_info >= (3, 4, 0))'; then
die "Python 3.4+ is required, but you have $("$pybin" -V 2>&1)!"
fi
"$pybin" -m venv "$ENV_DIR"
# For the case when PATH is already set, but venv was empty.
hash -r 2>&1 >/dev/null
fi
if ! is_on_path "$ENV_DIR/bin"; then
warn '! You should add ".venv/bin" to your PATH. Source .envrc" into your !'
warn '! shell, or install direnv or similar tool that will do it for you. !'
. .envrc
fi
info 'Installing Python modules...'
python3 -m pip install --disable-pip-version-check -r requirements-dev.txt 2>&1 \
| sed -e '/^Requirement already satisfied/d' \
-e '/don.t match your environment$/d'
#!/bin/sh
# vim: set ts=4:
#
# Runs linters, tests etc.
#
# This script follows convention https://github.com/github/scripts-to-rule-them-all.
set -u
. "$(dirname "$0")/bootstrap"
info 'Running tests...'
py.test -v
info 'Checking types...'
python3 -m mypy --check-untyped-defs --silent-imports *.py
cat <<-EOF >&2
NOTE: Since we currently don't have stub files for the dependencies
it basically checks only types from the stdlib!
EOF
info 'Running linter...'
python3 -m pycodestyle
# vim: set ts=4:
info() {
# bold cyan
printf '\033[1;36m> %s\033[0m\n' "$@" >&2
}
warn() {
# bold yellow
printf '\033[1;33m> %s\033[0m\n' "$@" >&2
}
die() {
# bold red
printf '\033[1;31mERROR:\033[0m %s\n' "$1" >&2
exit ${2:-2}
}
is_on_path() {
case "$PATH" in
*$1:*) return 0;;
*) return 1;;
esac
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment