@@ -37,6 +37,31 @@ file_changed_in_pr() {
3737 ) && return 0 || return 1
3838}
3939
40+ sed_update_if_changed () {
41+ # Usage: sed_update_if_changed 's/foo/bar/' file.txt
42+ if [ " $# " -ne 2 ]; then
43+ echo " Usage: sed_update_if_changed 'sed_command' file" >&2
44+ return 1
45+ fi
46+
47+ local sed_command=" $1 "
48+ local file=" $2 "
49+ local tmp_file=" $( mktemp " ${file} .XXXXXX" ) "
50+
51+ sed " $sed_command " " $file " > " $tmp_file " || {
52+ rm -f " $tmp_file "
53+ echo " sed command failed" >&2
54+ return 1
55+ }
56+
57+ if ! diff -q " $file " " $tmp_file " > /dev/null; then
58+ # Use cat to retain existing permissions, set umask to world readable in case the target file does not yet exist.
59+ (umask 022 && cat " $tmp_file " > " $file " )
60+ fi
61+ # Remove the temporary file
62+ rm -f " $tmp_file "
63+ }
64+
4065compare_and_copy () {
4166 if [ " $# " -ne 2 ]; then
4267 echo " Usage of function: compare_and_copy <source_file> <destination_file>"
@@ -193,15 +218,15 @@ copy_files_by_list ${TOPDIR} ${INSTALL_PREFIX}/init/easybuild "${hook_files[@]}"
193218# but that should be fine (no changes are made if version placeholder is not present anymore)
194219
195220# make sure that scripts in init/ and scripts/ use correct EESSI version
196- sed -i " s/__EESSI_VERSION_DEFAULT__/${EESSI_VERSION} /g" ${INSTALL_PREFIX} /init/eessi_defaults
221+ sed_update_if_changed " s/__EESSI_VERSION_DEFAULT__/${EESSI_VERSION} /g" ${INSTALL_PREFIX} /init/eessi_defaults
197222
198223# replace placeholder for default EESSI version in Lmod init scripts
199224for shell in $( ls ${INSTALL_PREFIX} /init/lmod) ; do
200- sed -i " s/__EESSI_VERSION_DEFAULT__/${EESSI_VERSION} /g" ${INSTALL_PREFIX} /init/lmod/${shell}
225+ sed_update_if_changed " s/__EESSI_VERSION_DEFAULT__/${EESSI_VERSION} /g" ${INSTALL_PREFIX} /init/lmod/${shell}
201226done
202227
203228# replace EESSI version used in comments in EESSI module
204- sed -i " s@/<EESSI_VERSION>/@/${EESSI_VERSION} /@g" ${INSTALL_PREFIX} /init/modules/EESSI/${EESSI_VERSION} .lua
229+ sed_update_if_changed " s@/<EESSI_VERSION>/@/${EESSI_VERSION} /@g" ${INSTALL_PREFIX} /init/modules/EESSI/${EESSI_VERSION} .lua
205230
206231# replace EESSI version used in EasyBuild hooks
207- sed -i " s@/<EESSI_VERSION>/@/${EESSI_VERSION} /@g" ${INSTALL_PREFIX} /init/easybuild/eb_hooks.py
232+ sed_update_if_changed " s@/eessi- <EESSI_VERSION>/@/eessi- ${EESSI_VERSION} /@g" ${INSTALL_PREFIX} /init/easybuild/eb_hooks.py
0 commit comments