#!/bin/bash set -e cd "$(dirname "$(readlink "$0")")" sources=($@) if [ "${#sources[@]}" -eq 0 ]; then sources=(src) fi rc=0 run() { # remember last failure if "$@"; then :; else rc=$?; fi } venv_installs=() venv_check() { # make sure a python package is installed in venv (to get latest/matching version) local pkg_file=$1 local package=$2 for path in "${venv_pkgs[@]}"; do if [ -e "${path}/${pkg_file}" ]; then return fi done echo "Missing ${pkg_file} in venv, installing ${package}" venv_installs+=("${package}") } py_check() { # make sure a python package is installed local pkg_file=$1 local package=$2 for path in "${site_pkgs[@]}"; do if [ -e "${path}/${pkg_file}" ]; then return fi done echo "Missing ${pkg_file}, installing ${package}" venv_installs+=("${package}") } venv_load_paths() { local venv_base=$(readlink -f "venv") local venv_base_l=${#venv_base} local abs_path local -a all_pkgs readarray -d '' -t all_pkgs < <(./venv/bin/python3 -c 'import site; print("\0".join(site.getsitepackages()), end="\0")') venv_pkgs=() site_pkgs=() for path in "${all_pkgs[@]}"; do if [ -d "${path}" ]; then site_pkgs+=("${path}") abs_path=$(readlink -f "${path}") if [ "${abs_path::${venv_base_l}}" == "${venv_base}" ]; then venv_pkgs+=("${path}") fi fi done } if [ -d venv ]; then python=venv/bin/python3 venv_load_paths py_check "pycodestyle.py" "pycodestyle" py_check "flake8" "flake8" venv_check "mypy" "mypy" venv_check "trio_typing" "trio-typing[mypy]" venv_check "yaml-stubs" "types-PyYAML" venv_check "aiofiles-stubs" "types-aiofiles" venv_check "colorama-stubs" "types-colorama" venv_check "cryptography-stubs" "types-cryptography" venv_check "google-stubs" "types-protobuf" venv_check "toml-stubs" "types-toml" py_check "black" "black" py_check "isort" "isort" if [ "${#venv_installs[@]}" -gt 0 ]; then ./venv/bin/pip install --upgrade "${venv_installs[@]}" fi else python=python3 fi echo "[pycodestyle]" run "${python}" -m pycodestyle --config=.pycodestyle "${sources[@]}" echo "[flake8]" run "${python}" -m flake8 "${sources[@]}" echo "[mypy]" run "${python}" -m mypy "${sources[@]}" echo "[black]" run "${python}" -m black --check "${sources[@]}" echo "[isort]" run "${python}" -m isort --check-only "${sources[@]}" exit $rc