def _software_create_env()

in var/ramble/repos/builtin/package_managers/spack-lightweight/package_manager.py [0:0]


    def _software_create_env(self, workspace, app_inst=None):
        """Create the spack environment for this experiment

        Extract all specs this experiment uses, and write the spack environment
        file for it.
        """

        logger.msg("Creating Spack environment")

        # See if we cached this already, and if so return
        env_path = app_inst.expander.env_path
        if not env_path:
            raise ApplicationError("Ramble env_path is set to None.")

        cache_tupl = ("spack-env", env_path)
        if workspace.check_cache(cache_tupl):
            logger.debug(f"{cache_tupl} already in cache.")
            return
        else:
            workspace.add_to_cache(cache_tupl)

        package_manager_config_dicts = [app_inst.package_manager_configs]
        for mod_inst in app_inst._modifier_instances:
            package_manager_config_dicts.append(
                mod_inst.package_manager_configs
            )

        for config_dict in package_manager_config_dicts:
            for _, config in config_dict.items():
                keep_config = app_inst.expander.satisfies(
                    config["when"], variant_set=app_inst.object_variants
                )
                if keep_config:
                    self.runner.add_config(config["config"])

        try:
            self.runner.set_dry_run(workspace.dry_run)
            self.runner.create_env(
                app_inst.expander.expand_var_name(self.keywords.env_path)
            )
            self.runner.activate()

            # Write auxiliary software files into created spack env.
            for name, contents in workspace.all_auxiliary_software_files():
                aux_file_path = app_inst.expander.expand_var(
                    os.path.join(
                        app_inst.expander.expansion_str(
                            self.keywords.env_path
                        ),
                        f"{name}",
                    )
                )
                self.runner.add_include_file(aux_file_path)
                with open(aux_file_path, "w+") as f:
                    f.write(app_inst.expander.expand_var(contents))

            env_context = app_inst.expander.expand_var_name(
                self.keywords.env_name
            )
            require_env = self.environment_required()
            software_envs = workspace.software_environments
            software_env = software_envs.render_environment(
                env_context, app_inst.expander, self, require=require_env
            )
            if software_env is not None:
                if isinstance(software_env, ExternalEnvironment):
                    self.runner.copy_from_external_env(
                        software_env.external_env
                    )
                else:
                    for (
                        pkg_spec
                    ) in software_envs.package_specs_for_environment(
                        software_env
                    ):
                        self.runner.add_spec(pkg_spec)

                    self.runner.generate_env_file()

                added_packages = set(self.runner.added_packages())
                for pkg, conf in app_inst.required_packages.items():
                    if (
                        app_inst.expander.satisfies(
                            conf["when"], variant_set=app_inst.object_variants
                        )
                        and pkg not in added_packages
                    ):
                        logger.die(
                            f"Software spec {pkg} is not defined "
                            f"in environment {env_context}, but is "
                            f"required by the {self.name} application "
                            "definition"
                        )

                for mod_inst in app_inst._modifier_instances:
                    for pkg, conf in mod_inst.required_packages.items():
                        if (
                            app_inst.expander.satisfies(
                                conf["when"],
                                variant_set=app_inst.object_variants,
                            )
                            and pkg not in added_packages
                        ):
                            logger.die(
                                f"Software spec {pkg} is not defined "
                                f"in environment {env_context}, but is "
                                f"required by the {mod_inst.name} modifier "
                                "definition"
                            )

                self.runner.deactivate()

        except RunnerError as e:
            logger.die(e)