merge_examples.sh 5.33 KB
Newer Older
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
#!/usr/bin/env bash
# Copyright (c) 2014-2015, Siemens AG. All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

#function for printing usage
usage() { 
        echo "This script is used by jenkins, to merge examples from examples_raw";
        echo "into examples. Should not be called manually.";
        echo ""
	echo "Example call (from the scripts directory as working directory):";
        echo "$0 -d ../";
        echo "";
        echo "Usage: $0 [-d <root project dir>]" 1>&2; exit 1;
}

#check if all dependencies are fulfilled
for DEPENDENCY in rsync mktemp cd grep cmake echo python realpath mkdir git
do
        command -v $DEPENDENCY >/dev/null 2>&1 || { echo >&2 "This script requires $DEPENDENCY but it's not installed. Exiting."; exit 1; }
done

#get command line options
while getopts "d:vq" o; do
        case "${o}" in
        d)
                d=${OPTARG}
                ;;
        *)
                usage
                ;;
        esac
done
shift $((OPTIND-1))

#used as wrapper, for switching between verbose and normal mode
redirect_cmd() {
        if [ -z "${v}" ]; then
                "$@" > /dev/null 2>&1
        else
                "$@"
        fi
}

#user has to specify directory
if [ -z "${d}" ]; then
        usage
fi

#the specified directory has to exist
if [ ! -d "$d" ]; then
        echo "--> ! Error, directory $d does not exist or is not a directory!" 
        echo "" 
        usage
fi

CMAKEFILE="$d/CMakeLists.txt"

#sanity check, the user specified directory should contain a CMakeLists.txt file.
if [ ! -f "$CMAKEFILE" ]; then
        echo "--> ! Error, could no locate CMakeLists.txt. Perhaps you specified a wrong directory?"
        echo ""
        usage
fi


#temporary directory for building other things (e.g. Latex or integrating snippets into examples)
MYTMPDIR_BUILD=`mktemp -d`

echo "--> Creating temporary directory $MYTMPDIR_BUILD"
#install traps, deleting the temporary directories when exiting
function finish {
rm -rf $MYTMPDIR_BUILD
}

trap finish EXIT

PROJECT_DIR_FULLPATH=`realpath ${d}`

echo "--> Calling rsync to temporary folder ($MYTMPDIR_BUILD)"

#doing a rsync to another temporary folder, which will be used to build things, like e.g. the tutorial pdf.
redirect_cmd rsync \
        --archive --recursive ${d} $MYTMPDIR_BUILD 

echo "--> Integrating Example Snippets"
REMEMBER_CUR_DIR=$(pwd)

EXAMPLES_DIR="$MYTMPDIR_BUILD/doc/examples_raw"
INTEGRATE_SNIPPETS_SCRIPT="insert_snippets.py"
EXAMPLES_TARGET_DIR="$PROJECT_DIR_FULLPATH/doc/examples"

113
if [ -f "$EXAMPLES_DIR/$INTEGRATE_SNIPPETS_SCRIPT" ]; then
114 115 116 117 118 119 120 121 122 123 124 125 126
        cd "$EXAMPLES_DIR"


        echo "---> Calling integrate script"
        python insert_snippets.py 
      
	if [[ $? = 0 ]]; then
    		echo "success"
	else
    		echo "failure: $?"
		exit 1
	fi
 
127
	if [ ! -d "$EXAMPLES_TARGET_DIR" ]; then
128
		echo "---> Examples target dir does not exist. Creating..."
129
		redirect_cmd mkdir "$EXAMPLES_TARGET_DIR"
130 131
	fi

132
        if [ -d "$EXAMPLES_TARGET_DIR" ]; then
133 134 135 136 137 138 139
                echo "---> Copy integrated examples back"
                #The examples have been integrated. Copy the integrated source files back.
                redirect_cmd rsync --delete --archive --recursive "$EXAMPLES_DIR/" "$EXAMPLES_TARGET_DIR/" \
                        --exclude=*snippet.h \
                        --exclude=*fragmented.h \
                        --exclude=*snippet.cc \
                        --exclude=*fragmented.cc \
140 141 142 143 144 145 146
                        --exclude="*$INTEGRATE_SNIPPETS_SCRIPT"
                echo redirect_cmd rsync --delete --archive --recursive "$EXAMPLES_DIR/" "$EXAMPLES_TARGET_DIR/" \
                        --exclude=*snippet.h \
                        --exclude=*fragmented.h \
                        --exclude=*snippet.cc \
                        --exclude=*fragmented.cc \
                        --exclude="*$INTEGRATE_SNIPPETS_SCRIPT"
147 148 149
		# for commiting, we must be in the project dir
		cd "$PROJECT_DIR_FULLPATH"
	
150 151
		redirect_cmd git add -u "$EXAMPLES_TARGET_DIR"
		redirect_cmd git add "$EXAMPLES_TARGET_DIR"
152 153 154 155 156 157 158
		redirect_cmd git commit -m 'Integrating examples_raw to examples using merge_examples.sh script.'
        fi
fi

cd "$REMEMBER_CUR_DIR"

echo "--> Done."