#!/bin/bash # Check if at least one root folder is provided as an argument if [ $# -eq 0 ]; then echo "Usage: $0 [...]" echo "Please provide at least one root folder path." exit 1 fi # Define the protected items list file PROTECTED_LIST="protected.txt" if [ ! -f "$PROTECTED_LIST" ]; then echo "Error: '$PROTECTED_LIST' not found." echo "Please create 'protected.txt' one directory up with a list of protected files/folders (one per line)." exit 1 fi # Safeguard: Check if any file/folder matching patterns in protected.txt exists in a root folder check_protected_items() { local root_dir="$1" while IFS= read -r pattern; do # Skip empty lines [ -z "$pattern" ] && continue # Handle wildcards by using find for pattern matching if [[ "$pattern" == *"*"* ]]; then # Convert pattern to a find-compatible search if [[ "$pattern" == /* ]]; then search_path="${root_dir}${pattern}" else search_path="${root_dir}/${pattern}" fi # Use find to check if any files match the pattern if find "$root_dir" -path "$search_path" 2>/dev/null | grep -q .; then echo "Error: Protected pattern '$pattern' matches files in '$root_dir'. Aborting execution." exit 1 fi else # Exact match for non-wildcard entries if [ -e "$root_dir/$pattern" ]; then echo "Error: Protected item '$pattern' found in '$root_dir'. Aborting execution." exit 1 fi fi done < "$PROTECTED_LIST" } # Function to check and delete subfolders delete_non_manifest_folders() { local dir="$1" # Loop through all immediate subdirectories in the given directory for subfolder in "$dir"/*/ ; do # Check if it's a directory if [ -d "$subfolder" ]; then # Check if __manifest__.py exists in this subfolder if [ ! -f "$subfolder/__manifest__.py" ]; then echo "Deleting '$subfolder' (no __manifest__.py found)" rm -rf "$subfolder" else echo "Keeping '$subfolder' (__manifest__.py found)" fi fi done } # Process each root folder provided as an argument for ROOT_FOLDER in "$@"; do # Check if the root folder exists and is a directory if [ ! -d "$ROOT_FOLDER" ]; then echo "Error: '$ROOT_FOLDER' is not a valid directory. Skipping." continue fi # Perform the safeguard check for this root folder echo "Checking for protected items in '$ROOT_FOLDER' from '$PROTECTED_LIST'..." check_protected_items "$ROOT_FOLDER" # Change to the root folder to handle relative paths cleanly cd "$ROOT_FOLDER" || { echo "Error: Could not change to directory '$ROOT_FOLDER'. Skipping." continue } # Call the function with the current root folder echo "Processing '$ROOT_FOLDER'..." delete_non_manifest_folders "." # Return to the original directory to process the next root folder cd - > /dev/null || { echo "Error: Could not return from '$ROOT_FOLDER'. Exiting." exit 1 } echo "Cleanup complete for '$ROOT_FOLDER'!" done echo "All root folders processed!" exit 0