From 7196909866981c6bb72b2c68c407a96ce4853a14 Mon Sep 17 00:00:00 2001 From: Brandon Rodriguez <brodriguez8774@gmail.com> Date: Tue, 28 Feb 2023 00:33:35 -0500 Subject: [PATCH] Create script --- clear_pycache.sh | 90 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100755 clear_pycache.sh diff --git a/clear_pycache.sh b/clear_pycache.sh new file mode 100755 index 0000000..24293b8 --- /dev/null +++ b/clear_pycache.sh @@ -0,0 +1,90 @@ +#!/usr/bin/env bash +### + # Script to clear all automatically generated PyCache files. + # + # When running code on an older/slower hard drive, it's possible to get errors about PyCache, when using PyTest. + # It seems entirely arbitrary when they pop up, and the easiest way to fix it is just to delete the associated PyCache + # files, so the project regenerates them. + # + # Thus, this script simply iterates through all sub folders and clears all found PyCache files. + ## + + +# Abort on error +set -e + + +# Color Output Variables. +text_reset="\033[0m" +text_blue="\033[0;34m" + + +# Standardize current terminal path so script runs consistently, regardless of original terminal location. +cd "$(dirname "$0")"/.. + + +function main () { + # Get absolute path of current folder. + current_folder="$(cd "$(dirname "${1}")" && pwd)/$(basename "${1}")" + + echo -e "${text_blue}Starting Directory: \"${current_folder}\"${text_reset}" + echo "" + echo "Removing auto-generated files in PyCache directories:" + + # Start with current directory, which should be project root. Pass in absolute path as start point. + iterate_dir ${current_folder} + + echo "" + echo -e "${text_blue}PyCache directories cleared. Terminating script.${text_reset}" +} + + +### + # Iterates through current dir, recursively checking for subdirectories and clearing any PyCache files. + ## +function iterate_dir () { + + # Parse variables. + dir="$(cd "$(dirname "${1}")" && pwd)/$(basename "${1}")" + dir_name=${dir##*/} + + # Change to indicated directory. + cd ${1} + + # Check if PyCache directory. + if [[ "${dir_name}" == "__pycache__" ]] + then + # Is PyCache directory. Check all files within. + echo " ${dir}" + for file in "${dir}/"* + do + # Check if value is actually a file. + if [[ -f "${file}" ]] + then + # Is file. Double check that is PyCache file. It should end in ".pyc". + file_extension=${file##*.} + if [[ "${file_extension}" == "pyc" ]] + then + # Is confirmed PyCache file. These are automatically generated so we can simply delete it. + rm ${file} + fi + fi + done + fi + + # Check for any subdirectories. + for sub_dir in "${dir}/"* + do + # Check if value is actually directory. + if [[ -d ${sub_dir} ]] + then + # Recursively iterate on subdirectory. + iterate_dir ${sub_dir} + fi + done + + cd .. +} + + +main -- GitLab