Skip to content

Commit

Permalink
Fix bug in grep (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
albrja authored Sep 30, 2024
1 parent f276bcf commit 6184b27
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 0 deletions.
23 changes: 23 additions & 0 deletions artifact_requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Note: The lines we will return via grep will look like 'package_name>=#.#.#' or will be of the format
# 'package_name @ git+https://github.com/ihmeuw/package_name@SOME_BRANCH'
vivarium>=3.0.0
vivarium_public_health>=3.0.0
gbd_mapping>=4.0.0
click
jinja2
loguru
numpy
pandas
pyyaml
scipy
tables
vivarium_inputs>=5.0.0
# Note viv_in is not install with [data] extra because we might need to specify the branch
# for viv_in for the data requirements
vivarium_gbd_access>=4.0.0
core-maths
vivarium_cluster_tools>=2.0.0
black==22.3.0
isort
jupyterlab
matplotlib
146 changes: 146 additions & 0 deletions environment.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
#!/bin/bash

# Reset OPTIND so help can be invoked multiple times per shell session.
OPTIND=1
Help()
{
# Display Help
echo "Script to automatically create and validate conda environments."
echo
echo "Syntax: source environment.sh [-h|t|v]"
echo "options:"
echo "h Print this Help."
echo "t Type of conda environment. Either 'simulation' (default) or 'artifact'."
echo "f Force creation of a new environment."
}

# Define variables
username=$(whoami)
env_type="simulation"
make_new="no"

# Process input options
while getopts ":hft:" option; do
case $option in
h) # display help
Help
return;;
t) # Type of conda environment to build
env_type=$OPTARG;;
f) # Force creation of a new environment
make_new="yes";;
\?) # Invalid option
echo "Error: Invalid option"
return;;
esac
done

# Parse environment name
env_name=$(basename "`pwd`")
env_name+="_$env_type"
branch_name=$(git rev-parse --abbrev-ref HEAD)
# Determine which requirements.txt to install from
if [ $env_type == 'simulation' ]; then
install_file="requirements.txt"
elif [ $env_type == 'artifact' ]; then
install_file="artifact_requirements.txt"
else
echo "Invalid environment type. Valid argument types are 'simulation' and 'artifact'."
return
fi

# Pull repo to get latest changes from remote if remote exists
git ls-remote --exit-code --heads origin $branch_name >/dev/null 2>&1
exit_code=$?
if [[ $exit_code == '0' ]]; then
git fetch --all
echo "Git branch '$branch_name' exists in the remote repository, pulling latest changes..."
git pull origin $branch_name
fi

# Check if environment exists already
create_env=$(conda info --envs | grep $env_name | head -n 1)
if [[ $create_env == '' ]]; then
# No environment exists with this name
echo "Environment $env_name does not exist."
create_env="yes"
env_exists="no"
elif [[ $make_new == 'yes' ]]; then
# User has requested to make a new environment
echo "Making a new environment."
create_env="yes"
env_exists="yes"
else
env_exists="yes"
conda activate $env_name
# Check if existing environment needs to be recreated
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.
if [[ $one_week_ago > $creation_time ]] | [[ $creation_time < $requirements_modification_time ]]; then
echo "Environment is stale. Deleting and remaking environment..."
create_env="yes"
else
# Install json parser if it is not installed
jq_exists=$(conda list | grep -w jq)
if [[ $jq_exists == '' ]]; then
# Empty string is no return on grep
conda install jq -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
# 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'
# echo $(grep -E 'vivarium|gbd|risk_distribution|layered_config' $install_file)
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 '.releases | to_entries | max_by(.key) | .value | .[0].upload_time')
fi
if [[ $creation_time > $last_commit_time ]]; then
create_env="yes"
echo "Last update time for $repo: $last_update_time. Environment is stale. Remaking environment..."
break
fi
done
fi
fi

if [[ $create_env == 'yes' ]]; then
if [[ $env_exists == 'yes' ]]; then
if [[ $env_name == $CONDA_DEFAULT_ENV ]]; then
conda deactivate
fi
conda remove -n $env_name --all -y
fi
# Create conda environment
conda create -n $env_name python=3.11 -y
conda activate $env_name
# 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
fi
else
echo "Existing environment validated"
fi
18 changes: 18 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Note: The lines we will return via grep will look like 'package_name>=#.#.#' or will be of the format
# 'package_name @ git+https://github.com/ihmeuw/package_name@SOME_BRANCH'
vivarium>=3.0.0
vivarium_public_health>=3.0.0
click
jinja2
loguru
numpy
pandas
pyyaml
scipy
tables
vivarium_cluster_tools>=2.0.0
pytest
black==22.3.0
isort
jupyterlab
matplotlib

0 comments on commit 6184b27

Please sign in to comment.