Created
May 6, 2025 17:17
-
-
Save ricardobarroslourenco/dea756aa2e91b0422b7942f1c9603206 to your computer and use it in GitHub Desktop.
Exporta conda env com versão fixa
This file contains hidden or 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 | |
# Script para exportação de ambiente conda com versionamento | |
# Ao final dar permissão de execução para script | |
# chmod +x export_env.sh | |
# Chamada de exportação seria: | |
# ./export_env.sh nome_do_ambiente caminho/para/environment.yml | |
# Verifica se os parâmetros foram fornecidos | |
if [ $# -ne 2 ]; then | |
echo "Uso: $0 <nome_do_ambiente> <caminho_para_output.yml>" | |
exit 1 | |
fi | |
ENV_NAME="$1" | |
OUTPUT_FILE="$2" | |
# Arquivos temporários | |
FULL_ENV="$(mktemp)" | |
MINIMAL_ENV="$(mktemp)" | |
VERSION_MAP="$(mktemp)" | |
# Exporta o ambiente completo sem informações de build | |
conda env export -n "$ENV_NAME" --no-builds > "$FULL_ENV" | |
# Exporta o ambiente com histórico (apenas pacotes explicitamente instalados) | |
conda env export -n "$ENV_NAME" --from-history > "$MINIMAL_ENV" | |
# Cria um mapa de versões dos pacotes a partir do ambiente completo | |
awk ' | |
BEGIN { in_dependencies = 0; } | |
/^dependencies:/ { in_dependencies = 1; next } | |
/^prefix:/ { in_dependencies = 0; next } | |
in_dependencies && /^ - / { | |
line = substr($0, 5) | |
split(line, fields, "=") | |
if (length(fields) > 1) { | |
print fields[1], fields[2] | |
} | |
} | |
' "$FULL_ENV" > "$VERSION_MAP" | |
# Atualiza o arquivo minimal com as versões dos pacotes | |
awk -v version_file="$VERSION_MAP" ' | |
BEGIN { | |
while ((getline line < version_file) > 0) { | |
split(line, fields, " ") | |
if (length(fields) > 1) { | |
versions[fields[1]] = fields[2] | |
} | |
} | |
close(version_file) | |
} | |
{ | |
if ($0 ~ /^ - /) { | |
pkg = substr($0, 5) | |
if (pkg in versions) { | |
print " - " pkg "=" versions[pkg] | |
} else { | |
print $0 | |
} | |
} else { | |
print $0 | |
} | |
} | |
' "$MINIMAL_ENV" > "$OUTPUT_FILE" | |
# Remove arquivos temporários | |
rm -f "$FULL_ENV" "$MINIMAL_ENV" "$VERSION_MAP" | |
echo "Arquivo '$OUTPUT_FILE' exportado com sucesso para o ambiente: $ENV_NAME" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment