Created
April 4, 2025 12:37
-
-
Save Elijas/ce4480d070d4726fe7eb1863d1afd58d to your computer and use it in GitHub Desktop.
Script to add __init__.py files to directories with Python files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Script to add __init__.py files to directories with Python files | |
# Usage: ./add_init_py.sh [<folder_path> ...] | |
# Function to get absolute path, works on both Linux and macOS | |
get_abs_path() { | |
local path="$1" | |
if command -v readlink >/dev/null 2>&1 && readlink -f "$path" >/dev/null 2>&1; then | |
readlink -f "$path" | |
else | |
# Fallback for macOS | |
( cd "$(dirname "$path")" >/dev/null 2>&1 && echo "$(pwd)/$(basename "$path")" ) | |
fi | |
} | |
# Set default values if no arguments are provided | |
TARGET_DIRS=("$@") | |
if [ ${#TARGET_DIRS[@]} -eq 0 ]; then | |
TARGET_DIRS=(".") | |
fi | |
# Process each directory provided as argument or default | |
for TARGET_DIR in "${TARGET_DIRS[@]}"; do | |
if [ ! -d "$TARGET_DIR" ]; then | |
echo "Error: '$TARGET_DIR' is not a directory" | |
continue | |
fi | |
# Make TARGET_DIR absolute path for comparison later | |
TARGET_DIR=$(get_abs_path "$TARGET_DIR") | |
# Check if there are any Python files | |
if ! find "$TARGET_DIR" -name "*.py" | grep -q .; then | |
echo "No Python files found in $TARGET_DIR or its subdirectories" | |
continue | |
fi | |
# Since we found Python files, ensure TARGET_DIR gets __init__.py | |
if [ ! -f "$TARGET_DIR/__init__.py" ]; then | |
echo "Adding __init__.py to target directory $TARGET_DIR" | |
touch "$TARGET_DIR/__init__.py" | |
fi | |
# Find all directories that contain .py files but don't have __init__.py | |
find "$TARGET_DIR" -type d ! -path "*/\.*" ! -path "*/__pycache__*" ! -path "*/venv*" | while read dir; do | |
# Check if directory contains Python files | |
if ls "$dir"/*.py >/dev/null 2>&1; then | |
# Make the directory absolute for comparison | |
abs_dir=$(get_abs_path "$dir") | |
# Check if __init__.py doesn't exist in the current directory | |
if [ ! -f "$abs_dir/__init__.py" ]; then | |
echo "Adding __init__.py to $abs_dir" | |
touch "$abs_dir/__init__.py" | |
fi | |
# Skip if this directory is already TARGET_DIR | |
if [ "$abs_dir" = "$TARGET_DIR" ]; then | |
continue | |
fi | |
# Now add __init__.py to all parent directories up to TARGET_DIR | |
current_dir="$abs_dir" | |
while [ "$current_dir" != "$TARGET_DIR" ] && [ "$current_dir" != "/" ]; do | |
parent_dir=$(dirname "$current_dir") | |
if [ "$parent_dir" = "$current_dir" ]; then | |
# We've reached the root or some error occurred | |
echo "Error: Failed to find parent directory of $current_dir, stopping traversal" | |
break | |
fi | |
current_dir="$parent_dir" | |
# Add __init__.py to parent directory if it doesn't exist | |
if [ ! -f "$current_dir/__init__.py" ]; then | |
echo "Adding __init__.py to parent directory $current_dir" | |
touch "$current_dir/__init__.py" | |
fi | |
done | |
fi | |
done | |
done | |
echo "Done!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
bash -c "$(curl -fsSL https://gist.githubusercontent.com/Elijas/ce4480d070d4726fe7eb1863d1afd58d/raw/657b0bfea2b660ee3b0938c79f53418a03229cf3/init_generator.sh)"