Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Obtain the envs dir directly from conda without using a subprocess #69

Merged
merged 1 commit into from
Aug 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
History
=======

Next
------------------

* Obtain ``envs_dir`` without using a subprocess (`#67`_).


.. _`#67`: https://github.com/ESSS/conda-devenv/issues/67

1.0.3 (2018-06-20)
------------------

Expand Down
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