def test_set_rc_string(): # Test setting string keys in .condarc # We specifically test ssl_verify since it can be either a boolean or a string try: stdout, stderr = run_conda_command('config', '--file', test_condarc, '--set', 'ssl_verify', 'yes') assert stdout == '' assert stderr == '' verify = yaml_load(open(test_condarc, 'r'))['ssl_verify'] assert verify == 'yes' stdout, stderr = run_conda_command('config', '--file', test_condarc, '--set', 'ssl_verify', 'test_string.crt') assert stdout == '' assert stderr == '' verify = yaml_load(open(test_condarc, 'r'))['ssl_verify'] assert verify == 'test_string.crt' os.unlink(test_condarc) finally: try: os.unlink(test_condarc) except OSError: pass
def test_config_command_remove_force(): try: # Finally, test --remove, --remove-key run_conda_command('config', '--file', test_condarc, '--add', 'channels', 'test') run_conda_command('config', '--file', test_condarc, '--set', 'always_yes', 'true') stdout, stderr = run_conda_command('config', '--file', test_condarc, '--remove', 'channels', 'test') assert stdout == stderr == '' assert yaml_load(_read_test_condarc()) == { 'channels': ['defaults'], 'always_yes': True } stdout, stderr = run_conda_command('config', '--file', test_condarc, '--remove', 'channels', 'test', '--force') assert stdout == '' assert stderr == "Error: 'test' is not in the 'channels' key of the config file" stdout, stderr = run_conda_command('config', '--file', test_condarc, '--remove', 'disallow', 'python', '--force') assert stdout == '' assert stderr == "Error: key 'disallow' is not in the config file" stdout, stderr = run_conda_command('config', '--file', test_condarc, '--remove-key', 'always_yes', '--force') assert stdout == stderr == '' assert yaml_load(_read_test_condarc()) == {'channels': ['defaults']} stdout, stderr = run_conda_command('config', '--file', test_condarc, '--remove-key', 'always_yes', '--force') assert stdout == '' assert stderr == "Error: key 'always_yes' is not in the config file" os.unlink(test_condarc) finally: try: pass os.unlink(test_condarc) except OSError: pass
def test_config_command_remove_force(): try: # Finally, test --remove, --remove-key run_conda_command('config', '--file', test_condarc, '--add', 'channels', 'test') run_conda_command('config', '--file', test_condarc, '--set', 'always_yes', 'true') stdout, stderr = run_conda_command('config', '--file', test_condarc, '--remove', 'channels', 'test') assert stdout == stderr == '' assert yaml_load(_read_test_condarc()) == {'channels': ['defaults'], 'always_yes': True} stdout, stderr = run_conda_command('config', '--file', test_condarc, '--remove', 'channels', 'test', '--force') assert stdout == '' assert stderr == "Error: 'test' is not in the 'channels' key of the config file" stdout, stderr = run_conda_command('config', '--file', test_condarc, '--remove', 'disallow', 'python', '--force') assert stdout == '' assert stderr == "Error: key 'disallow' is not in the config file" stdout, stderr = run_conda_command('config', '--file', test_condarc, '--remove-key', 'always_yes', '--force') assert stdout == stderr == '' assert yaml_load(_read_test_condarc()) == {'channels': ['defaults']} stdout, stderr = run_conda_command('config', '--file', test_condarc, '--remove-key', 'always_yes', '--force') assert stdout == '' assert stderr == "Error: key 'always_yes' is not in the config file" os.unlink(test_condarc) finally: try: pass os.unlink(test_condarc) except OSError: pass
def load_condarc(path): if not path or not isfile(path): return {} with open(path) as f: return yaml_load(f) or {}
def execute_config(args, parser): json_warnings = [] json_get = {} if args.system: rc_path = config.sys_rc_path elif args.file: rc_path = args.file else: rc_path = config.user_rc_path # Create the file if it doesn't exist if not os.path.exists(rc_path): has_defaults = ['channels', 'defaults'] in args.add if args.add and 'channels' in list(zip(*args.add))[0] and not has_defaults: # If someone adds a channel and their .condarc doesn't exist, make # sure it includes the defaults channel, or else they will end up # with a broken conda. rc_text = """\ channels: - defaults """ else: rc_text = "" else: with open(rc_path, 'r') as rc: rc_text = rc.read() rc_config = yaml_load(rc_text) if rc_config is None: rc_config = {} # Get if args.get is not None: if args.get == []: args.get = sorted(rc_config.keys()) for key in args.get: if key not in config.rc_list_keys + config.rc_bool_keys + config.rc_string_keys: if key not in config.rc_other: message = "unknown key %s" % key if not args.json: print(message, file=sys.stderr) else: json_warnings.append(message) continue if key not in rc_config: continue if args.json: json_get[key] = rc_config[key] continue if isinstance(rc_config[key], (bool, string_types)): print("--set", key, rc_config[key]) else: # Note, since conda config --add prepends, these are printed in # the reverse order so that entering them in this order will # recreate the same file for item in reversed(rc_config.get(key, [])): # Use repr so that it can be pasted back in to conda config --add print("--add", key, repr(item)) # Add for key, item in args.add: if key not in config.rc_list_keys: common.error_and_exit("key must be one of %s, not %r" % (', '.join(config.rc_list_keys), key), json=args.json, error_type="ValueError") if not isinstance(rc_config.get(key, []), list): bad = rc_config[key].__class__.__name__ raise CouldntParse("key %r should be a list, not %s." % (key, bad)) if key == 'default_channels' and rc_path != config.sys_rc_path: msg = "'default_channels' is only configurable for system installs" raise NotImplementedError(msg) if item in rc_config.get(key, []): # Right now, all list keys should not contain duplicates message = "Skipping %s: %s, item already exists" % (key, item) if not args.json: print(message, file=sys.stderr) else: json_warnings.append(message) continue rc_config.setdefault(key, []).insert(0, item) # Set set_bools, set_strings = set(config.rc_bool_keys), set(config.rc_string_keys) for key, item in args.set: # Check key and value yamlitem = yaml_load(item) if key in set_bools: if not isinstance(yamlitem, bool): common.error_and_exit("Key: %s; %s is not a YAML boolean." % (key, item), json=args.json, error_type="TypeError") rc_config[key] = yamlitem elif key in set_strings: rc_config[key] = yamlitem else: common.error_and_exit("Error key must be one of %s, not %s" % (', '.join(set_bools | set_strings), key), json=args.json, error_type="ValueError") # Remove for key, item in args.remove: if key not in rc_config: common.error_and_exit("key %r is not in the config file" % key, json=args.json, error_type="KeyError") if item not in rc_config[key]: common.error_and_exit("%r is not in the %r key of the config file" % (item, key), json=args.json, error_type="KeyError") rc_config[key] = [i for i in rc_config[key] if i != item] # Remove Key for key, in args.remove_key: if key not in rc_config: common.error_and_exit("key %r is not in the config file" % key, json=args.json, error_type="KeyError") del rc_config[key] # config.rc_keys with open(rc_path, 'w') as rc: rc.write(yaml_dump(rc_config)) if args.json: common.stdout_json_success( rc_path=rc_path, warnings=json_warnings, get=json_get ) return
def test_config_command_parser(): try: # Now test the YAML "parser" # Channels is normal content. # create_default_packages has extra spaces in list items condarc = """\ channels: - test - defaults create_default_packages : - ipython - numpy changeps1: false # Here is a comment always_yes: yes """ # First verify that this itself is valid YAML assert yaml_load(condarc) == { 'channels': ['test', 'defaults'], 'create_default_packages': ['ipython', 'numpy'], 'changeps1': False, 'always_yes': 'yes' } with open(test_condarc, 'w') as f: f.write(condarc) stdout, stderr = run_conda_command('config', '--file', test_condarc, '--get') assert stdout == """\ --set always_yes yes --set changeps1 False --add channels 'defaults' --add channels 'test' --add create_default_packages 'numpy' --add create_default_packages 'ipython'\ """ stdout, stderr = run_conda_command('config', '--file', test_condarc, '--add', 'channels', 'mychannel') assert stdout == stderr == '' assert _read_test_condarc() == """\ channels: - mychannel - test - defaults create_default_packages: - ipython - numpy changeps1: false # Here is a comment always_yes: 'yes' """ stdout, stderr = run_conda_command('config', '--file', test_condarc, '--set', 'changeps1', 'true') assert stdout == stderr == '' assert _read_test_condarc() == """\ channels: - mychannel - test - defaults create_default_packages: - ipython - numpy changeps1: true # Here is a comment always_yes: 'yes' """ os.unlink(test_condarc) # Test adding a new list key. We couldn't test this above because it # doesn't work yet with odd whitespace condarc = """\ channels: - test - defaults always_yes: true """ with open(test_condarc, 'w') as f: f.write(condarc) stdout, stderr = run_conda_command('config', '--file', test_condarc, '--add', 'disallow', 'perl') assert stdout == stderr == '' assert _read_test_condarc() == condarc + """\ disallow: - perl """ os.unlink(test_condarc) finally: try: pass os.unlink(test_condarc) except OSError: pass
def execute_config(args, parser): json_warnings = [] json_get = {} if args.system: rc_path = sys_rc_path elif args.file: rc_path = args.file else: rc_path = user_rc_path # Create the file if it doesn't exist if not os.path.exists(rc_path): has_defaults = ['channels', 'defaults'] in args.add if args.add and 'channels' in list( zip(*args.add))[0] and not has_defaults: # If someone adds a channel and their .condarc doesn't exist, make # sure it includes the defaults channel, or else they will end up # with a broken conda. rc_text = """\ channels: - defaults """ else: rc_text = "" else: with open(rc_path, 'r') as rc: rc_text = rc.read() rc_config = yaml_load(rc_text) if rc_config is None: rc_config = {} # Get if args.get is not None: if args.get == []: args.get = sorted(rc_config.keys()) for key in args.get: if key not in rc_list_keys + rc_bool_keys + rc_string_keys: if key not in rc_other: message = "unknown key %s" % key if not args.json: print(message, file=sys.stderr) else: json_warnings.append(message) continue if key not in rc_config: continue if args.json: json_get[key] = rc_config[key] continue if isinstance(rc_config[key], (bool, string_types)): print("--set", key, rc_config[key]) else: # Note, since conda config --add prepends, these are printed in # the reverse order so that entering them in this order will # recreate the same file for item in reversed(rc_config.get(key, [])): # Use repr so that it can be pasted back in to conda config --add print("--add", key, repr(item)) # Add for key, item in args.add: if key not in rc_list_keys: common.error_and_exit("key must be one of %s, not %r" % (', '.join(rc_list_keys), key), json=args.json, error_type="ValueError") if not isinstance(rc_config.get(key, []), list): bad = rc_config[key].__class__.__name__ raise CouldntParse("key %r should be a list, not %s." % (key, bad)) if key == 'default_channels' and rc_path != sys_rc_path: msg = "'default_channels' is only configurable for system installs" raise NotImplementedError(msg) if item in rc_config.get(key, []): # Right now, all list keys should not contain duplicates message = "Skipping %s: %s, item already exists" % (key, item) if not args.json: print(message, file=sys.stderr) else: json_warnings.append(message) continue rc_config.setdefault(key, []).insert(0, item) # Set set_bools, set_strings = set(rc_bool_keys), set(rc_string_keys) for key, item in args.set: # Check key and value yamlitem = yaml_load(item) if key in set_bools: if not isinstance(yamlitem, bool): common.error_and_exit("Key: %s; %s is not a YAML boolean." % (key, item), json=args.json, error_type="TypeError") rc_config[key] = yamlitem elif key in set_strings: rc_config[key] = yamlitem else: common.error_and_exit("Error key must be one of %s, not %s" % (', '.join(set_bools | set_strings), key), json=args.json, error_type="ValueError") # Remove for key, item in args.remove: if key not in rc_config: common.error_and_exit("key %r is not in the config file" % key, json=args.json, error_type="KeyError") if item not in rc_config[key]: common.error_and_exit( "%r is not in the %r key of the config file" % (item, key), json=args.json, error_type="KeyError") rc_config[key] = [i for i in rc_config[key] if i != item] # Remove Key for key, in args.remove_key: if key not in rc_config: common.error_and_exit("key %r is not in the config file" % key, json=args.json, error_type="KeyError") del rc_config[key] # config.rc_keys with open(rc_path, 'w') as rc: rc.write(yaml_dump(rc_config)) if args.json: common.stdout_json_success(rc_path=rc_path, warnings=json_warnings, get=json_get) return
def test_config_command_parser(): try: # Now test the YAML "parser" # Channels is normal content. # create_default_packages has extra spaces in list items condarc = """\ channels: - test - defaults create_default_packages : - ipython - numpy changeps1: false # Here is a comment always_yes: yes """ # First verify that this itself is valid YAML assert yaml_load(condarc) == {'channels': ['test', 'defaults'], 'create_default_packages': ['ipython', 'numpy'], 'changeps1': False, 'always_yes': 'yes'} with open(test_condarc, 'w') as f: f.write(condarc) stdout, stderr = run_conda_command('config', '--file', test_condarc, '--get') assert stdout == """\ --set always_yes yes --set changeps1 False --add channels 'defaults' --add channels 'test' --add create_default_packages 'numpy' --add create_default_packages 'ipython'\ """ stdout, stderr = run_conda_command('config', '--file', test_condarc, '--add', 'channels', 'mychannel') assert stdout == stderr == '' assert _read_test_condarc() == """\ channels: - mychannel - test - defaults create_default_packages: - ipython - numpy changeps1: false # Here is a comment always_yes: 'yes' """ stdout, stderr = run_conda_command('config', '--file', test_condarc, '--set', 'changeps1', 'true') assert stdout == stderr == '' assert _read_test_condarc() == """\ channels: - mychannel - test - defaults create_default_packages: - ipython - numpy changeps1: true # Here is a comment always_yes: 'yes' """ os.unlink(test_condarc) # Test adding a new list key. We couldn't test this above because it # doesn't work yet with odd whitespace condarc = """\ channels: - test - defaults always_yes: true """ with open(test_condarc, 'w') as f: f.write(condarc) stdout, stderr = run_conda_command('config', '--file', test_condarc, '--add', 'disallow', 'perl') assert stdout == stderr == '' assert _read_test_condarc() == condarc + """\ disallow: - perl """ os.unlink(test_condarc) finally: try: pass os.unlink(test_condarc) except OSError: pass