create_tarball_and_zip.sh 14.9 KB
Newer Older
1
#!/usr/bin/env bash
2
# Copyright (c) 2014-2016, Siemens AG. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
# 
# 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.

26
#function for printing usage
27
usage() { 
28
        echo "Create a tarball and a zip of the project. Specify the project root with the";
29 30 31 32 33 34 35 36 37 38 39
        echo "-d parameter. Optionally, specify the -v switch to get verbose output";
        echo "and/or the -q switch, for non-interactive (without user inputs)";
        echo "processing.";
        echo "";
        echo "Version number and project name is automatically derived from CMakeLists.txt";
        echo "in the project's root.";
        echo "Tarball name: [PROJECT_NAME]_[VERSION_NUMBER].tar.gz";
        echo "Example call (from the scripts directory as working directory):";
        echo "$0 -d ../";
        echo "";
        echo "Usage: $0 [-d <root project dir>] [-v] [-q]" 1>&2; exit 1;
40 41
}

42 43 44 45 46
#check if all dependencies are fulfilled
for DEPENDENCY in rsync pdflatex bibtex cp tar mktemp cd grep cmake find file echo python realpath sed
do
        command -v $DEPENDENCY >/dev/null 2>&1 || { echo >&2 "This script requires $DEPENDENCY but it's not installed. Exiting."; exit 1; }
done
47

48 49 50
#get command line options
while getopts "d:vq" o; do
        case "${o}" in
51
        d)
52 53 54 55 56 57 58 59
                d=${OPTARG}
                ;;
        v)
                v=1
                ;;
        q)
                q=1
                ;;
60
        *)
61 62 63
                usage
                ;;
        esac
64 65 66
done
shift $((OPTIND-1))

67 68 69 70 71 72 73 74 75 76
#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
77
if [ -z "${d}" ]; then
78
        usage
79 80
fi

81
#the specified directory has to exist
82
if [ ! -d "$d" ]; then
83 84 85
        echo "--> ! Error, directory $d does not exist or is not a directory!" 
        echo "" 
        usage
86 87 88 89 90
fi

CMAKEFILE="$d/CMakeLists.txt"

if [ ! -f "$CMAKEFILE" ]; then
91 92 93
        echo "--> ! Error, could no locate CMakeLists.txt"
        echo ""
        usage
94 95 96 97 98 99 100
fi

#derive version number from cmake script
VERSION_MAJOR=`cat $CMAKEFILE | grep EMBB_BASE_VERSION_MAJOR | sed "s/^[^0-9]*\([0-9]\+\)[^0-9]*$/\1/g"`
VERSION_MINOR=`cat $CMAKEFILE | grep EMBB_BASE_VERSION_MINOR | sed "s/^[^0-9]*\([0-9]\+\)[^0-9]*$/\1/g"`
VERSION_PATCH=`cat $CMAKEFILE | grep EMBB_BASE_VERSION_PATCH | sed "s/^[^0-9]*\([0-9]\+\)[^0-9]*$/\1/g"`
VERSION_NUMBER="${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}"
101 102

#generate tarball name
103 104 105
PROJECT_NAME=`cat $CMAKEFILE | grep project.*\(.*\) | sed "s/^[^(]*(\([^)]*\)).*$/\1/g" | tr '[:upper:]' '[:lower:]'`
n="${PROJECT_NAME}-${VERSION_NUMBER}"
TARBALL_NAME="${n}.tar.gz"
106
ZIP_NAME="${n}.zip"
107

108 109 110 111 112
#booleans for checking what has to be created
CREATE_TARBALL=true;
CREATE_ZIP=true;

#verify that tarball name doesn't contain forbidden characters
113
if ! [[ $TARBALL_NAME =~ ^[a-zA-Z0-9|\.|\_|-]+$ ]]; then
114 115
        echo "--> Want to create tarball with name $TARBALL_NAME." >&2
        echo '--> ! Filename not valid, only a-z, A-Z, .,- and _ characters are allowed' >&2 # write to stderr
116 117 118 119 120 121 122 123
        CREATE_TARBALL=false
fi

#verify that zip name doesn't contain forbidden characters
if ! [[ $ZIP_NAME =~ ^[a-zA-Z0-9|\.|\_|-]+$ ]]; then
        echo "--> Want to create zip with name $TARBALL_NAME." >&2
        echo '--> ! Filename not valid, only a-z, A-Z, .,- and _ characters are allowed' >&2 # write to stderr
        CREATE_ZIP=false
124 125
fi

126 127 128 129 130 131
if [ -z "${q}" ]; then
        #in interactive mode, ask the user if the tarball shall be created with this filename
        echo "--> Do you wish to create a tarball with the name $TARBALL_NAME in the current directory?"
        select yn in "Yes" "No"; do
                case $yn in
                Yes ) break;;
132
                No ) echo "Leaving tarball creation"; CREATE_TARBALL=false; break;;
133 134 135 136 137
                esac
        done
else
        echo "--> Tarball with name $TARBALL_NAME will be created."
fi
138

139 140 141

#check if file with the tarball_name already exists. In interactive mode, ask the user if this file shall be deleted. Otherwise do not create tarball.
if [ \( -f "$TARBALL_NAME" \) -a \( "$CREATE_TARBALL" = true \) ]; then
142 143 144 145 146
        if [ -z "${q}" ]; then
                echo "--> File $TARBALL_NAME exists. Delete file?"
                select yn in "Yes" "No"; do
                        case $yn in
                        Yes ) break;;
147
                        No ) echo "Leaving tarball creation"; CREATE_TARBALL=false; break;;
148 149
                        esac
                done
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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
		if [ "$CREATE_TARBALL" = true ] ; then
			rm $TARBALL_NAME
                        if [ -f "$TARBALL_NAME" ]; then
                        	echo "Could not delete $TARBALL_NAME"
                        	CREATE_TARBALL=false
                	fi
		fi
        else
                echo "--> ! File $TARBALL_NAME exists. Delete first. Exiting tarball creation."
                CREATE_TARBALL=false;
        fi
fi

if [ -z "${q}" ]; then
        #in interactive mode, ask the user if the zip shall be created with this filename
        echo "--> Do you wish to create a zip with the name $ZIP_NAME in the current directory?"
        select yn in "Yes" "No"; do
                case $yn in
                Yes ) break;;
                No ) echo "Leaving zip creation"; CREATE_ZIP=false; break;;
                esac
        done
else
        echo "--> Zip with name $ZIP_NAME will be created."
fi

#check if file with the zip_name already exists. In interactive mode, ask the user if this file shall be deleted. Otherwise do not create zip.
if [ \( -f "$ZIP_NAME" \) -a \( "$CREATE_ZIP" = true \) ]; then
        if [ -z "${q}" ]; then
                echo "--> File $ZIP_NAME exists. Delete file?"
                select yn in "Yes" "No"; do
                        case $yn in
                        Yes ) break;;
                        No ) echo "Leaving zip creation"; CREATE_ZIP=false; break;
                        esac
                done
		if [ "$CREATE_ZIP" = true ]; then
                	rm $ZIP_NAME
			if [ -f "$ZIP_NAME" ]; then
                        	echo "Could not delete $ZIP_NAME"
                        	CREATE_ZIP=false
                	fi
192 193
                fi
        else
194 195
                echo "--> ! File $ZIP_NAME exists. Delete first. Exiting zip creation."
		CREATE_ZIP=false
196
        fi
197 198
fi

199 200 201 202 203 204 205
#exit if tarball and zip must not / cannot be created
if [ \( "$CREATE_ZIP" = false \) -a \( "$CREATE_TARBALL" = false \) ] ; then
	echo "Exiting, no files to generate";
	exit 1;
fi


206 207 208 209 210
#temporary directory for doxygen
MYTMPDIR_DOXY_BUILD=`mktemp -d`
#temporary directory for building other things (e.g. Latex or integrating snippets into examples)
MYTMPDIR_BUILD=`mktemp -d`
#temporary target directory, from this the tarball will be created
211
MYTMPDIR=`mktemp -d`
212 213 214

echo "--> Creating temporary directories $MYTMPDIR $MYTMPDIR_BUILD $MYTMPDIR_DOXY_BUILD"
#install traps, deleting the temporary directories when exiting
215 216 217 218 219 220 221
function finish {
rm -rf $MYTMPDIR
rm -rf $MYTMPDIR_DOXY_BUILD
rm -rf $MYTMPDIR_BUILD
}

trap finish EXIT
222 223 224

PROJECT_DIR_FULLPATH=`realpath ${d}`

225

226 227 228
echo "--> Generating Doxygen"

REMEMBER_CUR_DIR=$(pwd)
229
cd "$MYTMPDIR_DOXY_BUILD"
230
echo "---> Initialize CMake"
231
redirect_cmd cmake "$PROJECT_DIR_FULLPATH" 
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
echo "---> Call CMake with target Doxygen"
redirect_cmd cmake --build . --target doxygen
REFMAN_TEXFILE="$MYTMPDIR_DOXY_BUILD/latex/refman.tex"
DO_CREATE_LATEXDOC=true

if [ ! -f "$REFMAN_TEXFILE" ]; then
        echo "---> ! Could not find doxygen tex source $REFMAN_TEXFILE. Leaving tarball creation."
        exit 1;
fi

#to resolve all references, pdf and bibtex have to be run more than once. With 4 runs, we should get everything right.
PDFRUNS=4

echo "---> Build Doxygen PDF reference"


if [ "$DO_CREATE_LATEXDOC" = true ] ; then
        cd "$MYTMPDIR_DOXY_BUILD/latex"
        for ((i=1; i<=$PDFRUNS; i++)); do
                echo "----> LaTeX Run ($i/$PDFRUNS)"
                redirect_cmd pdflatex refman.tex  
                redirect_cmd bibtex refman  
        done
fi


258
cd "$REMEMBER_CUR_DIR"
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302

echo "--> Calling rsync to temporary folder 1/2 ($MYTMPDIR)"

#this is the rsync, to the folder from which the tarball will be created later. Exclude everything, that should not be in the tarball. Also exclude things, that are generated somewhere else, like examples.
redirect_cmd rsync \
        --exclude ".git" \
        --exclude ".gitignore" \
        --exclude ".gitattributes" \
        --exclude "build*/" \
        --exclude "scripts/*.tar.gz" \
        --exclude "scripts/cpplint.py" \
        --exclude "scripts/create_tarball.sh" \
        --exclude "scripts/insert_license.sh" \
        --exclude "scripts/license.*" \
        --exclude "scripts/license_*" \
        --exclude "scripts/remove_license.sh" \
        --exclude "mtapi/MTAPI.mm" \
        --exclude ".cproject" \
        --exclude ".gitattributes" \
        --exclude ".project" \
        --exclude "*.blg" \
        --exclude "*.fls" \
        --exclude "*.bbl" \
        --exclude "*.fdb_latexmk" \
        --exclude "*.log" \
        --exclude "*.out" \
        --exclude "*.toc" \
        --exclude "*.aux" \
        --exclude "doc/tutorial/sty" \
        --exclude "doc/tutorial/pics" \
        --exclude "doc/tutorial/content" \
        --exclude "doc/tutorial/*.tex" \
        --exclude "doc/tutorial/*.bib" \
        --exclude "doc/reference/*.xml" \
        --exclude "doc/reference/*.dox" \
        --exclude "doc/reference/*.in" \
        --exclude "doc/reference/header.html" \
        --exclude "doc/reference/*.css" \
        --exclude "doc/examples" \
        --exclude "doc/examples/insert_snippets.py" \
        --exclude ".travis.yml" \
        --archive --recursive ${d} $MYTMPDIR/${n} 

echo "--> Replace version number in README"
303 304 305

README_FILE="$MYTMPDIR/${n}/README.md"

306
#replace version number in readme
307
if [ -f $README_FILE ]; then
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
        sed -i "s/\[VERSION_NUMBER_TEMPLATE\]/$VERSION_NUMBER/g" $README_FILE
fi

echo "--> Calling rsync to temporary folder 2/2 ($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 "--> Generating Tutorial PDF"
TUTORIAL_TEX_DIR="$MYTMPDIR_BUILD/doc/tutorial"
REMEMBER_CUR_DIR=$(pwd)
TUTORIAL_PDF_SOURCE="$TUTORIAL_TEX_DIR/tutorial.pdf"
TUTORIAL_PDF_TARGET="$MYTMPDIR/${n}/doc/tutorial/tutorial.pdf"

if [ -f "$TUTORIAL_TEX_DIR/tutorial.tex" ]; then

325
        cd "$TUTORIAL_TEX_DIR"	
326 327 328 329 330 331 332 333 334 335
        for ((i=1; i<=$PDFRUNS; i++)); do

                echo "---> LaTeX Run ($i/$PDFRUNS)"
                redirect_cmd pdflatex tutorial.tex  
                redirect_cmd bibtex tutorial  
        done
        if [ -f "$TUTORIAL_PDF_SOURCE" ]; then
                cp $TUTORIAL_PDF_SOURCE $TUTORIAL_PDF_TARGET
        fi
fi
336
cd "$REMEMBER_CUR_DIR"
337 338 339 340 341 342 343

REFMAN_TARGET="$MYTMPDIR/${n}/doc/reference/reference.pdf"
REFMAN_SOURCE="$MYTMPDIR_DOXY_BUILD/latex/refman.pdf"

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

344
EXAMPLES_DIR="$MYTMPDIR_BUILD/doc/examples"
345
INTEGRATE_SNIPPETS_SCRIPT="insert_snippets.py"
346
EXAMPLES_TARGET_DIR="$MYTMPDIR/${n}/doc/"
347 348

if [ -f $EXAMPLES_DIR/$INTEGRATE_SNIPPETS_SCRIPT ]; then
349
        cd "$EXAMPLES_DIR"
350 351 352 353 354 355 356 357


        echo "---> Calling integrate script"
        redirect_cmd python insert_snippets.py 

        if [ -d $EXAMPLES_TARGET_DIR ]; then
                echo "---> Copy integrated examples back"
                #The examples have been integrated. Copy the integrated source files.
358
                redirect_cmd rsync --archive --recursive $EXAMPLES_DIR $EXAMPLES_TARGET_DIR \
359 360 361 362 363 364
                        --exclude=*snippet.h \
                        --exclude=*fragmented.h \
                        --exclude=*snippet.cc \
                        --exclude=*fragmented.cc \
                        --exclude=*$INTEGRATE_SNIPPETS_SCRIPT 
        fi
365 366
fi

367
cd "$REMEMBER_CUR_DIR"
368 369 370 371 372 373 374 375

echo "--> Copy reference manual"
if [ -f $REFMAN_SOURCE ]; then
        cp $REFMAN_SOURCE $REFMAN_TARGET
else
        echo "--> ! Could not find doxygen pdf document $REFMAN_SOURCE. Exiting"
        exit 1;
fi
376

377 378 379 380 381 382 383 384 385 386
if [ -d $MYTMPDIR_DOXY_BUILD/html ]; then
        redirect_cmd rsync --archive --recursive $MYTMPDIR_DOXY_BUILD/html/ $MYTMPDIR/${n}/doc/reference/doxygen_html_generated
else
        echo "Doxygen HTML was not generated. Tarball will not contain HTML reference documentation. Exiting."
        exit 1;
fi

echo "--> Checking line endings"

#check for files, that have windows file endings. Those are forbidden.
387 388 389
WINLINES=`find $MYTMPDIR/${n} -not -type d -exec file "{}" ";" | grep CRLF`

if [ -n "$WINLINES" ]; then
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
        echo "Detected Dos line endings in following files:"
        echo "$WINLINES"
        echo "Error: The project guidelines forbid Dos line endings. Exiting."
        exit 1;
fi

#sanity check... verify, that expected targets are there, otherwise abort...
if ! [ -f $MYTMPDIR/${n}/doc/examples/main.cc ]; then
        echo "--> ! Examples missing. Exiting."
        exit 1;
fi

if ! [ -f $MYTMPDIR/${n}/doc/tutorial/tutorial.pdf ]; then
        echo "--> ! Tutorial PDF missing. Exiting."
        exit 1;
fi

if ! [ -f $MYTMPDIR/${n}/doc/reference/reference.pdf ]; then
        echo "--> ! Reference PDF documentation missing. Exiting."
        exit 1;
fi

if ! [ -f $MYTMPDIR/${n}/doc/reference/doxygen_html_generated/index.html ]; then
        echo "--> ! Reference HTML documentation missing. Exiting."
        exit 1;
fi

417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
#build the tarball, if CREATE_TARBALL is true.
if [ "$CREATE_TARBALL" = true ]; then
	echo "--> Calling tar"
	tar -czf $TARBALL_NAME -C $MYTMPDIR ${n}
	echo "--> Done. Created $TARBALL_NAME."
fi

#build the zip, if CREATE_ZIP is true
if [ "$CREATE_ZIP" = true ]; then
	echo "--> Calling zip"
	cd $MYTMPDIR
        zip -r -q $ZIP_NAME ./*
	mv $ZIP_NAME $REMEMBER_CUR_DIR
        echo "--> Done. Created $ZIP_NAME."
fi

433