def merge_conda_files(files: List[Path], result_file: Path) -> None: """ Merges the given Conda environment files using the conda_merge package, and writes the merged file to disk. :param files: The Conda environment files to read. :param result_file: The location where the merge results should be written. """ # This code is a slightly modified version of conda_merge. That code can't be re-used easily # it defaults to writing to stdout env_definitions = [conda_merge.read_file(str(f)) for f in files] unified_definition = {} NAME = "name" CHANNELS = "channels" DEPENDENCIES = "dependencies" name = conda_merge.merge_names(env.get(NAME) for env in env_definitions) if name: unified_definition[NAME] = name try: channels = conda_merge.merge_channels( env.get(CHANNELS) for env in env_definitions) except conda_merge.MergeError: logging.error("Failed to merge channel priorities.") raise if channels: unified_definition[CHANNELS] = channels deps = conda_merge.merge_dependencies( env.get(DEPENDENCIES) for env in env_definitions) if deps: unified_definition[DEPENDENCIES] = deps with result_file.open("w") as f: ruamel.yaml.dump(unified_definition, f, indent=2, default_flow_style=False)
def test_merge_names(): """Should leave only the last name""" names = ['a', None, 'b', 'c', None, ''] assert cm.merge_names(names) == 'c' assert cm.merge_names([]) is None assert cm.merge_names([None, None]) is None