changeset 445:2b09be3fb0f2 prerelease

Re-implemented watch for build_heavy ... added .bela_common file for shared config and utilities
author Giulio Moro <giuliomoro@yahoo.it>
date Sun, 19 Jun 2016 03:28:53 +0100
parents cdf77b9e66bf
children 566bb80c2d14
files scripts/.bela_common scripts/build_pd_heavy.sh scripts/build_project.sh scripts/update_board
diffstat 4 files changed, 77 insertions(+), 45 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/.bela_common	Sun Jun 19 03:28:53 2016 +0100
@@ -0,0 +1,32 @@
+[ -z "$BBB_ADDRESS" ] && BBB_ADDRESS="root@192.168.7.2"
+[ -z "$BBB_BELA_HOME" ] && BBB_BELA_HOME="~/Bela/"
+[ -z "$BBB_SCREEN_NAME" ] && BBB_SCREEN_NAME="Bela"
+[ -z "$RUN_PROJECT" ] && RUN_PROJECT=1
+[ -z "$COMMAND_ARGS" ] && COMMAND_ARGS=
+[ -z "$RUN_IN_FOREGROUND" ] && RUN_IN_FOREGROUND=1
+[ -z "$RUN_WITHOUT_SCREEN" ] && RUN_WITHOUT_SCREEN=0
+[ -z "$BBB_PROJECT_HOME" ] && BBB_PROJECT_HOME="${BBB_BELA_HOME}/projects/"
+[ -z "$BBB_DEFAULT_PROJECT_NAME" ] && BBB_DEFAULT_PROJECT_NAME="scriptUploadedProject"
+[ -z "$BBB_PROJECT_NAME" ] && BBB_PROJECT_NAME=$BBB_DEFAULT_PROJECT_NAME
+
+trap "echo; exit 0;" 2 9
+folder_has_changed(){
+	[ -z "$2" ] && { echo "Error: folder_has_changed(folder, reference, [filter])"; exit 1; }
+	[ -z "$3" ] && FILTER="." || FILTER="$3"
+	find "$1" -type f -newer "$2" | grep "$FILTER"
+	return $?
+}
+
+wait_for_change(){
+	[ -z "$2" ] && { echo "Error: folder_has_changed(folder, reference, [filter])"; exit 1; }
+	[ -z "$4" ] && SLEEP=0.5 || SLEEP="$4"
+	while ! folder_has_changed "$1" "$2" "$3"
+	do
+		sleep $SLEEP;	
+	done
+}
+
+set_date(){
+# an optional parameter will be executed as part of the same ssh session
+	ssh $BBB_ADDRESS "date -s \"`date '+%Y%m%d %T %Z'`\" > /dev/null; $1"
+}
--- a/scripts/build_pd_heavy.sh	Sun Jun 19 01:18:45 2016 +0100
+++ b/scripts/build_pd_heavy.sh	Sun Jun 19 03:28:53 2016 +0100
@@ -8,24 +8,20 @@
 trap "{ echo "";exit 0; }" 2 
 
 workingdir=".."
-verbose="0"
 pdpath=""
 NO_UPLOAD="0"
 WATCH="0"
 FORCE="0"
 #make sure the paths have the trailing / . 
 projectpath="../tmp/heavy/hvtemp/"
-[ -z "$BBB_ADDRESS" ] && BBB_ADDRESS="root@192.168.7.2"
-[ -z "$BBB_BELA_HOME" ] && BBB_BELA_HOME="~/Bela/"
-[ -z "$BBB_PROJECT_HOME" ] && BBB_PROJECT_HOME="${BBB_BELA_HOME}/projects/"
 BBB_DEFAULT_PROJECT_NAME="heavyProject"
-[ -z "$BBB_PROJECT_NAME" ] && BBB_PROJECT_NAME=$BBB_DEFAULT_PROJECT_NAME
 COMMAND_ARGS=
 RUN_PROJECT=1
 RUN_IN_FOREGROUND=0
 RUN_WITHOUT_SCREEN=1
 BELA_PYTHON27=
 
+source .bela_common || exit 1
 if [ -z "$BELA_PYTHON27" ]; then
     for PY in python python2.7 ; do
         python --version 2>&1 | grep "2\.7" >/dev/null 2>&1
@@ -46,11 +42,11 @@
 usage ()
 {
 printf "\nUSAGE: build_pd.sh [[-i input folder containing _main.pd file ]\
- [-o output folder for new heavy project .c files (default $projectpath)]\
+ [-o output folder for temp heavy project .c files (default $projectpath)]\
  [-b bbb path to copy to (default ~/Bela)] | [-h] | [-w|--watch] | [-n|--noupload] | [-r|--release arg]\n"
 printf "\nexample: build_pd.sh -i ../projects/heavy/pd/hello-world -o ../projects/heavy/hello-world\n"
-echo "If --watch is selected, the script will check every 1s for any file that is modified in the source folder, which triggers\
-the building process and runs the process.
+echo "If --watch is selected, the script will check every 1s for any file that is modified in the source folder, which re-triggers\
+the building process.
 If --screen is selected, the prompt returns to the user after launching Bela in a screen on the target device.
 If --screen and --watch are combined, while the process is running in the screen, modifications to the source files will \
 still trigger a new build.
@@ -72,8 +68,6 @@
         -r | --release )        shift
                                 release=$1
                                 ;;
-        -v | --verbose )        verbose=1
-                                ;;
         -s | --screen )         RUN_WITHOUT_SCREEN="0"
                                 ;;
         -w | --watch )          WATCH=1
@@ -105,6 +99,10 @@
 # These files will be cleared from $projectpath before calling uploader.py
 #TODO: get a reliable, exhaustive, up-to-date list.
 HEAVY_FILES='Heavy* Hv* Message* Signal*'
+
+set_date
+reference_time_file="$projectpath"/
+trap "kill -9 "'$BACKGROUND_PROCESS_PID'"; exit 0;" 2 9
 uploadBuildRun(){
     if [ $NO_UPLOAD -eq 0 ]; then
         # remove old static files to avoid obsolete errors
@@ -142,30 +140,34 @@
     CUSTOM_RENDER_SOURCE_PATH="$pdpath/heavy_render.cpp"
     if [ -f "$CUSTOM_RENDER_SOURCE_PATH" ]; then
         echo "Found custom heavy_render.cpp file in input folder, using that one instead of the default one.";
-        cp "$CUSTOM_RENDER_SOURCE_PATH" "$projectpath/"
+        cp "$CUSTOM_RENDER_SOURCE_PATH" "$projectpath/render.cpp"
     else
         echo "Using default heavy_render.cpp"
-        cp "hvresources/heavy_render.cpp" $projectpath/
+        cp "hvresources/heavy_render.cpp" "$projectpath/render.cpp"
     fi
     
     echo "Updating files on board..."
     # HvContext* files tend to hang when transferring with rsync because they are very large and -c checksum takes a lot, I guess
+    
+    touch $reference_time_file
+    # Transfer the files 
     rsync -ac --out-format="   %n" --no-t --delete-after --exclude='HvContext*' --exclude=build --exclude=$BBB_PROJECT_NAME "$projectpath"/ "$BBB_NETWORK_TARGET_FOLDER" &&\
-        { [ $NO_UPLOAD -eq 0 ] && scp "$projectpath"/HvContext* $BBB_NETWORK_TARGET_FOLDER; } ||\
+        { [ $NO_UPLOAD -eq 1 ] || scp "$projectpath"/HvContext* $BBB_NETWORK_TARGET_FOLDER; } ||\
 	{ echo "ERROR: while synchronizing files with the BBB. Is the board connected?"; exit 1; }
 
+    # TODO: rsync should upload a list of modified files, so that the corresponding objects can be deleted
     # TODO: this should be run only when Heavy_bbb.h changes. Otherwise render is recompiled every time for no good reason
-    ssh $BBB_ADDRESS "rm -rf ${BBB_PROJECT_FOLDER}/build/render.*" 
+    #ssh $BBB_ADDRESS "rm -rf ${BBB_PROJECT_FOLDER}/build/render.*" 
 
     #produce a list of files which content has changed (not just the date)
     #TODO: could be made faster (perhaps) by backing up the folder locally instead of bbb
     # UPDATED_FILES=`rsync -naic --log-format="%f" "$projectpath" "$BBB_BELA_HOME"/source | grep -v "\.$"`
     # echo "UPDATEDFILES : $UPDATED_FILES"
     # exit 2
-    # remove old executable and heavy context .o/.d files
-    if [ $NO_UPLOAD -eq 0 ]; then
-        ssh $BBB_ADDRESS "rm -rf "$BBB_PROJECT_FOLDER/$BBB_PROJECT_NAME;
-    fi;
+    # remove old executable
+    #if [ $NO_UPLOAD -eq 0 ]; then
+    #    ssh $BBB_ADDRESS "rm -rf "$BBB_PROJECT_FOLDER/$BBB_PROJECT_NAME;
+    #fi;
     # Make new Bela executable and run
     # It does not look very nice that we type the same things over and over
     # but that is because each line is an ssh session in its own right
@@ -176,16 +178,22 @@
         ssh $BBB_ADDRESS "$MAKE_COMMAND"
     else
         echo "Building and running project..."
-        if [ $RUN_WITHOUT_SCREEN -eq 1 ]
+	echo "WATCH: $WATCH"
+        if [ $WATCH -eq 1 ]
+	then
+	   # try to emulate run_without_screen: run with fifo
+           [ $RUN_WITHOUT_SCREEN -eq 1 ] && { ssh $BBB_ADDRESS "$MAKE_COMMAND runscreenfifo"& BACKGROUND_PROCESS_PID=$!; } ||\
+               { ssh $BBB_ADDRESS "$MAKE_COMMAND runscreen"; }
+        elif [ $RUN_WITHOUT_SCREEN -eq 1 ]
         then
             ssh -t $BBB_ADDRESS "$MAKE_COMMAND run" 
         elif [ $RUN_IN_FOREGROUND -eq 1 ]
         then
             # Run in screen without detaching
             ssh -t $BBB_ADDRESS "$MAKE_COMMAND runscreenfg"
-        else
+	else 
             # Run in screen and detach
-            ssh -t $BBB_ADDRESS "$MAKE_COMMAND runscreen"
+            ssh $BBB_ADDRESS "$MAKE_COMMAND runscreen"
         fi
     fi
 } #uploadBuildRun
@@ -193,17 +201,16 @@
 uploadBuildRun
 
 if [ $WATCH -ne 0 ]; then
-    WAIT_STRING="\rWaiting for changes in $pdpath"
-    while true; do
-        printf "$WAIT_STRING   "
-        sleep 0.3
-        printf "$WAIT_STRING.  "
-        sleep 0.3
-        printf "$WAIT_STRING.. "
-        sleep 0.3
-        printf "$WAIT_STRING..."
-        sleep 0.3
-        uploadBuildRun
-    done;
+	BACK_NO_UPLOAD=$NO_UPLOAD
+	while true
+	do
+		echo "Waiting for changes in $pdpath, or press ctrl-c to terminate"
+		wait_for_change "$pdpath" "$reference_time_file"
+		echo "Files changed"
+		# if .pd files did not change, no point in re-uploading
+		folder_has_changed "$pdpath" "$reference_time_file" "\.pd" &&\
+			NO_UPLOAD=$BACK_NO_UPLOAD || NO_UPLOAD=1
+		uploadBuildRun
+	done
 fi;
 #ssh -t root@192.168.7.2 "kill -s 2 \`pidof heavy_template\` 2>/dev/null; sleep 0.5; rm -f ~/$filename_bbb/Release/source/heavy/HvContext_bbb.? ~/$filename_bbb/Release/heavy_template && make all -C ~/$filename_bbb/Release" &>/dev/null
--- a/scripts/build_project.sh	Sun Jun 19 01:18:45 2016 +0100
+++ b/scripts/build_project.sh	Sun Jun 19 03:28:53 2016 +0100
@@ -5,16 +5,9 @@
 # The source files in this directory are copied to the board and compiled.
 
 # set defaults unless variables are already set
-[ -z "$BBB_ADDRESS" ] && BBB_ADDRESS="root@192.168.7.2"
-[ -z "$BBB_BELA_HOME" ] && BBB_BELA_HOME="~/Bela/"
-[ -z "$BBB_SCREEN_NAME" ] && BBB_SCREEN_NAME="Bela"
-[ -z "$RUN_PROJECT" ] && RUN_PROJECT=1
-[ -z "$COMMAND_ARGS" ] && COMMAND_ARGS=
-[ -z "$RUN_IN_FOREGROUND" ] && RUN_IN_FOREGROUND=1
-[ -z "$RUN_WITHOUT_SCREEN" ] && RUN_WITHOUT_SCREEN=0
-[ -z "$BBB_PROJECT_HOME" ] && BBB_PROJECT_HOME="${BBB_BELA_HOME}/projects/"
-[ -z "$BBB_DEFAULT_PROJECT_NAME" ] && BBB_DEFAULT_PROJECT_NAME="scriptUploadedProject"
-[ -z "$BBB_PROJECT_NAME" ] && BBB_PROJECT_NAME=$BBB_DEFAULT_PROJECT_NAME
+
+[ -f ".bela_common" ] && echo "loading bela_common" || { echo "Error: cannot find .bela_common . Make sure you run the script from within the scripts/ folder"; exit 1; } 
+source .bela_common
 
 usage()
 {
--- a/scripts/update_board	Sun Jun 19 01:18:45 2016 +0100
+++ b/scripts/update_board	Sun Jun 19 03:28:53 2016 +0100
@@ -6,6 +6,7 @@
 
 [ -z "$BBB_ADDRESS" ] && BBB_ADDRESS="root@192.168.7.2"
 [ -z "$BBB_BELA_HOME" ] && BBB_BELA_HOME="/root/Bela/"
+source .bela_common
 
 FILES_TO_COPY="core include Makefile libNE10.a libprussdrv.a examples Doxyfile"
 
@@ -81,8 +82,7 @@
 SCRIPTDIR=$(dirname "$0")
 [ -z "$IDE_FOLDER" ] && IDE_FOLDER=$SCRIPTDIR/../../bela-ide/
 
-
-ssh $BBB_ADDRESS "date -s \"`date '+%Y%m%d %T %Z'`\" > /dev/null; stat $BBB_BELA_HOME &>/dev/null" && DESTINATION_EMPTY=0 || DESTINATION_EMPTY=1
+set_date "stat $BBB_BELA_HOME &>/dev/null" && DESTINATION_EMPTY=0 || DESTINATION_EMPTY=1
 
 if [ $DESTINATION_EMPTY -eq 0 ];
 then