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