Skip to content

Commit

Permalink
Obtain the envs dir directly from conda without using a subprocess
Browse files Browse the repository at this point in the history
  • Loading branch information
prusse-martin committed Aug 14, 2018
1 parent 22e0b1a commit dcd946c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
15 changes: 10 additions & 5 deletions conda_devenv/devenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,12 +451,17 @@ def get_env_name(args, output_filename, conda_yaml_dict=None):
return conda_yaml_dict['name']


def _get_envs_dirs_from_conda():
from conda.base.context import context
return context.envs_dirs


def get_env_directory(env_name):
import subprocess
import json
info = subprocess.check_output(["conda", "info", "--json"]).decode()
info = json.loads(info)
envs_dirs = info["envs_dirs"]
"""
:rtype: Optional[str]
:return: The environment path if the enviromment exists.
"""
envs_dirs = _get_envs_dirs_from_conda()

for directory in envs_dirs:
env = os.path.join(directory, env_name)
Expand Down
16 changes: 8 additions & 8 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,14 @@ def test_get_env_directory(mocker, tmpdir):
env_1 = tmpdir.join('1/envs/my_env').ensure(dir=1)
conda_meta_env_1 = tmpdir.join('1/envs/my_env/conda-meta').ensure(dir=1)

# Replacing separators because of Windows
mock_output = '''
{{
"envs": ["{0}", "{1}"],
"envs_dirs": ["{2}"]
}}
'''.format(str(env_0), str(env_1), str(tmpdir.join('1/envs'))).replace('\\','\\\\').encode()
mocker.patch('subprocess.check_output', return_value=mock_output)
mocker.patch('subprocess.check_output', side_effect=AssertionError())
mocker.patch.object(
devenv, '_get_envs_dirs_from_conda',
return_value=[
str(tmpdir.join('0/envs')),
str(tmpdir.join('1/envs')),
],
)

obtained = devenv.get_env_directory('my_env')
assert obtained == str(env_1)
Expand Down

0 comments on commit dcd946c

Please sign in to comment.