Skip to content

Instantly share code, notes, and snippets.

@0xAungkon
Last active June 27, 2025 11:05
Show Gist options
  • Select an option

  • Save 0xAungkon/265373ed78d0e2aeaec59d2704453bf2 to your computer and use it in GitHub Desktop.

Select an option

Save 0xAungkon/265373ed78d0e2aeaec59d2704453bf2 to your computer and use it in GitHub Desktop.
A Script To Compile All Python at once
#!/bin/bash
src_dir="$(pwd)"
python3 -m compileall "$src_dir"
# Check if the compilation was successful
if [ $? -ne 0 ]; then
echo "Compilation failed."
exit 1
fi
echo "Compilation successful."
# Remove alla __pycache__ directories
find "$src_dir" -name "*.py" -type f -delete
if [ $? -ne 0 ]; then
echo "Source Code Wiping failed."
exit 1
fi
echo "Source Code Wiping successful."
# Copy .pyc files from __pycache__ to parent dir with proper renaming
find "$src_dir" -type d -name "__pycache__" | while read -r pycache_dir; do
find "$pycache_dir" -maxdepth 1 -name "*.pyc" | while read -r pyc_file; do
filename=$(basename "$pyc_file" | cut -d '.' -f1)
dest_dir=$(dirname "$pycache_dir")
cp "$pyc_file" "$dest_dir/$filename.pyc"
done
done
if [ $? -ne 0 ]; then
echo "Replace Compiled failed."
exit 1
fi
echo "Replace Compiled Successfull."
find "$src_dir" -type d -name "__pycache__" -exec rm -rf {} +
if [ $? -ne 0 ]; then
echo "Warning: Done! __pycache__ directories removal failed."
exit 1
fi
echo "Success: Done! __pycache__ directories removed successfully."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment