aboutsummaryrefslogtreecommitdiff
path: root/bin/tc_pyformat
blob: 21eceebe5e643679fda16c3caa6f20f34bb2a4bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/bin/bash
# Usage: tc_pyformat <list of pyformat options> file1.py file2.py ...
#
# Most common option is -i, which makes formatting changes in place.
set -u

PF=pyformat
PF_OPTIONS="--yapf --force_quote_type=single"
PF_USER_OPTIONS=""

if [[ -z "$(type -t ${PF})" ]]; then
  echo "Error: ${PF} not in your path."
  exit 1
fi

while [[ "$1" == -* ]]; do
  PF_USER_OPTIONS+=" $1"
  shift
done

FILES=$*
PF_OPTIONS+=${PF_USER_OPTIONS}

for f in ${FILES}; do
  if [[ $f != *.py ]]; then
    echo "Error: File $f is not a python file"
    exit 2
  elif [[ -x $f ]]; then
    ${PF} ${PF_OPTIONS} $f
  elif [[ -f $f ]]; then
    ${PF} --remove_shebang ${PF_OPTIONS} $f
  else
    echo "Error: File $f does not exist"
    exit 2
  fi
done