@@ -48,6 +48,27 @@ def get_py_version(envconfig, action):
48
48
return "python={}" .format (version )
49
49
50
50
51
+ class CondaRunWrapper :
52
+ """A functor that execute a command via conda run.
53
+
54
+ It wraps popen so the command is executed in the context of an activated env.
55
+ """
56
+
57
+ CONDA_RUN_CMD_PREFIX = "{conda_exe} run --no-capture-output -p {envdir}"
58
+
59
+ def __init__ (self , venv , popen ):
60
+ self .__envdir = venv .envconfig .envdir
61
+ self .__conda_exe = venv .envconfig .conda_exe
62
+ self .__popen = popen
63
+
64
+ def __call__ (self , cmd_args , ** kwargs ):
65
+ conda_run_cmd_prefix = self .CONDA_RUN_CMD_PREFIX .format (
66
+ conda_exe = self .__conda_exe , envdir = self .__envdir
67
+ )
68
+ cmd_args = conda_run_cmd_prefix .split () + cmd_args
69
+ return self .__popen (cmd_args , ** kwargs )
70
+
71
+
51
72
@hookimpl
52
73
def tox_addoption (parser ):
53
74
parser .add_testenv_attribute (
@@ -87,7 +108,7 @@ def tox_configure(config):
87
108
# This is a pretty cheesy workaround. It allows tox to consider changes to
88
109
# the conda dependencies when it decides whether an existing environment
89
110
# needs to be updated before being used
90
- for _ , envconfig in config .envconfigs .items ():
111
+ for envconfig in config .envconfigs .values ():
91
112
# Make sure the right environment is activated. This works because we're
92
113
# creating environments using the `-p/--prefix` option in `tox_testenv_create`
93
114
envconfig .setenv ["CONDA_DEFAULT_ENV" ] = envconfig .setenv ["TOX_ENV_DIR" ]
@@ -161,6 +182,9 @@ def tox_testenv_create(venv, action):
161
182
pass
162
183
venv .envconfig .config .interpreters .get_executable (venv .envconfig )
163
184
185
+ # this will force commands and commands_{pre,post} to be executed via conda run
186
+ venv .popen = CondaRunWrapper (venv , venv .popen )
187
+
164
188
return True
165
189
166
190
@@ -210,8 +234,10 @@ def tox_testenv_install_deps(venv, action):
210
234
# to be present when we call pip install
211
235
venv .envconfig .deps = venv .envconfig .deps [: - 1 * num_conda_deps ]
212
236
213
- # Install dependencies from pypi here
237
+ # Install dependencies from pypi via conda run
238
+ action .via_popen = CondaRunWrapper (venv , action .via_popen )
214
239
tox .venv .tox_testenv_install_deps (venv = venv , action = action )
240
+
215
241
# Restore for the config file
216
242
venv .envconfig .deps = saved_deps
217
243
return True
0 commit comments