44 lines
2 KiB
Bash
44 lines
2 KiB
Bash
#!/bin/bash
|
|
# Script to fix deprecated ansible.module_utils._text imports in collections
|
|
# This replaces the deprecated imports with the new recommended imports
|
|
|
|
set -e
|
|
|
|
COLLECTIONS_DIR="${HOME}/.ansible/collections/ansible_collections"
|
|
|
|
# Function to fix imports in a file
|
|
fix_imports() {
|
|
local file="$1"
|
|
|
|
# Replace: from ansible.module_utils._text import to_native
|
|
# With: from ansible.module_utils.common.text.converters import to_native
|
|
sed -i '' \
|
|
-e "s/from ansible.module_utils._text import to_native/from ansible.module_utils.common.text.converters import to_native/g" \
|
|
-e "s/from ansible.module_utils._text import to_text/from ansible.module_utils.common.text.converters import to_text/g" \
|
|
-e "s/from ansible.module_utils._text import to_bytes/from ansible.module_utils.common.text.converters import to_bytes/g" \
|
|
-e "s/from ansible.module_utils._text import to_native, to_text/from ansible.module_utils.common.text.converters import to_native, to_text/g" \
|
|
-e "s/from ansible.module_utils._text import to_bytes, to_native/from ansible.module_utils.common.text.converters import to_bytes, to_native/g" \
|
|
-e "s/from ansible.module_utils._text import to_bytes, to_text/from ansible.module_utils.common.text.converters import to_bytes, to_text/g" \
|
|
-e "s/from ansible.module_utils._text import to_native, to_text/from ansible.module_utils.common.text.converters import to_native, to_text/g" \
|
|
"$file"
|
|
}
|
|
|
|
# Find all Python files with deprecated imports
|
|
echo "Searching for files with deprecated imports..."
|
|
FILES=$(grep -r "from ansible.module_utils._text import" "$COLLECTIONS_DIR" --include="*.py" 2>/dev/null | cut -d: -f1 | sort -u)
|
|
|
|
if [ -z "$FILES" ]; then
|
|
echo "No files found with deprecated imports."
|
|
exit 0
|
|
fi
|
|
|
|
echo "Found $(echo "$FILES" | wc -l | tr -d ' ') files to fix."
|
|
|
|
# Fix each file
|
|
for file in $FILES; do
|
|
echo "Fixing: $file"
|
|
fix_imports "$file"
|
|
done
|
|
|
|
echo "Done! Fixed all deprecated imports."
|
|
|