black_format.sh 974 B

12345678910111213141516171819202122232425262728293031323334
  1. #!/usr/bin/env bash
  2. # This script runs black on all Python files in the repo.
  3. set -uo pipefail
  4. # Apply black.
  5. echo -e "Formatting Python files..."
  6. PY_FILES=$(find \( -path "./.git" \
  7. -o -path "./thirdparty" \
  8. \) -prune \
  9. -o \( -name "SConstruct" \
  10. -o -name "SCsub" \
  11. -o -name "*.py" \
  12. \) -print)
  13. black -l 120 $PY_FILES
  14. git diff --color > patch.patch
  15. # If no patch has been generated all is OK, clean up, and exit.
  16. if [ ! -s patch.patch ] ; then
  17. printf "Files in this commit comply with the black style rules.\n"
  18. rm -f patch.patch
  19. exit 0
  20. fi
  21. # A patch has been created, notify the user, clean up, and exit.
  22. printf "\n*** The following differences were found between the code "
  23. printf "and the formatting rules:\n\n"
  24. cat patch.patch
  25. printf "\n*** Aborting, please fix your commit(s) with 'git commit --amend' or 'git rebase -i <hash>'\n"
  26. rm -f patch.patch
  27. exit 1