comparison scripts/bela.sh @ 436:e09c156e04f4 better_scripting

Attempt at unified script for all things bela
author Giulio Moro <giuliomoro@yahoo.it>
date Fri, 17 Jun 2016 18:40:04 +0100
parents
children
comparison
equal deleted inserted replaced
435:cea66c2af560 436:e09c156e04f4
1 #/bin/ksh
2
3 # set defaults unless variables are already set
4 [ -z "$BBB_ADDRESS" ] && BBB_ADDRESS="root@192.168.7.2"
5 [ -z "$BBB_BELA_HOME" ] && BBB_BELA_HOME="~/Bela/"
6 [ -z "$BBB_SCREEN_NAME" ] && BBB_SCREEN_NAME="Bela"
7 [ -z "$RUN_PROJECT" ] && RUN_PROJECT=1
8 [ -z "$COMMAND_ARGS" ] && COMMAND_ARGS=
9 [ -z "$RUN_IN_FOREGROUND" ] && RUN_IN_FOREGROUND=1
10 [ -z "$RUN_WITHOUT_SCREEN" ] && RUN_WITHOUT_SCREEN=0
11 [ -z "$BBB_PROJECT_HOME" ] && BBB_PROJECT_HOME="${BBB_BELA_HOME}/projects/"
12 [ -z "$BBB_DEFAULT_PROJECT_NAME" ] && BBB_DEFAULT_PROJECT_NAME="scriptUploadedProject"
13 [ -z "$BBB_PROJECT_NAME" ] && BBB_PROJECT_NAME=$BBB_DEFAULT_PROJECT_NAME
14 [ -z "$BELA_SAFE_MODE" ] && BELA_SAFE_MODE=1
15 #script folder SF
16 SF="./"
17
18 trap "echo; exit" 2
19 usage(){
20 THIS_SCRIPT=`basename "$0"`
21 echo "Usage: $THIS_SCRIPT $*"
22 [ -z $* ] && SEARCH_STRING="^##bela " || SEARCH_STRING="^##[#]\?bela $*"
23 echo $SEARCH_STRING
24 grep "$SEARCH_STRING" $THIS_SCRIPT | sed s/^###/\ \ \.\\// | sed s/^##/\.\\//
25 }
26 FULL_INVOCATION="$0 $*"
27 MAKE_COMMAND="make --no-print-directory -C \"$BBB_BELA_HOME\""
28
29 beginswith() { case $2 in "$1"*) true;; *) false;; esac; }
30 contains() { case $2 in *"$1"*) true;; *) false;; esac; }
31 count_words(){
32 [ -z "$1" ] && { echo "Count words requires one argument (string)"; exit 1; }
33 _RET1=`echo $1 | wc -w`
34 _RET1="${_RET1#"${_RET1%%[![:space:]]*}"}"
35 return 0
36 }
37 folder_exists(){
38 [ -z $1 ] && { echo "folder_exists requires one argument (folder)"; exit 1; }
39 ls $1 >/dev/null 2>&1
40 return $?
41 }
42 find_project_folder(){
43 [ -z $2 ] && { echo "find_project_folder requires two arguments (projectName, searchPath)"; exit 1; }
44 }
45
46 find_project(){
47 #find_project(projectName, breakFirst) sets globals _RET1 with the local paths and _RET2 with the remote paths and _RET3 with the total number of matches. If breakFirst==1 it puts at most one result in either _RET1 or _RET2 accordingly
48 [ -z $1 ] && { echo "find_project requires one argument (projectName)"; exit 1; }
49 PROJECT=$1; shift
50 [ -z $1 ] && BREAK=0 || { BREAK=$1; shift; }
51
52 PRIORITY="../projects/ ../examples/"
53 LOCALS=
54 for F in "" $PRIORITY
55 do
56 FOLDER="${F}${PROJECT}"
57 folder_exists $FOLDER && { LOCALS="$LOCALS $FOLDER"; [ $BREAK -ne 0 ] && break; }
58 done
59 # do the same thing remotely
60 if [ $BREAK -eq 0 ] || [ -z $LOCALS ]
61 then
62 REMOTES=`ssh $BBB_ADDRESS '
63 REMOTES=
64 PROJECT='$PROJECT'
65 for F in "" ~/Bela/projects/ ~/Bela/examples/
66 do
67 FOLDER=${F}${PROJECT}
68 ls $FOLDER >/dev/null 2>&1 && { REMOTES="$REMOTES $FOLDER";
69 [ '$BREAK' -ne 0 ] && break; }
70 done
71 echo $REMOTES'`
72 fi
73 count_words "$LOCALS $REMOTES" && FOUND=$_RET1
74 echo $FOUND________
75 [ $FOUND -eq 0 ] && return 1
76
77 STR=
78 n=0
79 [ -z "$LOCALS" ] || STR="On your computer: \n"
80 for a in $LOCALS
81 do
82 n=$((n+1))
83 STR="$STR($n) $a\n"
84 done
85 [ -z "$REMOTES" ] || STR="${STR}On Bela: \n"
86 for a in $REMOTES
87 do
88 n=$((n+1))
89 STR="$STR($n) $a\n"
90 done
91
92 _RET1=${LOCALS}
93 _RET2=${REMOTES}
94 _RET3=$FOUND
95 _RET4=$STR
96 return 0
97 }
98
99 sequential(){
100 echo "ABORT"
101 exit 1
102 if [ -z $1 ] || [ -z $1 ];
103 then
104 echo "sequential should be called with two arguments (start, stop)"
105 fi
106 COUNTER=$1; shift
107 STOP=$1; shift
108 _RET1=
109
110 }
111
112 interactive(){
113 #interactive(count=1) waits for user input, validates it and sets returned value in _RET1
114 [ -z $1 ] && COUNT=1 || COUNT=$1
115 STR=
116 [ $COUNT -eq 1 ] && STR="(y/n): " || STR="(enter a number between 1 and $COUNT): "
117 while { printf "$STR"; read REPLY; }
118 do
119 if [ $COUNT -eq 1 ]
120 then
121 case $REPLY in
122 y|Y)
123 REPLY=1
124 break;
125 ;;
126 n|N)
127 REPLY=0
128 break;
129 ;;
130 *)
131 echo "wrong value"
132 continue
133 ;;
134 esac
135 else
136 case $REPLY in
137 #check it is a string
138 ''|*[!0-9]*)
139 continue
140 ;;
141 *)
142 # check range
143 if [ $REPLY -lt 1 ] || [ $REPLY -gt $COUNT ]
144 then
145 continue
146 else
147 break
148 fi
149 ;;
150 esac
151 fi
152 done
153 _RET1=$REPLY
154 return 0
155 }
156 get_one_project(){
157 #getOneProject(projectName) returns true if it finds a project, false otherwise. Fills _RET1 with the project path and _RET2 with the project location (local or remote)
158 [ -z $1 ] && { echo "get_one_project() requires one argument (projectName)"; exit 1; }
159 PROJECT=$1; shift
160
161 if [ $SAFE -eq 0 ];
162 then
163 find_project $PROJECT 1 || return 1
164 else
165 find_project $PROJECT 0 || return 1
166 if [ $_RET3 -ge 1 ] # it is >= 1
167 then
168 LOCALS=$_RET1
169 REMOTES=$_RET2
170 COUNT=$_RET3
171 STR="There are $COUNT projects with this name:\n$_RET4\nWhich one do you want to build? "
172 printf "$STR"
173 interactive $COUNT
174 SELECTED=$_RET1
175 LOCAL_COUNT=`echo $LOCALS | wc -w`
176 [ $SELECTED -le $LOCAL_COUNT ] &&\
177 LOCATION=local||LOCATION=remote
178 set -- $LOCALS $REMOTES
179 eval "PROJECT_PATH=\$${SELECTED}"
180 fi
181 fi
182 _RET1=$PROJECT_PATH
183 _RET2=$LOCATION
184 }
185 copy_project(){
186 printf ""
187 }
188
189 build_project_remote(){
190 printf ""
191 }
192
193 run(){
194 ## run (project, runFolder, runMode, arguments)
195 [ -z $1 ] && { echo "run requires one argument (projectName)"; exit 1; }
196 PROJECT=$1; RUN_FOLDER=$2; MODE=$3; shift 3; CL=$*
197 PROJECT_NAME=`basename $PROJECT`
198 get_one_project $PROJECT || { echo "Could not find a project called \"$PROJECT\", try specifying the full path to it"; exit 1; }
199 PROJECT_PATH=$_RET1
200 LOCATION=$_RET2
201 case $LOCATION in
202 local)
203 echo ${SF}build_project.sh -p $PROJECT_NAME -c "$CL" $MODE $PROJECT_PATH
204 ${SF}build_project.sh -p $PROJECT_NAME -c "$CL" $MODE $PROJECT_PATH
205 ;;
206 remote)
207 ssh -t $BBB_ADDRESS $MAKE_COMMAND FULLPATH=
208 ;;
209 *)
210 echo "Invalid LOCATION"
211 exit 1
212 ;;
213 esac
214 echo PROJECT_PATH=$PROJECT_PATH NAME=$PROJECT_NAME RUN_FOLDER=$RUN_FOLDER MODE=$MODE CL=$CL
215 }
216
217 get_ip(){
218 # get_ip (fullAddress)
219 [ -z $1 ] && { echo "get_ip requires one argument (fullAddress)"; return 1; }
220 _RET1=`echo $1 | sed s/.*@//`
221 }
222
223 check_board_connected(){
224 # Check if the board is alive and set the date
225 get_ip $BBB_ADDRESS && IP=$_RET1 || return 1
226 printf "Checking that the board is running on $IP..."
227 ssh -o ConnectTimeout=5 $BBB_ADDRESS "date -s \"`date '+%Y%m%d %T'`\" > /dev/null" &&\
228 echo ok || { printf "no\nBela does not respond on " get_ip $BBB_ADDRESS", check that the board is connected, up and running and the IP address is correct"; return 1; }
229 }
230
231 #start from here
232 #create a shorthand alias for BELA_SAFE_MODE
233 SAFE=$BELA_SAFE_MODE
234 [ $SAFE -eq 1 ] && check_board_connected || exit
235 OPT=$1
236 shift
237 case $OPT in
238 run)
239 ##bela run ... -- runs the specified project. The project can be stored on the computer or on the board.
240 ###bela run <projectName> <commandLineOptions> -- runs the project in a detachable screen
241 [ -z $1 ] && { usage run; echo "What project do you want to run?"; exit 1; }
242 PROJECT=$1
243 shift
244 RUNMODE=
245 #find if the next argument is a run mode or a command line options
246 case $1 in
247 fg|foreground)
248 ###bela run fg <projectName> <commandLineOptions> -- runs project in the foreground
249 RUNMODE=-f
250 shift
251 ;;
252 bg|background)
253 ###bela run bg <projectName> <commandLineOptions> -- runs project in the background
254 RUNMODE=-b
255 shift
256 ;;
257 esac;
258 run $PROJECT "" "$RUNMODE" $*
259 exit $?
260 ;;
261 heavy)
262 ##bela heavy ... -- runs a Pd patch with heavy
263 ###bela heavy <projectName> <commandLineOptions> -- runs <projectName> with <options> (optional)
264 [ -z $1 ] && { usage $OPT; echo "What project do you want to build with heavy?"; exit 1; }
265 echo TODO
266 exit 1;
267 ;;
268 libpd | pyo)
269 ##bela libpd ... -- runs a Pd patch with libpd
270 ###bela libpd <projectName> <commandLineOptions> -- runs <projectName> with <options> (optional)
271 ##bela pyo ... -- runs a python pyo project
272 ###bela pyo <projectName> <commandLineOptions> -- runs <projectName> with <options> (optional)
273 [ -z $1 ] && { usage $OPT; echo "What project do you want to run with $OPT?"; exit 1; }
274 PD_PROJECT=$1
275 shift
276 run basic_${OPT} $PD_PROJECT "" $*
277 exit $?
278 ;;
279 info)
280 ##bela info <projectName> -- gives info on a project
281 echo TODO
282 exit 1;
283 ;;
284 ide)
285 ##bela ide ... -- controls the bela IDE, which is accessible through a web browser
286 [ -z $1 ] && { usage ide; echo "What do you want to do with the ide?"; exit 1; }
287 OPT=$1
288 shift
289 T=
290 case $OPT in
291 startup)
292 ###bela ide startup -- activates the IDE when the board boots
293 ###bela ide startup no -- disables the IDE at boot
294 [ -z $1 ] && MAKE_TARGET=idestartup ||\
295 { [ no = $1 ] && MAKE_TARGET=idenostartup ||\
296 { usage ide startup; echo "unknown option $1"; exit 1; }
297 }
298 ;;
299 start)
300 ###bela ide start -- starts/restarts the IDE
301 MAKE_TARGET=idestart
302 ;;
303 connect)
304 ###bela ide connect -- connects the terminal to the running IDE session. Useful for debugging
305 T=-t
306 MAKE_TARGET=ideconnect
307 ;;
308 stop)
309 ###bela ide stop -- stops the IDE if it is currently running
310 MAKE_TARGET=idestop
311 ;;
312
313 #TODO: open address
314 *)
315 usage ide; echo "What do you want to do with the ide?"; exit 1
316 esac
317 ssh $T $BBB_ADDRESS $MAKE_COMMAND $MAKE_TARGET
318 exit $?
319 ;;
320 startup)
321 ##bela startup ... -- sets a project to run at boot
322 ###bela startup <projectName> <commandLineOptions> -- sets a project to run at boot
323 ###bela startup restart <projectName> <commandLineOptions> -- sets a project to run at boot and restarts it if it crashes.
324 ###bela startup no -- disables the project at boot
325 # startup can take a projectName (either local or remote) or "no"
326 [ -z $1 ] && { usage startup; echo "What program do you want to run at startup?"; exit 1; }
327 [ $1 = no ] && { ssh -t $BBB_ADDRESS $MAKE_COMMAND nostartup ; exit $?; }
328 TARGET=startup
329 [ $1 = restart ] && { TARGET=startuploop; shift; }
330 PROJECT=$1; shift
331 CL=$*
332 #TODO: check if PROJECT exists remotely, otherwise copy it from here
333 ssh $BBB_ADDRESS $MAKE_COMMAND startup PROJECT=$PROJECT CL="$CL"
334 exit $?
335 ;;
336 stop)
337 ##bela stop -- stops the Bela project that is currently running (if any)
338 ssh $BBB_ADDRESS $MAKE_COMMAND stop
339 exit $?
340 ;;
341 connect)
342 ##bela connect -- connects to the Bela project that is currently running in the background (if any)
343 ssh -t $BBB_ADDRESS $MAKE_COMMAND connect
344 exit $?
345 ;;
346 shutdown)
347 ##bela shutdown -- safely shuts down the board
348 ssh $BBB_ADDRESS halt
349 exit $?
350 ;;
351 date)
352 ##bela date -- sets the date on the board
353 check_board_connected $BBB_ADDRESS &&\
354 { echo "Date set correctly"; exit 0; } ||\
355 { echo "Error while setting the date. Current date on the board is `ssh $BBB_ADDRESS date`"; exit 1; }
356 ;;
357 esac
358 usage
359
360 #bela options projectName
361 #bela update
362 #bela update ide