这是indexloc提供的服务,不要输入任何密码
Skip to content
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
5 changes: 5 additions & 0 deletions conda_env/cli/main_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from conda._vendor.auxlib.path import expand
from conda.cli import install as cli_install
from conda.cli.conda_argparse import add_parser_json, add_parser_prefix, add_parser_networking
from conda.core.prefix_data import PrefixData
from conda.gateways.connection.session import CONDA_SESSION_SCHEMES
from conda.gateways.disk.delete import rm_rf
from conda.misc import touch_nonadmin
Expand Down Expand Up @@ -127,5 +128,9 @@ def execute(args, parser):
)
return -1

if env.variables:
pd = PrefixData(prefix)
pd.set_environment_env_vars(env.variables)

touch_nonadmin(prefix)
print_result(args, prefix, result)
16 changes: 11 additions & 5 deletions conda_env/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from conda._vendor.toolz.itertoolz import concatv, groupby # NOQA


VALID_KEYS = ('name', 'dependencies', 'prefix', 'channels')
VALID_KEYS = ('name', 'dependencies', 'prefix', 'channels', 'variables')


def validate_keys(data, kwargs):
Expand Down Expand Up @@ -95,12 +95,14 @@ def from_environment(name, prefix, no_builds=False, ignore_channels=False, from_
Returns: Environment object
"""
# requested_specs_map = History(prefix).get_requested_specs_map()
pd = PrefixData(prefix, pip_interop_enabled=True)
variables = pd.get_environment_env_vars()

if from_history:
history = History(prefix).get_requested_specs_map()
deps = [str(package) for package in history.values()]
return Environment(name=name, dependencies=deps, channels=list(context.channels),
prefix=prefix)
pd = PrefixData(prefix, pip_interop_enabled=True)
prefix=prefix, variables=variables)

precs = tuple(PrefixGraph(pd.iter_records()).graph)
grouped_precs = groupby(lambda x: x.package_type, precs)
Expand Down Expand Up @@ -130,7 +132,8 @@ def from_environment(name, prefix, no_builds=False, ignore_channels=False, from_
canonical_name = prec.channel.canonical_name
if canonical_name not in channels:
channels.insert(0, canonical_name)
return Environment(name=name, dependencies=dependencies, channels=channels, prefix=prefix)
return Environment(name=name, dependencies=dependencies, channels=channels, prefix=prefix,
variables=variables)


def from_yaml(yamlstr, **kwargs):
Expand Down Expand Up @@ -215,11 +218,12 @@ def unique(seq, key=None):

class Environment(object):
def __init__(self, name=None, filename=None, channels=None,
dependencies=None, prefix=None):
dependencies=None, prefix=None, variables=None):
self.name = name
self.filename = filename
self.prefix = prefix
self.dependencies = Dependencies(dependencies)
self.variables = variables

if channels is None:
channels = []
Expand All @@ -237,6 +241,8 @@ def to_dict(self, stream=None):
d['channels'] = self.channels
if self.dependencies:
d['dependencies'] = self.dependencies.raw
if self.variables:
d['variables'] = self.variables
if self.prefix:
d['prefix'] = self.prefix
if stream is None:
Expand Down
15 changes: 15 additions & 0 deletions docs/source/user-guide/tasks/manage-environments.rst
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,21 @@ When you deactivate your environment, you can see that environment variable goes
``echo my_var`` or ``conda env config vars list`` to show that the variable name
is no longer present.

Environment variables set using ``conda env config vars`` will be retained in the output of
``conda env export``. Further, you can declare environment variables in the environment.yml file
as shown here::

name: env-name
channels:
- conda-forge
- defaults
dependencies:
- python=3.7
- codecov
variables:
VAR1: valueA
VAR2: valueB


Saving environment variables
============================
Expand Down
2 changes: 2 additions & 0 deletions tests/conda_env/support/valid_keys.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ channels:
- anaconda
dependencies:
- nltk
variables:
VARIABLE: value
prefix: /something/miniconda/envs/test
63 changes: 63 additions & 0 deletions tests/conda_env/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@
- defaults
'''

environment_1_with_variables = '''
name: env-1
dependencies:
- python
channels:
- defaults
variables:
DUDE: woah
SWEET: yaaa
'''

environment_2 = '''
name: env-1
dependencies:
Expand Down Expand Up @@ -242,6 +253,26 @@ def test_create_valid_env(self):
len([env for env in parsed['envs'] if env.endswith(test_env_name_1)]), 0
)

def test_create_valid_env_with_variables(self):
'''
Creates an environment.yml file and
creates and environment with it
'''

create_env(environment_1_with_variables)
run_env_command(Commands.ENV_CREATE, None)
self.assertTrue(env_is_created(test_env_name_1))

o, e = run_env_command(Commands.ENV_CONFIG, test_env_name_1, "vars", "list", "--json", '-n', test_env_name_1)
output_env_vars = json.loads(o)
assert output_env_vars == {'DUDE': 'woah', "SWEET": "yaaa"}

o, e = run_conda_command(Commands.INFO, None, "--json")
parsed = json.loads(o)
self.assertNotEqual(
len([env for env in parsed['envs'] if env.endswith(test_env_name_1)]), 0
)

@pytest.mark.integration
def test_conda_env_create_http(self):
'''
Expand Down Expand Up @@ -461,6 +492,38 @@ def test_env_export(self):
assert len(env_description['dependencies'])
for spec_str in env_description['dependencies']:
assert spec_str.count('=') == 1


run_env_command(Commands.ENV_REMOVE, test_env_name_2)
assert not env_is_created(test_env_name_2)

def test_env_export_with_variables(self):
"""
Test conda env export
"""

run_conda_command(Commands.CREATE, test_env_name_2, "flask")
assert env_is_created(test_env_name_2)

run_env_command(Commands.ENV_CONFIG, test_env_name_2, "vars", "set", "DUDE=woah", "SWEET=yaaa", "-n", test_env_name_2)

snowflake, e, = run_env_command(Commands.ENV_EXPORT, test_env_name_2)

with Utf8NamedTemporaryFile(mode="w", suffix=".yml", delete=False) as env_yaml:
env_yaml.write(snowflake)
env_yaml.flush()
env_yaml.close()

run_env_command(Commands.ENV_REMOVE, test_env_name_2)
self.assertFalse(env_is_created(test_env_name_2))
run_env_command(Commands.ENV_CREATE, None, "--file", env_yaml.name)
self.assertTrue(env_is_created(test_env_name_2))

snowflake, e = run_env_command(Commands.ENV_EXPORT, test_env_name_2, '--no-builds')
assert not e.strip()
env_description = yaml_safe_load(snowflake)
assert len(env_description['variables'])
assert env_description['variables'].keys()

run_env_command(Commands.ENV_REMOVE, test_env_name_2)
assert not env_is_created(test_env_name_2)
Expand Down