Home | History | Annotate | Download | only in utils
      1 #!/bin/bash -x
      2 # Utilities to interact with android more easily
      3 
      4 function run_quiet() { eval "$* >/dev/null 2>&1"; }
      5 
      6 make_csv() {
      7 	out=""
      8 	in=$1
      9 	for p in $in; do
     10 		if [ "x$out" == "x" ]; then
     11 			out=$p
     12 		else
     13 			out="$out,$p"
     14 		fi
     15 	done
     16 	echo $out
     17 }
     18 
     19 make_spaces() {
     20 	out=""
     21 	in=$1
     22 	for p in $in; do
     23 		if [ "x$out" == "x" ]; then
     24 			out=$p
     25 		else
     26 			out="$out $p"
     27 		fi
     28 	done
     29 	echo $out
     30 }
     31 
     32 cmd_exists() {
     33 	which $1 > /dev/null
     34 	return $?
     35 }
     36 
     37 do_adb_root() {
     38 	ADB="$1"
     39 	$ADB root > /dev/null 2>&1
     40 	return $?
     41 }
     42 
     43 die() {
     44 	exit_code=$1
     45 	msg=$2
     46 	c_error "$msg"
     47 	exit $exit_code
     48 }
     49 
     50 die_if_no_androdeb() {
     51 	set +e
     52 	$ADB shell ls /data/androdeb/debian > /dev/null 2>&1
     53 	if [ $? -ne 0 ]; then die 8 "Existing androdeb env not found on device. $1"; fi
     54 	set -e
     55 }
     56 
     57 #  Helper function to count number of source arguments in a list
     58 #  when more than one argument is supplied, it is assumed the last argument
     59 #  is a destination
     60 count_sources() {
     61 	local source_count=$#
     62 	if [ $source_count -gt 1 ]; then
     63 		source_count=$((source_count - 1))
     64 	fi
     65 	echo "$source_count"
     66 }
     67 
     68 # Borrowed from project LISA.
     69 ################################################################################
     70 # Logging functions
     71 ################################################################################
     72 c_error() {
     73 	NOW=$(date +"%H:%m:%S")
     74 	# If there is only one parameter, let's assume it's just the message
     75 	if [ $# -gt 1 ]; then
     76 		local parent_lineno="$1"
     77 		local message="$2"
     78 		echo -e "${red}$NOW - ERROR: on or near line ${parent_lineno}: ${message}${nocol}"
     79 		return
     80 	fi
     81 
     82 	local message="$1"
     83 	echo -e "${red}$NOW - ERROR   : ${message}${nocol}"
     84 }
     85 
     86 c_warning() {
     87 	NOW=$(date +"%H:%m:%S")
     88 	# If there is only one parameter, let's assume it's just the message
     89 	if [ $# -gt 1 ]; then
     90 		local parent_lineno="$1"
     91 		local message="$2"
     92 		echo -e "${yellow}$NOW - WARNING: on or near line ${parent_lineno}: ${message}${nocol}"
     93 		return
     94 	fi
     95 	local message="$1"
     96 	echo -e "${yellow}$NOW - WARNING : ${message}${nocol}"
     97 }
     98 
     99 c_info() {
    100 	NOW=$(date +"%H:%m:%S")
    101 	# If there is only one parameter, let's assume it's just the message
    102 	if [ $# -gt 1 ]; then
    103 		local parent_lineno="$1"
    104 		local message="$2"
    105 		echo -e "${blue}$NOW - INFO: on or near line ${parent_lineno}: ${message}${nocol}"
    106 		return
    107 	fi
    108 	local message="$1"
    109 	echo -e "${blue}$NOW - INFO    : ${message}${nocol}"
    110 }
    111 
    112 d_notify() {
    113 	MESSAGE=$1
    114 	ICON=$2
    115 	# Let's try to send a desktop notification,
    116 	# silently fails if there is not support.
    117 	notify-send \
    118 		--icon=$ICON \
    119 		--urgency=critical \
    120 		--expire-time=1500 \
    121 		"Test Series" \
    122 		"$MESSAGE" \
    123 		2>/dev/null
    124 }
    125 
    126 my_tput() {
    127 	if [ "${TERM-dumb}" == dumb ]; then
    128 		return
    129 	fi
    130 	tput $*
    131 }
    132 
    133 box_out()
    134 {
    135 	local s=("$@") b w
    136 	for l in "${s[@]}"; do
    137 		((w<${#l})) && { b="$l"; w="${#l}"; }
    138 	done
    139 	my_tput setaf 3
    140 	echo -e "|-${b//?/-}-|"
    141 	for l in "${s[@]}"; do
    142 		printf '| %s%*s%s |\n' "$(my_tput setaf 4)" "-$w" "$l" "$(my_tput setaf 3)"
    143 		# echo "|-${b//?/-}-|"
    144 	done
    145 	echo "|-${b//?/-}-|"
    146 	my_tput sgr 0
    147 }
    148 
    149 
    150 ################################################################################
    151 # Colors
    152 ################################################################################
    153 
    154 if [ -t 1 ]; then
    155 	ncolors=$(my_tput colors)
    156 	if [ -n "${ncolors}" ] && [ ${ncolors} -ge 8 ]; then
    157 		nocol='\e[0m' # No Color
    158 		white='\e[1;37m'
    159 		black='\e[0;30m'
    160 		blue='\e[0;34m'
    161 		lblue='\e[1;34m'
    162 		green='\e[0;32m'
    163 		lgreen='\e[1;32m'
    164 		cyan='\e[0;36m'
    165 		lcyan='\e[1;36m'
    166 		red='\e[0;31m'
    167 		lred='\e[1;31m'
    168 		purple='\e[0;35m'
    169 		lpurple='\e[1;35m'
    170 		brown='\e[0;33m'
    171 		yellow='\e[1;33m'
    172 		grey='\e[0;30m'
    173 		lgrey='\e[0;37m'
    174 	fi
    175 fi
    176