Exemplo n.º 1
0
    def check(dirname):
        env_var = conda_api.conda_prefix_variable()

        try:
            _push_fake_env_creator()
            project = Project(dirname)
            environ = minimal_environ()
            result = prepare_without_interaction(project,
                                                 environ=environ,
                                                 env_spec_name='foo')
            expected_path = project.env_specs['foo'].path(
                project.directory_path)
            assert result.environ[env_var] == expected_path

            environ = minimal_environ()
            result = prepare_without_interaction(project,
                                                 environ=environ,
                                                 env_spec_name='bar')
            assert result.errors == []
            assert result
            expected_path = project.env_specs['bar'].path(
                project.directory_path)
            assert result.environ[env_var] == expected_path
        finally:
            _pop_fake_env_creator()
Exemplo n.º 2
0
 def missing_env_vars_to_configure(self, requirement, environ,
                                   local_state_file):
     """Override superclass to require env prefix."""
     if self._get_env_prefix(environ) is not None:
         return ()
     else:
         return (conda_api.conda_prefix_variable(), )
Exemplo n.º 3
0
    def __init__(self, registry, env_specs=None):
        """Extend superclass to default to CONDA_PREFIX and carry environment information.

        Args:
            registry (RequirementsRegistry): plugin registry
            env_specs (dict): dict from env name to ``CondaEnvironment``
        """
        super(CondaEnvRequirement,
              self).__init__(registry=registry,
                             env_var=conda_api.conda_prefix_variable())
        self.env_specs = env_specs
        self._conda = new_conda_manager()
    def exec_info_for_environment(self, environ, extra_args=None):
        """Get a ``CommandExecInfo`` ready to be executed.

        Args:
            environ (dict): the environment containing a CONDA_PREFIX, PATH, and PROJECT_DIR
            extra_args (list of str): extra args to append to the command line
        Returns:
            argv as list of strings
        """
        conda_var = conda_api.conda_prefix_variable()
        for name in (conda_var, 'PATH', 'PROJECT_DIR'):
            if name not in environ:
                raise ValueError(
                    "To get a runnable command for the app, %s must be set." %
                    (name))

        (args, shell) = self._choose_args_and_shell(environ, extra_args)

        if args is None:
            # command doesn't work on our platform for example
            return None

        # always look in the project directory. This is a little
        # odd because we don't add PROJECT_DIR to PATH for child
        # processes - maybe we should?
        path = os.pathsep.join([environ['PROJECT_DIR'], environ['PATH']])

        # if we're using a shell, then args[0] is a whole command
        # line and not a single program name, and the shell will
        # search the path for us.
        if not shell:
            executable = spawn.find_executable(args[0], path)
            # if we didn't find args[0] on the path, we leave it as-is
            # and wait for it to fail when we later try to run it.
            if executable is not None:
                # if the executable is in cwd, for some reason spawn.find_executable does not
                # return the full path to it, just a relative path.
                args[0] = os.path.abspath(executable)

        # conda.misc.launch() uses the home directory
        # instead of the project directory as cwd when
        # running an installed package, but for our
        # purposes where we know we have a project dir
        # that's user-interesting, project directory seems
        # more useful. This way apps can for example find
        # sample data files relative to the project
        # directory.
        return CommandExecInfo(cwd=environ['PROJECT_DIR'],
                               args=args,
                               env=environ,
                               shell=shell)
Exemplo n.º 5
0
    def check(dirname):
        env_var = conda_api.conda_prefix_variable()

        try:
            _push_fake_env_creator()
            project = Project(dirname)
            environ = minimal_environ()
            # we specify the command name but not the
            # env_spec_name but it should imply the proper env
            # spec name.
            result = prepare_without_interaction(project, environ=environ, command_name='hello')
            expected_path = project.env_specs['foo'].path(project.directory_path)
            assert result.environ[env_var] == expected_path
        finally:
            _pop_fake_env_creator()
def test_conda_variable_when_have_all_three(monkeypatch):
    monkeypatch.setattr(
        'os.environ',
        dict(CONDA_ENV_PATH='foo', CONDA_DEFAULT_ENV='bar',
             CONDA_PREFIX='baz'))
    assert conda_api.conda_prefix_variable() == 'CONDA_PREFIX'
def test_conda_variable_when_have_only_default_env(monkeypatch):
    monkeypatch.setattr('os.environ', dict(CONDA_DEFAULT_ENV='foo'))
    assert conda_api.conda_prefix_variable() == 'CONDA_DEFAULT_ENV'
def test_conda_variable_when_not_in_conda(monkeypatch):
    monkeypatch.setattr('os.environ', dict())
    assert conda_api.conda_prefix_variable() == 'CONDA_PREFIX'
from anaconda_project.internal.test.test_conda_api import monkeypatch_conda_not_to_use_links
from anaconda_project.prepare import (prepare_without_interaction,
                                      prepare_in_stages, unprepare)
from anaconda_project.project_file import DEFAULT_PROJECT_FILENAME
from anaconda_project.project import Project
from anaconda_project import provide
from anaconda_project.requirements_registry.registry import RequirementsRegistry
from anaconda_project.requirements_registry.providers.conda_env import CondaEnvProvider
from anaconda_project.requirements_registry.requirements.conda_env import CondaEnvRequirement

if platform.system() == 'Windows':
    script_dir = "Scripts"
else:
    script_dir = "bin"

conda_env_var = conda_api.conda_prefix_variable()


def test_find_by_class_name_conda_env():
    registry = RequirementsRegistry()
    found = registry.find_provider_by_class_name(class_name="CondaEnvProvider")
    assert found is not None
    assert isinstance(found, CondaEnvProvider)


@pytest.mark.slow
def test_prepare_and_unprepare_project_scoped_env(monkeypatch):
    monkeypatch_conda_not_to_use_links(monkeypatch)

    def prepare_project_scoped_env(dirname):
        project = Project(dirname)
Exemplo n.º 10
0
# -*- coding: utf-8 -*-
# ----------------------------------------------------------------------------
# Copyright © 2016, Continuum Analytics, Inc. All rights reserved.
#
# The full license is in the file LICENSE.txt, distributed with this software.
# ----------------------------------------------------------------------------
from __future__ import absolute_import, print_function

from anaconda_project.internal.cli.variable_commands import main_add, main_remove
from anaconda_project.internal.cli.main import _parse_args_and_run_subcommand
from anaconda_project.internal.test.tmpfile_utils import with_directory_contents_completing_project_file
from anaconda_project.internal.simple_status import SimpleStatus
from anaconda_project.internal import conda_api
from anaconda_project.project_file import DEFAULT_PROJECT_FILENAME

PLATFORM_ENV_VAR = conda_api.conda_prefix_variable()


class Args(object):
    def __init__(self,
                 vars_to_add=None,
                 vars_to_remove=None,
                 directory='.',
                 default=None,
                 env_spec=None):
        self.directory = directory
        self.vars_to_add = vars_to_add
        self.vars_to_remove = vars_to_remove
        self.default = None
        self.env_spec = None
Exemplo n.º 11
0
 def _get_env_prefix(self, environ):
     # on unix, ENV_PATH is the prefix and DEFAULT_ENV can be just a name,
     # on windows DEFAULT_ENV is always the prefix
     return environ.get(conda_api.conda_prefix_variable(), None)
Exemplo n.º 12
0
    def exec_info_for_environment(self, environ, extra_args=None):
        """Get a ``CommandExecInfo`` ready to be executed.

        Args:
            environ (dict): the environment containing a CONDA_PREFIX, PATH, and PROJECT_DIR
            extra_args (list of str): extra args to append to the command line
        Returns:
            argv as list of strings
        """
        conda_var = conda_api.conda_prefix_variable()
        for name in (conda_var, 'PATH', 'PROJECT_DIR'):
            if name not in environ:
                raise ValueError(
                    "To get a runnable command for the app, %s must be set." %
                    (name))

        (args, shell) = self._choose_args_and_shell(environ, extra_args)

        if args is None:
            # command doesn't work on our platform for example
            return None

        # always look in the project directory. This is a little
        # odd because we don't add PROJECT_DIR to PATH for child
        # processes - maybe we should?
        path = os.pathsep.join([environ['PROJECT_DIR'], environ['PATH']])

        # if we're using a shell, then args[0] is a whole command
        # line and not a single program name, and the shell will
        # search the path for us.
        if not shell:
            # We used to use spawn.find_executable but it is not
            # consistent across platforms. Specifically, for Windows
            # it seems to insist unnecessarily on an .exe extension
            # (unnecessary for our purposes at least)
            extensions = ['', '.bat', '.exe'] if _is_windows() else ['']
            executable = None
            for pdir in path.split(os.pathsep):
                for ext in extensions:
                    fpath = os.path.join(pdir, args[0] + ext)
                    if os.path.exists(fpath):
                        executable = fpath
                        break
                if executable is not None:
                    args[0] = os.path.abspath(executable)
                    break
            # if we didn't find args[0] on the path, we leave it as-is
            # and wait for it to fail when we later try to run it.

        # conda.misc.launch() uses the home directory
        # instead of the project directory as cwd when
        # running an installed package, but for our
        # purposes where we know we have a project dir
        # that's user-interesting, project directory seems
        # more useful. This way apps can for example find
        # sample data files relative to the project
        # directory.
        return CommandExecInfo(cwd=environ['PROJECT_DIR'],
                               args=args,
                               env=environ,
                               shell=shell)