Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame^] | 1 | #!/bin/bash |
| 2 | CHECKSTYLE=$1 |
| 3 | FILES=$2 |
| 4 | CONFIG=$3 |
| 5 | SUPPRESSIONS=$4 |
| 6 | |
| 7 | PORT_FILE="$1.port" |
| 8 | |
| 9 | function ppid() { |
| 10 | ps -p ${1:-$$} -o ppid= -o pid= -o comm= |
| 11 | } |
| 12 | |
| 13 | function buck_pid() { |
| 14 | BUCK_PID=($(ppid)) |
| 15 | while [ ${BUCK_PID[0]} -ne 0 ]; do |
| 16 | BUCK_PID=($(ppid $BUCK_PID)) |
| 17 | if [ "${BUCK_PID[2]}" == "buck" ]; then |
| 18 | # use parent PID of buck |
| 19 | echo ${BUCK_PID[0]} |
| 20 | return |
| 21 | fi |
| 22 | if [ "${BUCK_PID[2]}" == "buckd" ] || |
| 23 | [[ "${BUCK_PID[2]}" == *"python"* ]]; then |
| 24 | # use PID of buckd or python |
| 25 | echo ${BUCK_PID[1]} |
| 26 | return |
| 27 | fi |
| 28 | done |
| 29 | # fallback last read PID |
| 30 | echo ${BUCK_PID[1]} |
| 31 | } |
| 32 | |
| 33 | function port() { |
| 34 | cat $PORT_FILE 2>/dev/null || echo 0 |
| 35 | } |
| 36 | |
| 37 | function check_socket() { |
| 38 | nc localhost $(port) < /dev/null 2>/dev/null |
| 39 | return $? |
| 40 | } |
| 41 | |
| 42 | # check to see if checkstyle daemon is running; if not, start it |
| 43 | if ! check_socket; then |
| 44 | # Starting checkstyle server... |
| 45 | #FIXME change to /dev/null if/when we are confident |
| 46 | nohup java -jar $CHECKSTYLE $PORT_FILE $(buck_pid) $3 $4 >>/tmp/checkstyle.daemon 2>&1 & |
| 47 | |
| 48 | TRIES=20 |
| 49 | i=0 |
| 50 | # Wait for checkstyle server to start for 2 seconds |
| 51 | while [ $i -lt $TRIES ]; do |
| 52 | if check_socket; then |
| 53 | CONNECTED=true |
| 54 | break |
| 55 | fi |
| 56 | let i=i+1 |
| 57 | sleep 0.1 |
| 58 | done |
| 59 | if [ -z "$CONNECTED" ]; then |
| 60 | echo "Failed to start checkstyle server" |
| 61 | exit 3 |
| 62 | fi |
| 63 | fi |
| 64 | |
| 65 | # run the actual checkstyle client |
| 66 | OUT=$(cat $FILES | nc localhost $(port)) |
| 67 | if [ $? -ne 0 ]; then |
| 68 | echo "Error connecting to checkstyle server" |
| 69 | exit 2 |
| 70 | fi |
| 71 | if [ -n "$OUT" ]; then |
| 72 | printf "$OUT" |
| 73 | exit 1 |
| 74 | fi |