Created
September 17, 2019 09:32
-
-
Save marcelm/32dc00cb5e4018670294d2508883402d to your computer and use it in GitHub Desktop.
Create a Conda environment.lock.yml for macOS while running on Linux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This script creates both | |
# - environment.osx.lock.yml and | |
# - environment.linux.lock.yml | |
# regardless of the operating system it is running on. The trick is | |
# temporarily setting the subdir and subdirs keys in .condarc to | |
# what would be appropriate for the other operating system. | |
# | |
# It assumes that there exists a (manually managed) environment.yml file | |
# that contains the abstract dependencies. | |
# When creating the test environment, the .lock.yml files are used in order | |
# to ensure reproducibility. | |
set -euo pipefail | |
# We need to temporarily move away the user's .condarc if it exists. | |
if [[ -e ~/.condarc ]]; then | |
mv ~/.condarc ~/.condarc.condalock.bak | |
trap "mv ~/.condarc.condalock.bak ~/.condarc" EXIT | |
else | |
trap "rm ~/.condarc" EXIT | |
fi | |
for os in linux osx; do | |
env=temporary-environment-$os | |
env_yml=environment.$os.lock.yml | |
printf "subdir: %s-64\nsubdirs:\n - %s-64\n - noarch\n" $os $os >> ~/.condarc | |
conda env create -n $env -f environment.yml | |
conda env export -n $env | grep -Ev '^(name|prefix):' > ${env_yml} | |
conda env remove -n $env | |
echo "Created ${env_yml}" | |
done | |
# The original .condarc will be restored by the exit trap |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script will very likely not work in case any of the packages include a
pre-link
orpost-link
script. Those scripts are intended to be run on the correct platform.