def set_variables(project, vars_and_values, env_spec_name=None): """Set variables' values in anaconda-project-local.yml. Returns a ``Status`` instance which evaluates to True on success and has an ``errors`` property (with a list of error strings) on failure. Args: project (Project): the project vars_and_values (list of tuple): key-value pairs env_spec_name (str): name of env spec to use Returns: ``Status`` instance """ (env_prefix, status) = _prepare_env_prefix(project, env_spec_name) if env_prefix is None: return status local_state = LocalStateFile.load_for_directory(project.directory_path) var_reqs = dict() for req in project.find_requirements(klass=EnvVarRequirement): var_reqs[req.env_var] = req present_vars = set(var_reqs.keys()) errors = [] local_state_count = 0 keyring_count = 0 for varname, value in vars_and_values: if varname in present_vars: if var_reqs[varname].encrypted: # import keyring locally because it's an optional dependency # that prints a warning when it's needed but not found. from anaconda_project.internal import keyring keyring.set(env_prefix, varname, value) keyring_count = keyring_count + 1 else: local_state.set_value(['variables', varname], value) local_state_count = local_state_count + 1 else: errors.append("Variable %s does not exist in the project." % varname) if errors: return SimpleStatus(success=False, description="Could not set variables.", errors=errors) else: if local_state_count > 0: local_state.save() if keyring_count == 0: description = ("Values saved in %s." % local_state.filename) elif local_state_count == 0: description = ("Values saved in the system keychain.") else: description = ( "%d values saved in %s, %d values saved in the system keychain." % (local_state_count, local_state.filename, keyring_count)) return SimpleStatus(success=True, description=description)
def test_get_set_using_mock(monkeypatch): passwords = _monkeypatch_keyring(monkeypatch) keyring.set("abc", "FOO", "bar") assert "bar" == keyring.get("abc", "FOO") assert dict(anaconda={'abc/FOO': 'bar'}) == passwords keyring.reset_keyring_module()
def test_unset_using_mock(monkeypatch): passwords = _monkeypatch_keyring(monkeypatch) keyring.set("abc", "FOO", "bar") assert "bar" == keyring.get("abc", "FOO") keyring.unset("abc", "FOO") assert keyring.get("abc", "FOO") is None assert dict(anaconda=dict()) == passwords keyring.reset_keyring_module()
def test_set_get_using_broken(monkeypatch, capsys): _monkeypatch_broken_keyring(monkeypatch) keyring.set("abc", "FOO", "bar") assert "bar" == keyring.get("abc", "FOO") (out, err) = capsys.readouterr() assert '' == out assert (expected_broken_message % "setting") == err keyring.reset_keyring_module()
def _set_encrypted_config_values_as_strings(self, requirement, environ, local_state_file, default_env_spec_name, overrides, values): # import keyring locally because it's an optional dependency # that prints a warning when it's needed but not found. import anaconda_project.internal.keyring as keyring env_prefix = self._get_env_prefix(environ) from_keyring = keyring.get(env_prefix, requirement.env_var) value_string = values.get('value', from_keyring) if value_string is not None: if value_string == '': keyring.unset(env_prefix, requirement.env_var) else: keyring.set(env_prefix, requirement.env_var, value_string)
def check(): keyring.set("abc", "FOO", "bar") assert "bar" == keyring.get("abc", "FOO") keyring.unset("abc", "FOO") assert keyring.get("abc", "FOO") is None
def check(): keyring.set("abc", "FOO", "bar") assert "bar" == keyring.get("abc", "FOO")