def execute(args, parser): from conda.base.context import context name = args.remote_definition or args.name try: spec = specs.detect(name=name, filename=get_filename(args.file), directory=os.getcwd()) env = spec.environment # FIXME conda code currently requires args to have a name or prefix # don't overwrite name if it's given. gh-254 if args.prefix is None and args.name is None: args.name = env.name except exceptions.SpecNotFound: raise prefix = get_prefix(args, search=False) if args.force and prefix != context.root_prefix and os.path.exists(prefix): rm_rf(prefix) cli_install.check_prefix(prefix, json=args.json) # TODO, add capability # common.ensure_override_channels_requires_channel(args) # channel_urls = args.channel or () result = {"conda": None, "pip": None} 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)
class PrefixDatarUnitTests(TestCase): def setUp(self): tempdirdir = gettempdir() dirname = str(uuid4())[:8] self.prefix = join(tempdirdir, dirname) mkdir_p(self.prefix) assert isdir(self.prefix) mkdir_p(join(self.prefix, 'conda-meta')) activate_env_vars = join(self.prefix, PREFIX_STATE_FILE) with open(activate_env_vars, 'w') as f: f.write(ENV_VARS_FILE) self.pd = PrefixData(self.prefix) def tearDown(self): rm_rf(self.prefix) assert not lexists(self.prefix) def test_get_environment_env_vars(self): ex_env_vars = { "ENV_ONE": "one", "ENV_TWO": "you", "ENV_THREE": "me" } env_vars = self.pd.get_environment_env_vars() assert ex_env_vars == env_vars def test_set_unset_environment_env_vars(self): env_vars_one = { "ENV_ONE": "one", "ENV_TWO": "you", "ENV_THREE": "me", } env_vars_add = { "ENV_ONE": "one", "ENV_TWO": "you", "ENV_THREE": "me", "WOAH": "dude" } self.pd.set_environment_env_vars({"WOAH":"dude"}) env_vars = self.pd.get_environment_env_vars() assert env_vars_add == env_vars self.pd.unset_environment_env_vars(['WOAH']) env_vars = self.pd.get_environment_env_vars() assert env_vars_one == env_vars def test_set_unset_environment_env_vars_no_exist(self): env_vars_one = { "ENV_ONE": "one", "ENV_TWO": "you", "ENV_THREE": "me", } self.pd.unset_environment_env_vars(['WOAH']) env_vars = self.pd.get_environment_env_vars() assert env_vars_one == env_vars
def execute_set(args, parser): prefix = get_prefix(args, search=False) or context.active_prefix pd = PrefixData(prefix) if not lexists(prefix): raise EnvironmentLocationNotFound(prefix) env_vars_to_add = {} for v in args.vars: var_def = v.split('=') env_vars_to_add[var_def[0].strip()] = var_def[-1].strip() pd.set_environment_env_vars(env_vars_to_add) if prefix == context.active_prefix: print("To make your changes take effect please reactivate your environment")
def execute(args, parser): name = args.remote_definition or args.name try: spec = install_specs.detect(name=name, filename=get_filename(args.file), directory=os.getcwd()) env = spec.environment except exceptions.SpecNotFound: raise if not (args.name or args.prefix): if not env.name: # Note, this is a hack fofr get_prefix that assumes argparse results # TODO Refactor common.get_prefix name = os.environ.get('CONDA_DEFAULT_ENV', False) if not name: msg = "Unable to determine environment\n\n" msg += textwrap.dedent(""" Please re-run this command with one of the following options: * Provide an environment name via --name or -n * Re-run this command inside an activated conda environment.""" ).lstrip() # TODO Add json support raise CondaEnvException(msg) # Note: stubbing out the args object as all of the # conda.cli.common code thinks that name will always # be specified. args.name = env.name prefix = get_prefix(args, search=False) # CAN'T Check with this function since it assumes we will create prefix. # cli_install.check_prefix(prefix, json=args.json) # TODO, add capability # common.ensure_override_channels_requires_channel(args) # channel_urls = args.channel or () # create installers before running any of them # to avoid failure to import after the file being deleted # e.g. due to conda_env being upgraded or Python version switched. installers = {} for installer_type in env.dependencies: try: installers[installer_type] = get_installer(installer_type) except InvalidInstaller: sys.stderr.write( textwrap.dedent(""" Unable to install package for {0}. Please double check and ensure you 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 result = {"conda": None, "pip": None} for installer_type, specs in env.dependencies.items(): installer = installers[installer_type] result[installer_type] = installer.install(prefix, specs, args, env) if env.variables: pd = PrefixData(prefix) pd.set_environment_env_vars(env.variables) touch_nonadmin(prefix) print_result(args, prefix, result)