2
2
"""
3
3
Freeze the conda environment.yml
4
4
5
- It runs the freeze in a continuumio/miniconda3 image to ensure portability
5
+ Using conda-lock
6
6
7
7
Usage:
8
8
19
19
from ruamel .yaml import YAML
20
20
21
21
22
- DOCKER_IMAGE = "condaforge/mambaforge:4.9.2-5"
23
- # set mamba and/or conda versions
24
- # if needed to differ from what's in the image
25
- MAMBA_VERSION = ""
26
- CONDA_VERSION = ""
27
-
28
22
HERE = pathlib .Path (os .path .dirname (os .path .abspath (__file__ )))
29
23
30
24
ENV_FILE = HERE / "environment.yml"
36
30
yaml = YAML (typ = "rt" )
37
31
38
32
39
- def freeze (env_file , frozen_file ):
33
+ def freeze (env_file , frozen_file , platform = "linux-64" ):
40
34
"""Freeze a conda environment.yml
41
35
42
- By running in docker:
36
+ By running
43
37
44
- mamba env create
45
- mamba env export
38
+ conda-lock --mamba --platform=linux-64 -f environment.yml
46
39
47
40
Result will be stored in frozen_file
48
41
"""
@@ -58,43 +51,32 @@ def freeze(env_file, frozen_file):
58
51
return
59
52
print (f"Freezing { env_file } -> { frozen_file } " )
60
53
54
+ # FIXME: conda-lock 0.8 requires {platform} in template
55
+ # https://github.com/conda-incubator/conda-lock/pull/78
56
+ frozen_template = str (frozen_dest ) + ".{platform}"
57
+ frozen_tempfile = pathlib .Path (frozen_template .format (platform = platform ))
58
+
59
+ check_call (
60
+ [
61
+ "conda-lock" ,
62
+ # FIXME: adopt micromamba after ordering is fixed
63
+ # https://github.com/conda-incubator/conda-lock/issues/79
64
+ "--mamba" ,
65
+ f"--platform={ platform } " ,
66
+ f"--filename-template={ frozen_template } " ,
67
+ f"--file={ env_file } " ,
68
+ ]
69
+ )
70
+
61
71
with frozen_dest .open ("w" ) as f :
62
72
f .write (
63
73
f"# AUTO GENERATED FROM { env_file .relative_to (HERE )} , DO NOT MANUALLY MODIFY\n "
64
74
)
65
75
f .write (f"# Frozen on { datetime .utcnow ():%Y-%m-%d %H:%M:%S UTC} \n " )
76
+ with frozen_tempfile .open () as temp :
77
+ f .write (temp .read ())
66
78
67
- check_call (
68
- [
69
- "docker" ,
70
- "run" ,
71
- "--rm" ,
72
- "-v" f"{ HERE } :/r2d" ,
73
- "-it" ,
74
- DOCKER_IMAGE ,
75
- "sh" ,
76
- "-c" ,
77
- "; " .join (
78
- [
79
- "set -ex" ,
80
- "conda config --set channel_priority strict" ,
81
- "conda config --add channels conda-forge" ,
82
- f"mamba install -yq -S mamba={ MAMBA_VERSION } "
83
- if MAMBA_VERSION
84
- else "true" ,
85
- f"mamba install -yq -S conda={ CONDA_VERSION } "
86
- if CONDA_VERSION
87
- else "true" ,
88
- "conda config --system --set auto_update_conda false" ,
89
- f"mamba env create -v -f /r2d/{ env_file .relative_to (HERE )} -n r2d" ,
90
- # add conda-forge broken channel as lowest priority in case
91
- # any of our frozen packages are marked as broken after freezing
92
- "conda config --append channels conda-forge/label/broken" ,
93
- f"mamba env export -n r2d >> /r2d/{ frozen_file .relative_to (HERE )} " ,
94
- ]
95
- ),
96
- ]
97
- )
79
+ os .remove (frozen_tempfile )
98
80
99
81
100
82
def set_python (py_env_file , py ):
0 commit comments