Skip to content

Commit 1b6be1e

Browse files
committed
fixup! Move conda environment activation to own script
1 parent c4557e7 commit 1b6be1e

File tree

2 files changed

+3
-63
lines changed

2 files changed

+3
-63
lines changed

scripts/activateCondaEnvironment.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# in the same directory as the given jupyter notebook ipynb file
88
# to create the environment.
99

10-
# Requires juypter nbconvert,operatingSystemFunctions.sh
10+
# Requires operatingSystemFunctions.sh
1111

1212
# Fail on any error ("-e" = exit on first error, "-o pipefail" exist on errors within piped commands)
1313
set -eo pipefail

scripts/executeJupyterNotebook.sh

Lines changed: 2 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -82,62 +82,8 @@ if [ ! -f "${jupyter_notebook_file_path}/.env" ] ; then
8282
echo "NEO4J_INITIAL_PASSWORD=${NEO4J_INITIAL_PASSWORD}" > "${jupyter_notebook_file_path}/.env"
8383
fi
8484

85-
# Define conda environment to use for code structure analysis. Default "codegraph"
86-
CODEGRAPH_CONDA_ENVIRONMENT=${CODEGRAPH_CONDA_ENVIRONMENT:-"codegraph"} # Name of the conda environment to use for code graph analysis
87-
backupCondaEnvironment=$CONDA_DEFAULT_ENV
88-
echo "executeJupyterNotebook: CODEGRAPH_CONDA_ENVIRONMENT=${CODEGRAPH_CONDA_ENVIRONMENT}"
89-
echo "executeJupyterNotebook: CONDA_PREFIX=${CONDA_PREFIX}"
90-
echo "executeJupyterNotebook: Current conda environment=${CONDA_DEFAULT_ENV}"
91-
92-
# Include operation system function to for example detect Windows.
93-
source "${SCRIPTS_DIR}/operatingSystemFunctions.sh"
94-
95-
# Determine the path to "conda"
96-
if [ -n "${CONDA}" ]; then
97-
if isWindows; then
98-
pathToConda="${CONDA}\\Scripts\\" # the trailing backslash character is required
99-
else
100-
pathToConda="${CONDA}/bin/" # the trailing slash character is required
101-
fi
102-
else
103-
pathToConda=""
104-
fi
105-
106-
echo "executeJupyterNotebook: pathToConda=${pathToConda}"
107-
108-
scriptExtension=$(ifWindows ".bat" "")
109-
echo "executeJupyterNotebook: scriptExtension=${scriptExtension}"
110-
111-
# Activate conda shell hook. Also resets CONDA_DEFAULT_ENV to base.
112-
# Thats why CONDA_DEFAULT_ENV (base) is never equal to CODEGRAPH_CONDA_ENVIRONMENT (codegraph).
113-
eval "$(${pathToConda}conda${scriptExtension} shell.bash hook)"
114-
echo "executeJupyterNotebook: Current conda environment after shell hook=${CONDA_DEFAULT_ENV}"
115-
116-
# Create (if missing) and activate Conda environment for code structure graph analysis
117-
if { "${pathToConda}conda" env list | grep "$CODEGRAPH_CONDA_ENVIRONMENT "; } >/dev/null 2>&1; then
118-
echo "executeJupyterNotebook: Conda environment $CODEGRAPH_CONDA_ENVIRONMENT already created"
119-
else
120-
if [ ! -f "${jupyter_notebook_file_path}/environment.yml" ] ; then
121-
echo "executeJupyterNotebook: Couldn't find environment file ${jupyter_notebook_file_path}/environment.yml."
122-
exit 2
123-
fi
124-
echo "executeJupyterNotebook: Creating Conda environment ${CODEGRAPH_CONDA_ENVIRONMENT}"
125-
"${pathToConda}conda" env create --file "${jupyter_notebook_file_path}/environment.yml" --name "${CODEGRAPH_CONDA_ENVIRONMENT}"
126-
fi
127-
128-
echo "executeJupyterNotebook: Activating Conda environment ${CODEGRAPH_CONDA_ENVIRONMENT}"
129-
if isWindows; then
130-
"${pathToConda}conda" activate ${CODEGRAPH_CONDA_ENVIRONMENT}
131-
else
132-
conda activate "${CODEGRAPH_CONDA_ENVIRONMENT}"
133-
fi
134-
135-
if [ "${CONDA_DEFAULT_ENV}" = "${CODEGRAPH_CONDA_ENVIRONMENT}" ] ; then
136-
echo "executeJupyterNotebook: Activated Conda environment: ${CONDA_DEFAULT_ENV}"
137-
else
138-
echo "executeJupyterNotebook: Error: Failed to activate Conda environment ${CODEGRAPH_CONDA_ENVIRONMENT}. ${CONDA_DEFAULT_ENV} still active."
139-
exit 1
140-
fi
85+
# Create and activate (if necessary) Conda environment as defined in environment variable CODEGRAPH_CONDA_ENVIRONMENT (default "codegraph")
86+
source "${SCRIPTS_DIR}/activateCondaEnvironment.sh"
14187

14288
# Execute the Jupyter Notebook and write it to the output file name
14389
jupyter nbconvert --to notebook \
@@ -158,10 +104,4 @@ mv -f "${jupyter_notebook_markdown_file}.nostyle" "${jupyter_notebook_markdown_f
158104
# Convert the Jupyter Notebook to PDF
159105
if [ -n "${ENABLE_JUPYTER_NOTEBOOK_PDF_GENERATION}" ]; then
160106
jupyter nbconvert --to webpdf --no-input --allow-chromium-download --disable-chromium-sandbox "$jupyter_notebook_output_file"
161-
fi
162-
163-
# Restore Conda environment
164-
if [ ! "${backupCondaEnvironment}" = "${CODEGRAPH_CONDA_ENVIRONMENT}" ] ; then
165-
"${pathToConda}conda" activate "${backupCondaEnvironment}"
166-
echo "executeJupyterNotebook: Restored Conda Environment to ${backupCondaEnvironment}: ${CONDA_DEFAULT_ENV}"
167107
fi

0 commit comments

Comments
 (0)