dev:code:emulation_of_graphical_display_and_interaction_using_xvfb_and_xdotool [Promethee]

Emulation of graphical display and interaction

This section is dedicated to a useful tip to produce a script that run your code in batch mode on a no-display environment, whenever the code requires a graphical display to produce views of the model for instance.
The following example script was made for the Tripoli code, and is an extract of the Tripoli extended plugin. It is based on the Xvfb and xdotool Linux comands (to be installed previously).
#!/bin/bash
 
# to customize with your own installation path
TRIPOLI_HOME=...
 
...
...
 
# convert EOL from windows to unix
FICH=`basename $1`
mv -f $FICH $FICH.orig
tr -d '\r' < $FICH.orig > $FICH
 
# to check if graf is asked. In this case, uses Xvfb and xdotool to get .ps and continue calculation.
# need packages Xvfb, xdotool, x11-xkb-utils. Xvfb and xdotool to be in PATH
GRAF=`grep GRAF * | wc -l`
 
PROG=$TRIPOLI_HOME/CODE/bin/$TRIPOLI_ARCH/static_tripoli4
 
if [ ! "$GRAF" == "0" ]; then
  for i in `seq 1 10`; do if [ ! -e /tmp/.X$i-lock ]; then NDISPLAY=$i; break; fi done
  Xvfb :$NDISPLAY &
  PID_XVFB=$!
  echo $PID_XVFB >> PID
  echo "export DISPLAY=:"$NDISPLAY".0"
  export DISPLAY=:$NDISPLAY".0"
fi
 
$PROG -d $FICH -s $SOPTION -c $COPTION -o $FICH.listing > $FICH.out &
PID_TRIPOLI=$!
# allows Promethee to kill Tripoli when needed
echo $PID_TRIPOLI >> PID
 
if [ ! "$GRAF" == "0" ]; then
  PRINTED_GRAF=`grep "checking association" $FICH.listing`
  PRINTED_ERROR=`grep "error" $FICH.out``grep "entry not found" $FICH.out`
  while [ "$PRINTED_GRAF" == "" ] && [ "$PRINTED_ERROR" == "" ]
  do
    sleep 3
    echo "Return"
    xdotool key Return # virtually press "Return" key to continue on GRAF loop
    PRINTED_GRAF=`grep "checking association" $FICH.listing`
    PRINTED_ERROR=`grep "error" $FICH.out``grep "entry not found" $FICH.out`
  done
 
  kill -2 $PID_XVFB
  rm -f /tmp/.X$NDISPLAY-lock
fi
 
wait $PID_TRIPOLI
 
# restore initial input file
mv -f $FICH.orig $FICH
 
rm -f PID
 
PRINTED_ERROR=`grep "error" $FICH.out``grep "entry not found" $FICH.out`
if [ ! "$PRINTED_ERROR" == "" ]; then
		echo "Calculation failed. exiting."
		#echo " simulation time -1" >> $FICH.listing # turn around no longer needed in 1.1
    	exit 0
fi
© IRSN - All right reserved - Legal information