Skip to content

Instantly share code, notes, and snippets.

@galatolofederico
Created October 10, 2020 12:21
Show Gist options
  • Select an option

  • Save galatolofederico/285a2b0b9d1529d4b3941d0cf5a54e6d to your computer and use it in GitHub Desktop.

Select an option

Save galatolofederico/285a2b0b9d1529d4b3941d0cf5a54e6d to your computer and use it in GitHub Desktop.
Freeze and remove old virtualenvs
#! /bin/bash
# shellcheck source=/dev/null
# Example:
# Freeze and delete virtualenvs folders older than 100 days:
#
# find ./ -maxdepth 1 -type d -mtime +100 -print | ./virtualenvs_cleanup.sh
#
REQUIREMENTS_FILE="requirements.bak.txt"
VIRTUALENV_NAMES=( "env" "venv" )
while IFS= read -r folder; do
for env in "${VIRTUALENV_NAMES[@]}"; do
current_env="${folder:?}/${env:?}"
if [ -d "${current_env}" ]; then
echo "Freezing ${current_env}"
source "${current_env}/bin/activate"
pip freeze > "${folder:?}/${env}_${REQUIREMENTS_FILE}"
deactivate
echo "Deleting ${current_env}"
rm -rf "${current_env}"
fi
done
done < "${1:-/dev/stdin}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment