Commit 0559e125 authored by Mark Olesen's avatar Mark Olesen

BACKPORT: openfoam shell wrapper (interactive or 'one-shot' use)

parent 56be7ed6
#!/bin/sh
#------------------------------------------------------------------------------
# ========= |
# \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
# \\ / O peration |
# \\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
# \\/ M anipulation |
#-------------------------------------------------------------------------------
# License
# This file is part of OpenFOAM, licensed under GNU General Public License
# <http://www.gnu.org/licenses/>.
#
# Script
# openfoam [args]
#
# Description
# Open an interactive bash session with an OpenFOAM environment,
# or run an OpenFOAM application (with arguments) after first sourcing
# the OpenFOAM etc/bashrc file from the project directory.
#
# This script normally exists in $WM_PROJECT_DIR/bin/tools but can also
# be modified to use a hard-coded PROJECT_DIR entry and placed elsewhere
# in the filesystem (eg, /usr/bin).
#
#------------------------------------------------------------------------------
# Hard-coded value (eg, with autoconfig)
projectDir="@PROJECT_DIR@"
if [ -z "$projectDir" ] || [ "${projectDir#@}" != "$projectDir" ]
then
# Auto-detect from location
toolsDir="${0%/*}" # The bin/tools dir
projectDir="${toolsDir%/bin/tools}" # Project dir
case "$projectDir" in
(/bin | /usr/bin | /usr/local/bin)
# This shouldn't happen.
# If copied to a system dir, should also be using hard-coded values!
echo "Warning: suspicious looking project dir: $projectDir" 1>&2
;;
("$toolsDir")
# Eg, called as ./openfoam etc - need to try harder
projectDir="$(\cd $(dirname $0)/../.. && \pwd -L)" || unset projectDir
;;
esac
fi
#------------------------------------------------------------------------------
usage() {
exec 1>&2
while [ "$#" -ge 1 ]; do echo "$1"; shift; done
cat<<USAGE
Usage: ${0##*/} [OPTION] [application ...]
options:
-prefix=DIR Specify alternative OpenFOAM directory
-sp Single precision
-dp Double precision
-spdp Mixed single/double precision
-int32 | -int64 The label-size
-help Print the usage
Open an interactive bash session with an OpenFOAM environment,
or run an OpenFOAM application (with arguments) after first sourcing
the OpenFOAM etc/bashrc file from the project directory:
($projectDir)
For more information: www.OpenFOAM.com
USAGE
exit 1
}
#-------------------------------------------------------------------------------
# Only preserve settings for non-interactive?
if [ "$#" -eq 0 ]
then
unset _foamSettings FOAM_SETTINGS
else
_foamSettings="$FOAM_SETTINGS"
fi
# Parse options
while [ "$#" -gt 0 ]
do
case "$1" in
-h | -help*)
usage
;;
-prefix=* | -foam=*)
projectDir="${1#*=}"
;;
-sp | -SP)
# WM_PRECISION_OPTION=...
_foamSettings="$_foamSettings${_foamSettings:+ }WM_PRECISION_OPTION=SP"
;;
-dp | -DP)
# WM_PRECISION_OPTION=...
_foamSettings="$_foamSettings${_foamSettings:+ }WM_PRECISION_OPTION=DP"
;;
-spdp | -SPDP)
# WM_PRECISION_OPTION=...
_foamSettings="$_foamSettings${_foamSettings:+ }WM_PRECISION_OPTION=SPDP"
;;
-int32 | -int64)
# WM_LABEL_SIZE=...
_foamSettings="$_foamSettings${_foamSettings:+ }WM_LABEL_SIZE=${1#-int}"
;;
--)
shift
break
;;
-*)
echo "Error: unknown option: '$1'" 1>&2
exit 1
;;
*)
break
;;
esac
shift
done
#-------------------------------------------------------------------------------
# Remove current OpenFOAM environment
if [ -d "$WM_PROJECT_DIR" ] && [ -f "$WM_PROJECT_DIR/etc/config.sh/unset" ]
then
. "$WM_PROJECT_DIR/etc/config.sh/unset"
fi
[ -d "$projectDir" ] || {
echo "Error: no project dir: $projectDir" 1>&2
exit 2
}
_foamSourceBashEnv="$projectDir/etc/bashrc"
if [ "$#" -eq 0 ]
then
# Interactive shell
_foamSourceBashEnv="$projectDir/bin/tools/source-bashrc"
fi
[ -f "$_foamSourceBashEnv" ] || {
echo "Error: file not found: $_foamSourceBashEnv" 1>&2
exit 2
}
if [ "$#" -eq 0 ]
then
# Source user ~/.bashrc and OpenFOAM etc/bashrc.
# 1) Can either use a tmp file, or 2) chain off to a dedicated file
# We use a dedicated file.
if [ -n "$_foamSettings" ]
then
export FOAM_SETTINGS="$_foamSettings"
fi
## echo "Source with $_foamSourceBashEnv with '$FOAM_SETTINGS'" 1>&2
# Interactive shell (newer bash can use --init-file instead of --rcfile)
exec bash --rcfile "$_foamSourceBashEnv" -i
else
# Non-interactive
# Source bashrc within a function to preserve command-line arguments
# - this will not have aliases, but working non-interactively anyhow
sourceBashrc()
{
. "$_foamSourceBashEnv" $_foamSettings
}
sourceBashrc
exec "$@"
fi
#------------------------------------------------------------------------------
#----------------------------------*-sh-*--------------------------------------
# ========= |
# \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
# \\ / O peration |
# \\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
# \\/ M anipulation |
#------------------------------------------------------------------------------
# License
# This file is part of OpenFOAM, licensed under GNU General Public License
# <http://www.gnu.org/licenses/>.
#
# File
# bin/tools/source-bashrc
#
# Description
# Source user ~/.bashrc and OpenFOAM etc/bashrc
#
# This file is normally not sourced manually,
# but from bash with the --rcfile option.
#------------------------------------------------------------------------------
# Hard-coded value (eg, with autoconfig)
projectDir="@PROJECT_DIR@"
if [ -z "$projectDir" ] || [ "${projectDir#@}" != "$projectDir" ]
then
# Auto-detect (as per OpenFOAM etc/bashrc)
# --
# Assuming this file is $WM_PROJECT_DIR/bin/tools/source-bashrc,
# the next lines should work when sourced by BASH or ZSH shells.
# --
projectDir="${BASH_SOURCE:-${ZSH_NAME:+$0}}"
[ -n "$projectDir" ] && projectDir="$(\cd $(dirname $projectDir)/../.. && \pwd -L)" || unset projectDir
fi
#------------------------------------------------------------------------------
if [ -d "$projectDir" ]
then
_foamSourceBashEnv="$projectDir/etc/bashrc"
else
unset _foamSourceBashEnv
fi
# Source the user bashrc first.
# Simply hope that they don't unset/reset _foamSourceBashEnv !!
if [ -f "$HOME/.bashrc" ]
then
. "$HOME/.bashrc"
fi
# Source the OpenFOAM etc/bashrc
if [ -f "$_foamSourceBashEnv" ]
then
. "$_foamSourceBashEnv" $FOAM_SETTINGS
# Avoid further inheritance
unset FOAM_SETTINGS
# Some feedback
if [ -n "$PS1" ] && [ -d "$WM_PROJECT_DIR" ]
then
info="$(foamEtcFile -show-patch 2>/dev/null)"
# echo "Using: OpenFOAM-$WM_PROJECT_VERSION ($FOAM_API${info:+ patch=$info}) - see www.OpenFOAM.com" 1>&2
echo "Using: OpenFOAM-$WM_PROJECT_VERSION${info:+ (patch=$info)} - see www.OpenFOAM.com" 1>&2
echo "Arch: $WM_OPTIONS (mpi=$FOAM_MPI)" 1>&2
## echo "$WM_PROJECT_DIR" 1>&2
## echo 1>&2
# Set prompt as reminder that this is a shell session
# Chalmers likes this one:
# PS1="OpenFOAM${FOAM_API:+-$FOAM_API}:"'$(foamPwd)\n\u\$ '
PS1="OpenFOAM${FOAM_API:+-$FOAM_API}:"'\w/\n\u\$ '
fi
else
echo "Could not locate OpenFOAM etc/bashrc in '$projectDir'" 1>&2
fi
echo "OpenFOAM shell session - use exit to quit" 1>&2
echo 1>&2
# Cleanup variables (done as final statement for a clean exit code)
unset _foamSourceBashEnv projectDir
#------------------------------------------------------------------------------
......@@ -2,7 +2,7 @@
# ========= |
# \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
# \\ / O peration |
# \\ / A nd | Copyright (C) 2016-2018 OpenCFD Ltd.
# \\ / A nd | Copyright (C) 2016-2019 OpenCFD Ltd.
# \\/ M anipulation |
#------------------------------------------------------------------------------
# | Copyright (C) 2011-2016 OpenFOAM Foundation
......@@ -111,15 +111,16 @@ foamPV()
unset -f foamPwd 2>/dev/null
foamPwd()
{
if [ -d "$WM_PROJECT_DIR" ]
if [ -n "$WM_PROJECT_DIR" ]
then
echo "$PWD" | sed \
-e "s#^${FOAM_RUN}#\$FOAM_RUN#;" \
-e "s#^${WM_PROJECT_DIR}#\$WM_PROJECT_DIR#;" \
-e "s#^${WM_PROJECT_USER_DIR}#\$WM_PROJECT_USER_DIR#;" \
-e "s#^${HOME}#\$HOME#";
echo "$PWD/" | sed \
-e "s#^${FOAM_RUN}/#\$FOAM_RUN/#" \
-e "s#^${WM_PROJECT_DIR}/#\$WM_PROJECT_DIR/#" \
-e "s#^${WM_PROJECT_USER_DIR}/#\$WM_PROJECT_USER_DIR/#" \
-e "s#^${HOME}/#~/#" \
;
else
echo "$PWD" | sed -e "s#^${HOME}#\$HOME#;"
echo "$PWD/" | sed -e "s#^${HOME}/#~/#";
fi
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment