More robust env.sh | Fix conda activate if in subshell
This commit is contained in:
@@ -250,11 +250,25 @@ if run_step "10"; then
|
|||||||
exit_if_last_command_failed
|
exit_if_last_command_failed
|
||||||
fi
|
fi
|
||||||
|
|
||||||
cat <<EOT >> env.sh
|
# Conda Setup
|
||||||
# line auto-generated by $0
|
# Provide a sourceable snippet that can be used in subshells that may not have
|
||||||
|
# inhereted conda functions that would be brought in under a login shell that
|
||||||
|
# has run conda init (e.g., VSCode, CI)
|
||||||
|
read -r -d '\0' CONDA_ACTIVATE_PREAMBLE <<'END_CONDA_ACTIVATE'
|
||||||
|
if ! type conda >& /dev/null; then
|
||||||
|
echo "::ERROR:: you must have conda in your environment first"
|
||||||
|
return 1 # don't want to exit here because this file is sourced
|
||||||
|
fi
|
||||||
|
|
||||||
|
# if we're sourcing this in a sub process that has conda in the PATH but not as a function, init it again
|
||||||
|
conda activate --help >& /dev/null || source $(conda info --base)/etc/profile.d/conda.sh
|
||||||
|
\0
|
||||||
|
END_CONDA_ACTIVATE
|
||||||
|
|
||||||
|
replace_content env.sh build-setup "# line auto-generated by $0
|
||||||
|
$CONDA_ACTIVATE_PREAMBLE
|
||||||
conda activate $CYDIR/.conda-env
|
conda activate $CYDIR/.conda-env
|
||||||
source $CYDIR/scripts/fix-open-files.sh
|
source $CYDIR/scripts/fix-open-files.sh"
|
||||||
EOT
|
|
||||||
|
|
||||||
echo "Setup complete!"
|
echo "Setup complete!"
|
||||||
|
|
||||||
|
|||||||
@@ -157,8 +157,6 @@ if [ ! -f ./software/firemarshal/marshal-config.yaml ]; then
|
|||||||
echo "firesim-dir: '../../sims/firesim/'" > ./software/firemarshal/marshal-config.yaml
|
echo "firesim-dir: '../../sims/firesim/'" > ./software/firemarshal/marshal-config.yaml
|
||||||
fi
|
fi
|
||||||
|
|
||||||
cat << EOT >> env.sh
|
replace_content env.sh init-submodules "# line auto-generated by init-submodules-no-riscv-tools.sh
|
||||||
# line auto-generated by init-submodules-no-riscv-tools.sh
|
|
||||||
__DIR="$RDIR"
|
__DIR="$RDIR"
|
||||||
PATH=\$__DIR/software/firemarshal:\$PATH
|
PATH=\$__DIR/software/firemarshal:\$PATH"
|
||||||
EOT
|
|
||||||
|
|||||||
56
scripts/replace-content.py
Executable file
56
scripts/replace-content.py
Executable file
@@ -0,0 +1,56 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
# Replace text in a file given a key identifying a block to replace.
|
||||||
|
# If the file doesn't exist, create it.
|
||||||
|
#
|
||||||
|
# args
|
||||||
|
# $1 - file to replace text in
|
||||||
|
# $2 - key used to find block of text to replace
|
||||||
|
# $3 - text to fill in block that is replaced
|
||||||
|
|
||||||
|
import re
|
||||||
|
import sys
|
||||||
|
|
||||||
|
def CY_INITIALIZE_RE_BLOCK(k):
|
||||||
|
return (
|
||||||
|
r"^# >>> " + f"{k}" + r" initialize >>>(?:\n|\r\n)"
|
||||||
|
r"([\s\S]*?)"
|
||||||
|
r"# <<< " + f"{k}" + r" initialize <<<(?:\n|\r\n)?"
|
||||||
|
)
|
||||||
|
|
||||||
|
def CY_INITIALIZE_START_TOKEN(k):
|
||||||
|
return "# >>> " + f"{k}" + " initialize >>>"
|
||||||
|
|
||||||
|
def CY_INITIALIZE_END_TOKEN(k):
|
||||||
|
return "# <<< " + f"{k}" + " initialize <<<"
|
||||||
|
|
||||||
|
# ------------------------------
|
||||||
|
|
||||||
|
try:
|
||||||
|
with open(sys.argv[1]) as fh:
|
||||||
|
fh_content = fh.read()
|
||||||
|
except FileNotFoundError:
|
||||||
|
fh_content = ""
|
||||||
|
except:
|
||||||
|
raise
|
||||||
|
|
||||||
|
initialize_comment_key = sys.argv[2]
|
||||||
|
inner_contents = CY_INITIALIZE_START_TOKEN(initialize_comment_key) + "\n" + sys.argv[3] + "\n" + CY_INITIALIZE_END_TOKEN(initialize_comment_key) + "\n"
|
||||||
|
|
||||||
|
# ------------------------------
|
||||||
|
|
||||||
|
replace_str = "__CY_REPLACE_ME_123__"
|
||||||
|
fh_content = re.sub(
|
||||||
|
CY_INITIALIZE_RE_BLOCK(initialize_comment_key),
|
||||||
|
replace_str,
|
||||||
|
fh_content,
|
||||||
|
flags=re.MULTILINE,
|
||||||
|
)
|
||||||
|
# TODO: maybe remove all but last of replace_str, if there's more than one occurrence
|
||||||
|
fh_content = fh_content.replace(replace_str, inner_contents)
|
||||||
|
|
||||||
|
if CY_INITIALIZE_START_TOKEN(initialize_comment_key) not in fh_content:
|
||||||
|
fh_content += "\n%s\n" % inner_contents
|
||||||
|
|
||||||
|
with open(sys.argv[1], "w") as fh:
|
||||||
|
fh.write(fh_content)
|
||||||
@@ -53,3 +53,27 @@ function restore_bash_options
|
|||||||
{
|
{
|
||||||
set +vx; eval "$OLDSTATE"
|
set +vx; eval "$OLDSTATE"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#######################################
|
||||||
|
# Wrapper around replace-content.py.
|
||||||
|
# For a file ($1), write out text ($3) into it
|
||||||
|
# replacing any area designated by $2.
|
||||||
|
#######################################
|
||||||
|
function replace_content
|
||||||
|
{
|
||||||
|
# On macOS, use GNU readlink from 'coreutils' package in Homebrew/MacPorts
|
||||||
|
if [ "$(uname -s)" = "Darwin" ] ; then
|
||||||
|
READLINK=greadlink
|
||||||
|
else
|
||||||
|
READLINK=readlink
|
||||||
|
fi
|
||||||
|
|
||||||
|
# If BASH_SOURCE is undefined, we may be running under zsh, in that case
|
||||||
|
# provide a zsh-compatible alternative
|
||||||
|
DIR="$(dirname "$($READLINK -f "${BASH_SOURCE[0]:-${(%):-%x}}")")"
|
||||||
|
file="$1"
|
||||||
|
shift
|
||||||
|
key="$1"
|
||||||
|
shift
|
||||||
|
$DIR/replace-content.py "$file" "$key" "$@"
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user