Skip to content

Instantly share code, notes, and snippets.

@mohit2152sharma
Last active August 20, 2024 04:28
Show Gist options
  • Save mohit2152sharma/e1437176adb9eafffbe4f5de36ac3c9e to your computer and use it in GitHub Desktop.
Save mohit2152sharma/e1437176adb9eafffbe4f5de36ac3c9e to your computer and use it in GitHub Desktop.
Script to check if the given directory is a python project or not. (Assumes it to be a python project if there exists `requirements.txt` or `pyproject.toml` or any file ending with `.py`
is_python_project() {
local dir=$1
local filename
filename=$(basename "$dir")
# assumes a directory is a python project, if it has files which are usually found in python project root directory like
# requirements.txt, pyproject.toml and setup.py
if [[ -f "$dir"/requirements.txt || -f "$dir"/pyproject.toml || -f "$dir"/setup.py || "$filename" == *.py ]]; then
echo "${dir} is a python project"
return 0
else
available_dirs=$(ls "$dir")
if [[ -n "$available_dirs" ]]; then
for file in "$dir"/*; do
if [[ -d "$file" ]]; then
is_python_project "$file"
fi
done
fi
fi
return 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment