Home | History | Annotate | Download | only in shell
      1 #
      2 # SPDX-License-Identifier: Apache-2.0
      3 #
      4 # Copyright (C) 2015, ARM Limited and contributors.
      5 #
      6 # Licensed under the Apache License, Version 2.0 (the "License"); you may
      7 # not use this file except in compliance with the License.
      8 # You may obtain a copy of the License at
      9 #
     10 # http://www.apache.org/licenses/LICENSE-2.0
     11 #
     12 # Unless required by applicable law or agreed to in writing, software
     13 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     14 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15 # See the License for the specific language governing permissions and
     16 # limitations under the License.
     17 #
     18 
     19 # LISA Shell: the Linux Integrated System Analysis Shell
     20 
     21 # Setup colors
     22 source src/shell/lisa_colors
     23 
     24 # By default use internal libraries
     25 DEVMODE=${DEVMODE:-1}
     26 
     27 # Get base installation path of LISA
     28 export LISA_HOME="$(pwd)"
     29 
     30 # Run any android scripts before launching
     31 android_pre=$LISA_HOME/src/shell/android-pre.sh
     32 if [ -f $android_pre ]; then
     33 	source $android_pre;
     34 fi
     35 
     36 export PYTHONPATH=''
     37 export PYTHONPATH=$LISA_HOME/libs/utils:$PYTHONPATH
     38 export PYTHONPATH=$LISA_HOME/libs/wlgen:$PYTHONPATH
     39 export PYTHONPATH=$LISA_HOME:$PYTHONPATH
     40 
     41 if [ "x$DEVMODE" == "x1" ]; then
     42 	export PYTHONPATH=$LISA_HOME/libs/devlib:$PYTHONPATH
     43 	export PYTHONPATH=$LISA_HOME/libs/trappy:$PYTHONPATH
     44 	export PYTHONPATH=$LISA_HOME/libs/bart:$PYTHONPATH
     45 fi
     46 
     47 ################################################################################
     48 # Generic LISA Shell commands
     49 ################################################################################
     50 
     51 # LISA Shell On-Line HELP
     52 function lisa-help {
     53 clear
     54 echo -ne '\E[37;42m'
     55 echo "                                                                               "
     56 echo "                    .:: LISA Shell - HELP On-Line ::.                          "
     57 echo "                                                                               "
     58 echo -ne "$LISASHELL_RESET$LISASHELL_GREEN"
     59 cat $LISA_HOME/LisaShell.txt
     60 echo -ne "$LISASHELL_DEFAULT"
     61 }
     62 
     63 function lisa-version {
     64 echo -ne "$LISASHELL_GREEN"
     65 cat <<EOF
     66 
     67 .:: LISA ($(git describe --all))
     68 
     69 Submodules version:
     70 $(git submodule status)
     71 
     72 
     73 EOF
     74 echo -ne "$LISASHELL_DEFAULT"
     75 }
     76 
     77 ################################################################################
     78 # LISA Update utility functions
     79 ################################################################################
     80 
     81 function _lisa-update-usage {
     82     echo "Usage: lisa-update (CMD)"
     83 	echo "  CMD: what to update (default: all)"
     84 	echo "       all - update lisa and all the external dependencies"
     85 	echo "       submodules - update external dependencies provided by submodules"
     86 	echo
     87     echo "Update submodules (if DEVMODE enabled)"
     88 }
     89 
     90 function _lisa-update-submodules {
     91 echo -ne "${LISASHELL_BLUE}"
     92 if [ "x$DEVMODE" == "x1" ]; then
     93 	# Force update existing modules
     94 	echo
     95 	echo 'Developer mode ENABLED, updating local libraries...'
     96 	git submodule sync
     97 	git submodule update --init
     98 	echo 'DONE'
     99 fi
    100 echo -ne "$LISASHELL_DEFAULT"
    101 }
    102 
    103 function _lisa-update-all {
    104 echo -ne "${LISASHELL_BLUE}"
    105 echo -e "*** Update LISA installation"
    106 
    107 git update-index -q --refresh
    108 ret=$?
    109 if [ $ret -ne 0 ]; then
    110     echo "LISA internal error: git update-index failed"
    111     echo "Please report it: https://github.com/ARM-software/lisa/issues"
    112     return $ret
    113 fi
    114 
    115 git diff-index --quiet --ignore-submodules HEAD
    116 ret=$?
    117 if [ $ret -ne 0 ]; then
    118     echo "There are outstanding uncommitted changes."
    119     echo "Please, commit your changes or stash them before you can update lisa"
    120     return $ret
    121 fi
    122 
    123 curr_commit=$(git rev-parse HEAD)
    124 remote_name=$(git remote -v | grep ARM-software/lisa | grep -m 1 fetch | awk '{print $1}')
    125 git merge-base --is-ancestor $curr_commit $remote_name/master
    126 ret=$?
    127 if [ $ret -ne 0 ]; then
    128     echo "You have committed changes that are not part of $remote_name/master"
    129     echo "Please move to the master branch before running lisa-update"
    130     return $ret
    131 fi
    132 
    133 git pull --ff-only $remote_name master
    134 ret=$?
    135 if [ $ret -ne 0 ]; then
    136     # git pull should have printed some error.  Abort and propagate the error code.
    137     return $ret
    138 fi
    139 
    140 _lisa-update-submodules
    141 
    142 echo -ne "$LISASHELL_DEFAULT"
    143 }
    144 
    145 function lisa-update {
    146 CMD=${1:-all}
    147 echo
    148 case "x${CMD^^}" in
    149 'xSUBMODULES')
    150 	_lisa-update-submodules
    151 	;;
    152 'xALL')
    153 	_lisa-update-all
    154 	;;
    155 "xHELP"|*)
    156 	_lisa-update-usage
    157 	;;
    158 esac
    159 echo
    160 echo
    161 }
    162 
    163 ################################################################################
    164 # LISA Notebooks utility functions
    165 ################################################################################
    166 
    167 function _lisa-ipython-usage {
    168     echo "Usage: lisa-ipython CMD [NETIF [PORT]]"
    169 	echo " CMD     - IPython Notebooks command (deafult: start)"
    170 	echo "  start  start the ipython server"
    171 	echo "   stop  stop the ipython server"
    172     echo " NETIF   - the network interface to start the server on (default: lo)"
    173     echo " PORT    - the tcp port for the server (default: 8888)"
    174 }
    175 
    176 function _lisa-ipython-start {
    177 # Get IP address for the specified interface
    178 IPADDR=$(/sbin/ifconfig $NETIF 2>/dev/null  | \
    179 	awk '/inet / {print $2}' | \
    180 	sed 's/addr://')
    181 if [ "x$IPADDR" == "x" ]; then
    182     echo
    183     echo "$NETIF is not a valid network interface"
    184     echo
    185     echo "Usage: $0 <NETIF>"
    186     echo " NETIF - The network interface to start the server on"
    187     echo
    188     return 1
    189 fi
    190 # Setup paths
    191 PYDIR="$LISA_HOME/ipynb"
    192 LOGFILE="$PYDIR/server.log"
    193 PIDFILE="$PYDIR/server.pid"
    194 URLFILE="$PYDIR/server.url"
    195 
    196 # Generate server URL
    197 TOKEN=$(cat /dev/urandom | tr -dc 'a-fA-F0-9' | fold -w 48 | head -n 1)
    198 URL="http://$IPADDR:$PORT/?token=$TOKEN"
    199 
    200 # Check if an instance is already running
    201 if [ -f "$PIDFILE" ] && pgrep -F $PIDFILE >/dev/null; then
    202 	echo "Server already running:"
    203     echo "  " $(cat $URLFILE)
    204     xdg-open $(cat $URLFILE)
    205 	return 1
    206 fi
    207 
    208 # Start the server bindeed to the specified interface
    209 echo
    210 echo 'Notebook server configuration:'
    211 echo '  URL        : ' $URL
    212 echo '  Folder     : ' $PYDIR
    213 echo '  Logfile    : ' $LOGFILE
    214 echo '  PYTHONPATH : '
    215 echo -e "\t${PYTHONPATH//:/\\n\\t}"
    216 cd $PYDIR
    217 echo
    218 echo -n 'Notebook server task: '
    219 if which ipython >/dev/null; then
    220     local cmd=ipython
    221 else
    222     local cmd=jupyter
    223 fi
    224 nohup $cmd notebook --ip=$IPADDR --port=$PORT \
    225                     --NotebookApp.token=$TOKEN \
    226                     >$LOGFILE 2>&1 &
    227 echo $! >$PIDFILE
    228 echo $URL >$URLFILE
    229 cd - >/dev/null
    230 }
    231 
    232 function _lisa-ipython-stop {
    233 PYDIR="$LISA_HOME/ipynb"
    234 PIDFILE="$PYDIR/server.pid"
    235 if [ -f "$PIDFILE" ] && pgrep -F $PIDFILE >/dev/null; then
    236 	kill $(<$PIDFILE) 2>/dev/null
    237 fi
    238 rm -f $PIDFILE 2>/dev/null
    239 }
    240 
    241 function lisa-ipython {
    242 CMD=${1:-start}
    243 
    244 if [ "x$2" == "x" -a $USER == "vagrant" -a -e /vagrant/src/shell/lisa_shell ]; then
    245     # NETIF not set and we are in a vagrant environment.  Default to
    246     # eth0 as loopback won't let you connect from your host machine.
    247     NETIF="eth0"
    248 else
    249     NETIF=${2:-lo}
    250 fi
    251 
    252 PORT=${3:-8888}
    253 echo
    254 case "x${CMD^^}" in
    255 'xSTART')
    256 	echo "Starting IPython Notebooks..."
    257 	_lisa-ipython-start $NETIF $PORT
    258 	;;
    259 'xSTOP')
    260 	echo "Stopping IPython Notebooks..."
    261 	_lisa-ipython-stop
    262 	;;
    263 "xHELP"|*)
    264 	_lisa-ipython-usage
    265 	;;
    266 esac
    267 echo
    268 echo
    269 }
    270 
    271 function lisa-check-submods {
    272 if [ ! -f ./libs/devlib/setup.py ] ||
    273    [ ! -f ./libs/bart/setup.py ] ||
    274    [ ! -f ./libs/trappy/setup.py ]; then
    275 	echo "One or more submodules missing, updating";
    276 	lisa-update submodules
    277 fi
    278 }
    279 
    280 ################################################################################
    281 # LISA Tests utility functions
    282 ################################################################################
    283 
    284 function _lisa-test-usage {
    285 cat <<EOF
    286 Usage: lisa-test [args] FILE[:CLASS]
    287   Run automated tests. Tests can be found under the tests/ directory.
    288 
    289   This is a wrapper for the 'nosetests' utility, additional arguments are passed
    290   to that tool.
    291 
    292   Examples:
    293     Run all EAS Acceptance tests:
    294 
    295       lisa-test tests/eas/acceptance.py
    296 
    297     Run ForkMigration test from EAS Acceptance suite:
    298 
    299       lisa-test tests/eas/acceptance.py:ForkMigration
    300 
    301     Run ForkMigration test from EAS Acceptance suite, generating an XML test
    302     report via nose's XUnit plugin (see nosetests documentation):
    303 
    304       lisa-test --with-xunit --xunit-file=report.xml tests/eas/acceptance.py:ForkMigration
    305 
    306 EOF
    307 }
    308 
    309 function _lisa-test {
    310 nosetests -v --nocapture --nologcapture \
    311           --logging-config=logging.conf \
    312           $*
    313 }
    314 
    315 function lisa-test {
    316 CMD=${1:-help}
    317 echo
    318 case "x${CMD^^}" in
    319 "xHELP")
    320 	_lisa-test-usage
    321 	;;
    322 *)
    323 	_lisa-test $*
    324         local retcode=$?
    325 esac
    326 echo
    327 echo
    328 return $retcode
    329 }
    330 
    331 function lisa-report {
    332 CMD=${1^^}
    333 [ "x$CMD" != "xHELP" ] && CMD=report
    334 echo
    335 case "x${CMD^^}" in
    336 'xREPORT')
    337 	./tools/report.py $*
    338 	;;
    339 "xHELP"|*)
    340 	./tools/report.py --help
    341 	;;
    342 esac
    343 echo
    344 echo
    345 }
    346 
    347 
    348 ################################################################################
    349 # LISA Shell MAIN
    350 ################################################################################
    351 
    352 # Setup Shell variables
    353 PS1="\[${LISASHELL_BLUE}\][LISAShell \[${LISASHELL_LCYAN}\]\W\[${LISASHELL_BLUE}\]] \> \[${LISASHELL_RESET}\]"
    354 
    355 # Dump out a nice LISA Shell logo
    356 clear
    357 
    358 # Android post shell initialization script
    359 android_post=$LISA_HOME/src/shell/android-post.sh
    360 if [ -f $android_post ]; then
    361 	source $android_post;
    362 fi
    363 
    364 echo -e '\E[37;44m'
    365 
    366 echo "                                                                               "
    367 echo "                        .:: LISA Shell ::.                                     "
    368 echo "                                                                               "
    369 echo -ne "$LISASHELL_RESET$LISASHELL_BLUE"
    370 cat <<EOF
    371 
    372 Welcome to the Linux Integrated System Analysis SHELL!
    373 
    374 LISA_HOME  : $LISA_HOME
    375 PYTHONPATH :
    376 EOF
    377 echo -e "\t${PYTHONPATH//:/\\n\\t}"
    378 
    379 if [ "x$DEVMODE" == "x1" ]; then
    380 lisa-check-submods
    381 cat <<EOF
    382 Submodules :
    383 EOF
    384 git submodule status
    385 fi
    386 
    387 cat <<EOF
    388 
    389 
    390 Type "lisa-help" for on-line help on available commands
    391 
    392 EOF
    393 
    394 # Setup default SHELL text color
    395 echo -e "$LISASHELL_DEFAULT"
    396 
    397 # vim: set tabstop=4:
    398