Return-Path: From: Wang Mingyu Date: Tue, 13 Aug 2019 09:45:22 -0700 Message-ID: <1565714722-44904-1-git-send-email-wangmy@cn.fujitsu.com> MIME-Version: 1.0 Content-Type: text/plain Subject: [Fuego] [PATCH] Optimiz the assert_has_program code. List-Id: Mailing list for the Fuego test framework List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: fuego@lists.linuxfoundation.org It is faster to check the prgoram or file on the target. Usage: before: assert_has_program AAA assert_has_program BBB after: assert_has_program "AAA BBB" Signed-off-by: Wang Mingyu --- scripts/functions.sh | 74 ++++++++++++++++++++++++------------------- scripts/need_check.sh | 13 ++------ 2 files changed, 45 insertions(+), 42 deletions(-) diff --git a/scripts/functions.sh b/scripts/functions.sh index 0fa80b8..d100db3 100755 --- a/scripts/functions.sh +++ b/scripts/functions.sh @@ -977,37 +977,48 @@ function hd_test_clean_umount() { # check for a program or file on the target, and set a variable if it's present # $1 - file, dir or program on target -# $2 - variable to set during the build -# $3 - (optional) set of paths to look for on target -# if $3 is not specified, a find is done from the root -# this requires the 'find' command on the target -function is_on_target { - # FIXTHIS: race condition - tmpfile=$(mktemp /tmp/found_loc.XXXXXX) - cmd "touch $tmpfile" - if [ -z "$3" ] ; then - safe_cmd "find / -name \"$1\" | head -n 1 >$tmpfile" - else - # split search path on colon - for d in $(echo "$3" | tr ":" "\n") ; do - # execute a command on the target to detect $d/$1 - cmd "if [ -z \"\$(cat $tmpfile)\" -a -e \"$d/$1\" ] ; then echo \"$d/$1\" >$tmpfile ; fi" - done - fi - get $tmpfile $tmpfile - LOCATION=$(cat $tmpfile) - export $2=$LOCATION - cmd "rm $tmpfile" - rm -f $tmpfile # -f for tests running on the host +# $2 - tmpfile to save the detect result +function set_cmd_str { + tmpfile=$2 + cmd_str="touch $tmpfile + for prg in \$(echo $1 | tr \" \" \"\\n\") + do + # execute command type on the target to detect \$prg + if type -P \$prg + then + echo \"\$d/\$prg\" >$tmpfile + fi + if [ ! -s $tmpfile ] + then + find / -name \"\$prg\" | head -n 1 >$tmpfile + fi + if [ ! -s $tmpfile ] + then + echo \"\$prg\" >$tmpfile + break; + else + > $tmpfile + fi + done" } -# check for a program or file on a directory listed on the PATH on the target, -# and set a variable if it's present +# check for a program or file on the target, and set a variable if it's present # $1 - file, dir or program on target # $2 - variable to set during the build +# $3 - name of program that is not found on the target function is_on_target_path { - TARGET_PATH=$(cmd "echo \$PATH") - is_on_target $1 $2 $TARGET_PATH + tmpfile=$(mktemp /tmp/found_loc.XXXXXX) + set_cmd_str "$1" $tmpfile + cmd "$cmd_str" + get $tmpfile $tmpfile + if [ -s $tmpfile ] ; then + prg=$(cat $tmpfile) + upName=${prg^^} + export $2=PROGRAM_${upName//[-,.]/_} + export $3=$prg + fi + cmd "rm $tmpfile" + rm -f $tmpfile # -f for tests running on the host } # check for a library on the SDK, and set a variable if it's present @@ -1036,13 +1047,12 @@ function is_on_sdk { # check for a program or file on the target, and send message if the program or file is missing # $1 has the program that is required on the target board -# this has the side effect of defining PROGRAM_$1 (uppercased) -# with the value as the directory where $1 is found on the board. function assert_has_program { - upName=${1^^} - progVar=PROGRAM_${upName//[-,.]/_} - is_on_target_path $1 ${progVar} - assert_define ${progVar} "Missing '$1' program on target board" + is_on_target_path "$1" prog_path prg + if [ ! -z "$prog_path" ] + then + assert_define ${prog_path} "Missing '$prg' program on target board" + fi } # check for a module on the target, and abort if it is missing diff --git a/scripts/need_check.sh b/scripts/need_check.sh index c9bbb75..5912ee1 100755 --- a/scripts/need_check.sh +++ b/scripts/need_check.sh @@ -319,18 +319,11 @@ function check_root { # check if those specified commands exist on board # $1 has single string with a list of command entries to check for function check_program { - # split $1 on whitespace, without file globbing - set -f - arg_array=($1) - set +f - - for prg in "${arg_array[@]}" ; do - is_on_target_path $prg prg_path - if [ -z "$prg_path" ] ; then - echo -e "\n\nABORTED: Expected command \"$prg\" on the target, but it's not there!" + is_on_target_path "$1" prg_path prg + if [ ! -z "$prg_path" ] ; then + echo -e "\n\nABORTED: Expected command \"$prg\" is on the target, but it's not there!" return 1 fi - done # return OK if all necessary commands exist on the target return 0 -- 2.17.1