BUG: foamCleanPath not removing duplicate non-existent directories

This commit is contained in:
Mark Olesen
2017-04-20 13:04:46 +02:00
parent 7f01a4beda
commit 65eb43fcb1

View File

@ -4,7 +4,7 @@
# \\ / F ield | OpenFOAM: The Open Source CFD Toolbox # \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
# \\ / O peration | # \\ / O peration |
# \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation # \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
# \\/ M anipulation | # \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
# License # License
# This file is part of OpenFOAM. # This file is part of OpenFOAM.
@ -29,128 +29,134 @@
# Usage: foamCleanPath [-strip] path [wildcard] .. [wildcard] # Usage: foamCleanPath [-strip] path [wildcard] .. [wildcard]
# #
# Prints its argument (which should be a ':' separated path) # Prints its argument (which should be a ':' separated path)
# without all # without the following:
# - duplicate elements # - duplicate elements
# - elements whose start matches a wildcard # - elements whose start matches a wildcard
# - inaccessible directories (with the -strip (at your option) # - inaccessible directories (with the -strip option)
# #
# Note # Note
# - this routine will fail when directories have embedded spaces # - this routine will fail when directories have embedded spaces
# - false matches possible if a wildcard contains '.' (sed regex) # - false matches possible if a wildcard contains '.' (sed regex)
# - the wildcards can themselves can be written together and separated # - the wildcards themselves can be written together and separated
# by colons or whitespace # by colons or whitespace
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
usage() { usage() {
cat <<USAGE 1>&2 cat <<USAGE 1>&2
Usage: ${0##*/} [OPTION] path [wildcard1] .. [wildcardN] Usage: ${0##*/} [OPTION] path [wildcard1] .. [wildcardN]
options: options:
-debug print debug information to stderr
-strip remove inaccessible directories -strip remove inaccessible directories
-help print the usage -help print the usage
Prints its argument (which should be a ':' separated list) cleansed from Prints its argument (which should be a ':' separated list) cleansed from
- duplicate elements - duplicate elements
- elements whose start matches one of the wildcard(s) - elements whose start matches one of the wildcard(s)
- inaccessible directories (with the -strip option) - inaccessible directories (with the -strip option)
Exit status Exit status
0 on success 0 on success
1 for miscellaneous errors. 1 for miscellaneous errors.
2 initial value of 'path' is empty 2 initial value of 'path' is empty
USAGE USAGE
exit 1 exit 1
} }
unset strip # Parse options
# parse options unset optDebug optStrip
while [ "$#" -gt 0 ] while [ "$#" -gt 0 ]
do do
case "$1" in case "$1" in
-h | -help) -h | -help)
usage usage
;; ;;
-debug)
optDebug=true
;;
-strip) -strip)
strip=true optStrip=true
shift
;; ;;
*) *)
break break
;; ;;
esac esac
shift
done done
# Basic checks, setup
[ "$#" -ge 1 ] || usage [ "$#" -ge 1 ] || usage
dirList="$1" dirList="$1"
shift shift
[ -n "$dirList" ] || exit 2 # quick exit on empty 'dirList' [ -n "$dirList" ] || exit 2 # Quick exit on empty 'dirList'
#-------------------------------------------------------------------------------
##DEBUG echo "input>$dirList<" 1>&2 # Debugging (optional)
if [ -n "$optDebug" ]
then
printDebug() { while [ "$#" -ge 1 ]; do echo "$1" 1>&2; shift; done; }
else
printDebug() { true; } # No-op
fi
# preserve current IFS and split on colon or whitespace # Check directory existence (optional)
oldIFS="$IFS" if [ -n "$optStrip" ]
IFS=': ' then
isDir() { test -d "$1"; } # Check for directory
else
isDir() { true; } # No check (always true)
fi
# "wildcard1 ... wildcardN" may have been passed as a single parameter # The "wildcard1 ... wildcardN" may have been passed as a single parameter
# or may contain ':' separators # or may contain ':' separators
oldIFS="$IFS" # Preserve initial IFS
IFS=': ' # Split on colon, whitespace
set -- $* set -- $*
printDebug "input>$dirList<"
# Strip out wildcards via sed. Path and wildcard cannot contain '?'. # Strip out wildcards via sed. Path and wildcard cannot contain '?'.
while [ "$#" -ge 1 ] while [ "$#" -ge 1 ]
do do
wildcard=$1 wildcard="$1"
shift shift
##DEBUG echo "remove>$wildcard<" 1>&2
if [ -n "$wildcard" ] if [ -n "$wildcard" ]
then then
printDebug "remove>$wildcard<"
dirList=$(echo "$dirList:" | sed -e "s?${wildcard}[^:]*:??g") dirList=$(echo "$dirList:" | sed -e "s?${wildcard}[^:]*:??g")
fi fi
done done
printDebug "intermediate>$dirList<"
# split on ':' (and on space as well to avoid any surprises) IFS=': ' # Split on colon, whitespace (to avoid surprises)
IFS=': '
set -- $dirList set -- $dirList
##DEBUG echo "intermediate>$dirList<" 1>&2 IFS="$oldIFS" # Restore initial IFS
# rebuild the list from scratch # Rebuild the list
unset dirList unset dirList
for dir for dir
do do
##DEBUG echo "check>$dir<" 1>&2 printDebug "check>$dir< in $dirList"
#- dirs must exist if isDir "$dir"
if [ -e "$dir" ]
then then
#- no duplicate dirs # Detect duplicates (ie, dir already in the list)
duplicate=$(echo " $dirList " | sed -ne "s: $dir :DUP:p") duplicate=$(echo ":$dirList:" | sed -ne '\?:'"$dir"':?p')
if [ ! "$duplicate" ] if [ -n "$duplicate" ]
then then
dirList="$dirList $dir" printDebug "duplicate>$dir<"
else
dirList="${dirList}${dirList:+:}$dir"
fi fi
elif [ "$strip" != true ]
then
# Print non-existing directories if not in 'strip' mode.
dirList="$dirList $dir"
fi fi
done done
# split on whitespace printDebug "output>$dirList<"
IFS=' '
set -- $dirList
# rejoin on ':'
IFS=':'
dirList="$*"
# restore IFS
IFS="$oldIFS"
##DEBUG echo "output>$dirList<" 1>&2
echo "$dirList" echo "$dirList"
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------