8000 Conda env create dry-run by AlbertDeFusco · Pull Request #10635 · conda/conda · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Conda env create dry-run #10635

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

Merged
merged 2 commits into from
May 24, 2021
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
82 changes: 52 additions & 30 deletions conda_env/cli/main_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from __future__ import print_function

from argparse import RawDescriptionHelpFormatter
import json
import os
import sys
import textwrap
Expand Down Expand Up @@ -68,6 +69,12 @@ def configure_parser(sub_parsers):
action='store_true',
default=False,
)
p.add_argument(
'-d', '--dry-run',
help='Only display what would have been done.',
action='store_true',
default=False
)
add_parser_default_packages(p)
add_parser_json(p)
p.set_defaults(func='.main_create.execute')
Expand Down Expand Up @@ -102,36 +109,51 @@ def execute(args, parser):
result = {"conda": None, "pip": None}

args_packages = context.create_default_packages if not args.no_default_packages else []
if args_packages:
installer_type = "conda"
installer = get_installer(installer_type)
result[installer_type] = installer.install(prefix, args_packages, args, env)

if len(env.dependencies.items()) == 0:
installer_type = "conda"
pkg_specs = []
if args.dry_run:
installer_type = 'conda'
installer = get_installer(installer_type)
result[installer_type] = installer.install(prefix, pkg_specs, args, env)

pkg_specs = env.dependencies.get(installer_type, [])
pkg_specs.extend(args_packages)

solved_env = installer.dry_run(pkg_specs, args, env)
if args.json:
print(json.dumps(solved_env.to_dict(), indent=2))
else:
print(solved_env.to_yaml(), end='')

else:
for installer_type, pkg_specs in env.dependencies.items():
try:
installer = get_installer(installer_type)
result[installer_type] = installer.install(prefix, pkg_specs, args, env)
except InvalidInstaller:
sys.stderr.write(textwrap.dedent("""
Unable to install package for {0}.

Please double check and ensure your dependencies file has
the correct spelling. You might also try installing the
conda-env-{0} package to see if provides the required
installer.
""").lstrip().format(installer_type)
)
return -1

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

touch_nonadmin(prefix)
print_result(args, prefix, result)
if args_packages:
installer_type = "conda"
installer = get_installer(installer_type)
result[installer_type] = installer.install(prefix, args_packages, args, env)

if len(env.dependencies.items()) == 0:
installer_type = "conda"
pkg_specs = []
installer = get_installer(installer_type)
result[installer_type] = installer.install(prefix, pkg_specs, args, env)
else:
for installer_type, pkg_specs in env.dependencies.items():
try:
installer = get_installer(installer_type)
result[installer_type] = installer.install(prefix, pkg_specs, args, env)
except InvalidInstaller:
sys.stderr.write(textwrap.dedent("""
Unable to install package for {0}.

Please double check and ensure your dependencies file has
the correct spelling. You might also try installing the
conda-env-{0} package to see if provides the required
installer.
""").lstrip().format(installer_type)
)
return -1

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

touch_nonadmin(prefix)
print_result(args, prefix, result)
21 changes: 20 additions & 1 deletion conda_env/installers/conda.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# SPDX-License-Identifier: BSD-3-Clause
from __future__ import absolute_import

import tempfile
from os.path import basename

from conda._vendor.boltons.setutils import IndexedSet
Expand All @@ -13,8 +14,9 @@
from conda.exceptions import UnsatisfiableError
from conda.models.channel import Channel, prioritize_channels

from ..env import Environment

def install(prefix, specs, args, env, *_, **kwargs):
def _solve(prefix, specs, args, env, *_, **kwargs):
# TODO: support all various ways this happens
# Including 'nodefaults' in the channels list disables the defaults
channel_urls = [chan for chan in env.channels if chan != 'nodefaults']
Expand All @@ -27,6 +29,23 @@ def install(prefix, specs, args, env, *_, **kwargs):
subdirs = IndexedSet(basename(url) for url in _channel_priority_map)

solver = Solver(prefix, channels, subdirs, specs_to_add=specs)
return solver


def dry_run(specs, args, env, *_, **kwargs):
solver = _solve(tempfile.mkdtemp(), specs, args, env, *_, **kwargs)
pkgs = solver.solve_final_state()
solved_env = Environment(
name=env.name,
dependencies=[str(p) for p in pkgs],
channels=env.channels
)
return solved_env


def install(prefix, specs, args, env, *_, **kwargs):
solver = _solve(prefix, specs, args, env, *_, **kwargs)

try:
unlink_link_transaction = solver.solve_for_transaction(
prune=getattr(args, 'prune', False), update_modifier=UpdateModifier.FREEZE_INSTALLED)
Expand Down
19 changes: 19 additions & 0 deletions tests/conda_env/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import os
import yaml

import pytest
import unittest
Expand Down Expand Up @@ -253,6 +254,24 @@ def test_create_valid_env(self):
len([env for env in parsed['envs'] if env.endswith(test_env_name_1)]), 0
)

def test_create_dry_run_yaml(self):
create_env(environment_1)
o, e = run_env_command(Commands.ENV_CREATE, None, '--dry-run')
self.assertFalse(env_is_created(test_env_name_1))

output = yaml.safe_load('\n'.join(o.splitlines()[2:]))
assert output['name'] == 'env-1'
assert len(output['dependencies']) > 0

def test_create_dry_run_json(self):
create_env(environment_1)
o, e = run_env_command(Commands.ENV_CREATE, None, '--dry-run', '--json')
self.assertFalse(env_is_created(test_env_name_1))

output = json.loads(o)
assert output.get('name') == 'env-1'
assert len(output['dependencies'])

def test_create_valid_env_with_variables(self):
'''
Creates an environment.yml file and
Expand Down
0