Skip to content

Commit

Permalink
Albrja/mic-5272/miniforge (#90)
Browse files Browse the repository at this point in the history
* Update creation script

* Remove line
  • Loading branch information
albrja authored Oct 9, 2024
1 parent 6184b27 commit 594721d
Showing 1 changed file with 30 additions and 23 deletions.
53 changes: 30 additions & 23 deletions {{cookiecutter.package_name}}/environment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ while getopts ":hft:" option; do
return;;
t) # Type of conda environment to build
env_type=$OPTARG;;
f) # Force creation of new environment
f) # Force creation of a new environment
make_new="yes";;
\?) # Invalid option
echo "Error: Invalid option"
Expand Down Expand Up @@ -77,6 +77,7 @@ else
echo "Existing environment found for $env_name."
one_week_ago=$(date -d "7 days ago" '+%Y-%m-%d %H:%M:%S')
creation_time="$(head -n1 $CONDA_PREFIX/conda-meta/history)"
creation_time=$(echo $creation_time | sed -e 's/^==>\ //g' -e 's/\ <==//g')
requirements_modification_time="$(date -r $install_file '+%Y-%m-%d %H:%M:%S')"
# Check if existing environment is older than a week or if environment was built
# before last modification to requirements file. If so, mark for recreation.
Expand All @@ -88,26 +89,36 @@ else
jq_exists=$(conda list | grep -w jq)
if [[ $jq_exists == '' ]]; then
# Empty string is no return on grep
conda install jq -y
conda install jq -c anaconda -y
fi
echo "Checking framework packages are up to date..."
# Check if there has been an update to vivarium packages since last modification to requirements file
# or more reccent than environment creation
grep @ $install_file
# TODO: can we make this grep output a variable?
while read -r line ; do
# Parse each line of grep output
repo_info=(${line//@/ })
repo=${repo_info[0]}
repo_branch=${repo_info[2]}
last_commit_time=$(curl -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/ihmeuw/$repo/commits?sha=$repo_branch | jq '.["0"].commit.committer.date')
if [[ $creation_time > $last_commit_time ]]; then
# Note: The lines we will return via grep will look like 'vivarium>=#.#.#' or will be of the format
# 'vivarium @ git+https://github.com/ihmeuw/vivarium@SOME_BRANCH'
framework_packages=$(grep -E 'vivarium|gbd|risk_distribution|layered_config' $install_file)
num_packages=$(grep -E 'vivarium|gbd|risk_distribution|layered_config' -c $install_file)

# Iterate through each return of the grep output
for ((i = 1; i <= $num_packages; i++)); do
line=$(echo "$framework_packages" | sed -n "${i}p")
# Check if the line contains '@'
if [[ "$line" == *"@"* ]]; then
repo_info=(${line//@/ })
repo=${repo_info[0]}
repo_branch=${repo_info[2]}
last_update_time=$(curl -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/ihmeuw/$repo/commits?sha=$repo_branch | jq .[0].commit.committer.date)
else
repo=$(echo "$line" | cut -d '>' -f1)
last_update_time=$(curl -s https://pypi.org/pypi/$repo/json | jq -r '.releases | to_entries | max_by(.key) | .value | .[0].upload_time')
fi
last_update_time=$(date -d "$last_update_time" '+%Y-%m-%d %H:%M:%S')
if [[ $creation_time < $last_update_time ]]; then
create_env="yes"
echo "Last update time for $repo: $last_update_time. Environment is stale. Remaking environment..."
break
fi
# This way of writing/exiting while loop is a here string so the process runs
# in the main shell and not a subshell: https://www.gnu.org/software/bash/manual/bashref.html#Here-Strings
# I put an arbitrary empty string here but this is so we can set create_env to yes if we hit that trigger
done <<< "$(echo "")"
done
fi
fi

Expand All @@ -119,21 +130,17 @@ if [[ $create_env == 'yes' ]]; then
conda remove -n $env_name --all -y
fi
# Create conda environment
conda create -n $env_name python=3.11 -y
conda create -n $env_name python=3.11 -c anaconda -y
conda activate $env_name
else
echo "Existing environment validated"
fi

# Install requirements via Github
if [[ $create_env == 'yes' ]]; then
# NOTE: update branch name if you update requirements.txt in a branch
echo "Installing packages for $env_type environment"
pip install -r $install_file
# Editable install of repo
pip install -e .[dev]
# Install redis for simulation environments
if [ $env_type == 'simulation' ]; then
conda install redis -y
conda install redis -c anaconda -y
fi
else
echo "Existing environment validated"
fi

0 comments on commit 594721d

Please sign in to comment.