Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More robust env.sh | Fix conda activate if in subshell #1653

Merged
merged 1 commit into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions scripts/build-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,25 @@ if run_step "10"; then
exit_if_last_command_failed
fi

cat <<EOT >> env.sh
# line auto-generated by $0
# Conda Setup
# 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
source $CYDIR/scripts/fix-open-files.sh
EOT
source $CYDIR/scripts/fix-open-files.sh"

echo "Setup complete!"

Expand Down
6 changes: 2 additions & 4 deletions scripts/init-submodules-no-riscv-tools-nolog.sh
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,6 @@ if [ ! -f ./software/firemarshal/marshal-config.yaml ]; then
echo "firesim-dir: '../../sims/firesim/'" > ./software/firemarshal/marshal-config.yaml
fi

cat << EOT >> env.sh
# line auto-generated by init-submodules-no-riscv-tools.sh
replace_content env.sh init-submodules "# line auto-generated by init-submodules-no-riscv-tools.sh
__DIR="$RDIR"
PATH=\$__DIR/software/firemarshal:\$PATH
EOT
PATH=\$__DIR/software/firemarshal:\$PATH"
56 changes: 56 additions & 0 deletions scripts/replace-content.py
Original file line number Diff line number Diff line change
@@ -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)
24 changes: 24 additions & 0 deletions scripts/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,27 @@ function restore_bash_options
{
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" "$@"
}