blob: 764f7d99ec4b78e2e4a7966f1d5869d268993ccd [file] [log] [blame]
Brian O'Connorbe95f682016-05-18 15:40:19 -07001#!/bin/bash
2CHECKSTYLE=$1
3FILES=$2
4CONFIG=$3
5SUPPRESSIONS=$4
6
7PORT_FILE="$1.port"
8
9function ppid() {
10 ps -p ${1:-$$} -o ppid= -o pid= -o comm=
11}
12
13function 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
33function port() {
34 cat $PORT_FILE 2>/dev/null || echo 0
35}
36
37function 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
43if ! 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
63fi
64
65# run the actual checkstyle client
66OUT=$(cat $FILES | nc localhost $(port))
67if [ $? -ne 0 ]; then
68 echo "Error connecting to checkstyle server"
69 exit 2
70fi
71if [ -n "$OUT" ]; then
72 printf "$OUT"
73 exit 1
74fi