-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_geant_cmake.sh
executable file
·141 lines (116 loc) · 3.95 KB
/
run_geant_cmake.sh
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
#!/usr/bin/env bash
#####################
#
# Script to execute the provided cmake command and download datasets if required
#
# New versions of geant4 often come with new datasets. This script automates their
# download and extraction by running the cmake command file created by
# create_geant_cmake.sh, parsing the output and doing what's needed.
#
#####################
RESTORE="\e[0m" #N.B. This is a reset of the colour, not black
RED="\e[31m"
GREEN="\e[32m"
BLUE="\e[1;34m"
YELLOW="\e[33m"
usage() {
echo -e "
\t${BLUE}USAGE:${RESTORE}\t${BASH_SOURCE##*/} -f <cmake command file>
"
exit 1
}
while getopts ":hf:" OPTIONS
do
case "${OPTIONS}" in
h | \? | : )
usage
;;
f )
FILE=${OPTARG}
;;
esac
done
#Confirm that an input file has been provided
if [[ -z "${FILE}" ]]
then
echo -e "\n\t${RED}ERROR:${RESTORE}\tYou need to specify the cmake command file to run"
usage
elif [[ ! -e "${FILE}" ]]
then
echo -e "\n\t${RED}ERROR:${RESTORE}\t${FILE} does not exist"
usage
fi
#Capture where the data files are expected to live
DATADIR=$(sed "s/^.*INSTALL_DATADIR=\([^ ]*\).*$/\1/" ${FILE})
if [[ -z ${DATADIR} ]]
then
echo -e "${RED}\n\tERROR:${RESTORE}\tThe data directory has not been specified, exiting...\n"
exit 2
fi
#We'll capture the output rather than running cmake multiple times.
CMAKE_OUTPUT=cmake_output.log
#Run the cmake command that was provided
echo -e "${YELLOW}Running cmake${RESTORE}\n"
#If cmake fails, we have bigger issues than missing data
./${FILE} 2>&1 | tee ${CMAKE_OUTPUT}
if [[ ${PIPESTATUS[0]} -ne 0 ]]
then
echo -e "${RED}\n\tERROR:${RESTORE}\tRunning cmake failed\n"
exit 3
fi
#What should we look for to confirm that we are missing datasets
MISSING_STRING="the following datasets are NOT present"
#If we aren't missing any datasets, there is nothing further to do.
if [[ $(grep -c "${MISSING_STRING}" ${CMAKE_OUTPUT}) -eq 0 ]]
then
echo -e "\n${GREEN}All went as expected.${RESTORE}\n"
exit 4
#But if we are, make sure we can write/extract to the path specified
else
echo -en "\n${YELLOW}Data files are missing "
if [[ ! -d ${DATADIR} ]]
then
echo -en "\n**WARNING**: The data directory (${DATADIR}) does not exist creating now"
if [[ ! -w ${DATADIR}/.. ]]
then
echo -e "${RED}\n\tERROR:${RESTORE} You don't have permission to write to ${DATADIR}/.. , exiting...\n${RESTORE}"
exit 5
fi
mkdir -pv "${DATADIR}"
fi
if [[ ! -w ${DATADIR} ]]
then
echo -e "${YELLOW}but you don't have permission to write to ${DATADIR}, exiting...\n${RESTORE}"
exit 5
fi
echo -e "aquiring now.${RESTORE}\n"
fi
#Store the url(s) of the missing datasets
FILE_LIST=${DATADIR}/filesToGet.txt
#The regex for missing data sets
FILE_STRING="http[s?]://.*\.tar\.gz"
#Get which data files are missing.
sed -n "s|^[ \t]*\(${FILE_STRING}\)|\1|p" ${CMAKE_OUTPUT} > ${FILE_LIST}
echo -e "${YELLOW}Moving into the data directory ${DATADIR}${RESTORE}\n"
pushd "${DATADIR}" > /dev/null
echo -e "${YELLOW}Downloading and extracting the necessary files:${RESTORE}"
#We could pass the file to wget with the -i flag but we'd still have to parse
#it to extract each dataset so do it this way
while read -r LINE
do
#echo "${LINE} |---| ${LINE##*/}"
#We want to keep the archived files so download and extract separately
wget -nv --show-progress ${LINE}
#Without the 'o' flag, files extract as owned by geant developers i.e. unknown on this system
tar xof ${LINE##*/}
done < ${FILE_LIST}
#Cleanup, delete, the file with list of missing datasets
rm -f ${FILE_LIST}
#We could do a recursive call to this script, but lets not.
#Just re-run the cmake command, everything should work now.
echo -e "\n${YELLOW}Re-running cmake now that we have the all of the datasets${RESTORE}"
popd > /dev/null
./${FILE}
#Leave the output from the original cmake execution
#rm -v ${CMAKE_OUTPUT}
exit $?