Home | History | Annotate | Download | only in bin
      1 #!/bin/bash
      2 # Usage: tc_pyformat <list of pyformat options> file1.py file2.py ...
      3 #
      4 # Most common option is -i, which makes formatting changes in place.
      5 set -u
      6 
      7 PF=pyformat
      8 PF_OPTIONS="--yapf --force_quote_type=single"
      9 PF_USER_OPTIONS=""
     10 
     11 if [[ -z "$(type -t ${PF})" ]]; then
     12   echo "Error: ${PF} not in your path."
     13   exit 1
     14 fi
     15 
     16 while [[ "$1" == -* ]]; do
     17   PF_USER_OPTIONS+=" $1"
     18   shift
     19 done
     20 
     21 FILES=$*
     22 PF_OPTIONS+=${PF_USER_OPTIONS}
     23 
     24 for f in ${FILES}; do
     25   if [[ $f != *.py ]]; then
     26     echo "Error: File $f is not a python file"
     27     exit 2
     28   elif [[ -x $f ]]; then
     29     ${PF} ${PF_OPTIONS} $f
     30   elif [[ -f $f ]]; then
     31     ${PF} --remove_shebang ${PF_OPTIONS} $f
     32   else
     33     echo "Error: File $f does not exist"
     34     exit 2
     35   fi
     36 done
     37