blob: 8ede1f64c4f422b92fa4bb3f0b63459268324bfc [file] [log] [blame] [edit]
#!/bin/bash
# Get the directory of this script
SCRIPT_DIR="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
install_virtual_environment_doc() {
echo "Please install the virtual environment before running format.sh by running"
echo "the following commands:"
echo ""
echo " cd $SCRIPT_DIR"
echo " python3 -m venv .venv"
echo " (source .venv/bin/activate && pip install -e \".[dev]\")"
}
if [ -f "$SCRIPT_DIR/.venv/bin/activate" ] ; then
source "$SCRIPT_DIR/.venv/bin/activate"
else
echo ""
echo "====================="
echo "Error: Virtual environment not installed!"
echo "====================="
echo ""
install_virtual_environment_doc
echo ""
exit 1
fi
# Verify expected virtual environment binaries exist to prevent unintentionally running
# different versions from outside the environment.
#
# Note: The virtual environment may exist without the binaries if dependencies weren't installed
# (e.g., running `python3 -m venv .venv` without `pip install -e '.[dev]'`).
find_venv_binary() {
find .venv/bin -name $1 | grep -q .
}
venv_binaries="autoflake black isort"
all_binaries_found=true
for binary in $venv_binaries; do
if ! find_venv_binary $binary; then
all_binaries_found=false
echo "Error: $binary not installed in virtual environment"
fi
done
if ! $all_binaries_found; then
echo ""
install_virtual_environment_doc
echo ""
exit 1
fi
# Detect trivial unused code.
#
# Automatically removal is possible, but is considered an unsafe operation. When a
# change hasn't been commited, automatic removal could cause unintended irreversible
# loss of in-progress code.
#
# Note: This cannot detect unused code between modules or packages. For complex unused
# code detection, vulture should be used.
autoflake \
--quiet \
--check-diff \
--remove-duplicate-keys \
--remove-unused-variables \
--remove-all-unused-imports \
--recursive .
if [ $? -eq 0 ]; then
echo "No unused code found"
else
echo ""
echo "====================="
echo "Unused code detected!"
echo "====================="
echo ""
echo "If these changes are trivial, consider running:"
echo "\"autoflake --in-place --remove-unused-variables --remove-all-unused-imports -r .\""
echo ""
read -p "Run this command to remove all unused code? [y/n] " -n 1 -r
echo ""
echo ""
if [[ $REPLY =~ ^[Yy]$ ]]; then
autoflake --in-place --remove-unused-variables --remove-all-unused-imports -r .
else
exit 1
fi
fi
# Sort imports to avoid bikeshedding.
isort .
# Format code; also to avoid bikeshedding.
black .