Пример #1
0
    def test_exec_env(self):
        with open("exec_env.py", "w") as f:
            f.write("""#!/usr/bin/env python\n
import os
import json
with open("exec_env_exec_env.json") as f:
     exec_env = json.load(f)
assert exec_env["TEST_ENV"] == "123"
assert exec_env["NOT_SET"] is None
                """)
        os.chmod("exec_env.py", stat.S_IEXEC + stat.S_IREAD)

        with open("EXEC_ENV", "w") as f:
            f.write("EXECUTABLE exec_env.py\n")
            f.write("EXEC_ENV TEST_ENV 123\n")
            f.write("EXEC_ENV NOT_SET")

        ext_job = ExtJob("EXEC_ENV", False)
        job_list = ExtJoblist()
        job_list.add_job("EXEC_ENV", ext_job)
        forward_model = ForwardModel(job_list)
        forward_model.add_job("EXEC_ENV")
        global_args = SubstitutionList()
        env_varlist = EnvironmentVarlist()
        forward_model.formatted_fprintf("run_id", None, "data_root",
                                        global_args, 0, env_varlist)

        for msg in list(JobRunner().run([])):
            if isinstance(msg, Start):
                with open("exec_env_exec_env.json") as f:
                    exec_env = json.load(f)
                    assert exec_env["TEST_ENV"] == "123"
                    assert exec_env["NOT_SET"] is None
Пример #2
0
    def test_exec_env(self):
        with TestAreaContext("exec_env_test"):
            with open("exec_env.py", "w") as f:
                f.write("""#!/usr/bin/env python\n
import os
import json
with open("exec_env_exec_env.json") as f:
     exec_env = json.load(f)
assert exec_env["TEST_ENV"] == "123"
assert exec_env["NOT_SET"] is None
                """)
            os.chmod("exec_env.py", stat.S_IEXEC + stat.S_IREAD)

            with open("EXEC_ENV", "w") as f:
                f.write("EXECUTABLE exec_env.py\n")
                f.write("EXEC_ENV TEST_ENV 123\n")
                f.write("EXEC_ENV NOT_SET")

            ext_job = ExtJob("EXEC_ENV", False)
            job_list = ExtJoblist()
            job_list.add_job("EXEC_ENV", ext_job)
            forward_model = ForwardModel(job_list)
            forward_model.add_job("EXEC_ENV")
            global_args = SubstitutionList()
            env_varlist = EnvironmentVarlist()
            forward_model.formatted_fprintf( "run_id", None, "data_root", global_args, 0, env_varlist)

            jobm = JobManager(json_file = "jobs.json")
            job0 = jobm[0]
            exec_env = job0.get("exec_env")
            self.assertEqual(len(exec_env), 2)
            exit_status, msg = jobm.runJob(job0)
            self.assertEqual(exit_status, 0)
Пример #3
0
    def __init__(
        self,
        data_root,
        joblist,
        last_history_restart,
        refcase,
        config_content=None,
        config_dict=None,
        is_reference=False,
    ):
        if config_dict is not None and config_content is not None:
            raise ValueError(
                "Error: Unable to create ModelConfig with multiple config objects"
            )

        hist_src_enum = ModelConfig._get_history_src_enum(
            config_dict, config_content)
        if hist_src_enum == HistorySourceEnum.SCHEDULE:
            raise ValueError("{} as {} is not supported".format(
                HistorySourceEnum.SCHEDULE, ConfigKeys.HISTORY_SOURCE))

        if config_dict is None:
            c_ptr = self._alloc(config_content, data_root, joblist,
                                last_history_restart, refcase)
        else:
            # MAX_RESAMPLE_KEY
            max_resample = config_dict.get(ConfigKeys.MAX_RESAMPLE)

            # NUM_REALIZATIONS_KEY
            num_realizations = config_dict.get(ConfigKeys.NUM_REALIZATIONS)

            # RUNPATH_KEY
            run_path = config_dict.get(ConfigKeys.RUNPATH)
            if run_path is not None:
                run_path = os.path.realpath(run_path)

            # DATA_ROOT_KEY
            data_root_from_config = config_dict.get(ConfigKeys.DATAROOT)
            if data_root_from_config is not None:
                data_root = os.path.realpath(data_root_from_config)

            # ENSPATH_KEY
            ens_path = config_dict.get(ConfigKeys.ENSPATH)
            if ens_path is not None:
                ens_path = os.path.realpath(ens_path)

            # JOBNAME_KEY
            job_name = config_dict.get(ConfigKeys.JOBNAME)

            # FORWARD_MODEL_KEY
            forward_model = ForwardModel(ext_joblist=joblist)
            # SIMULATION_JOB_KEY
            for job_description in config_dict.get(ConfigKeys.FORWARD_MODEL,
                                                   []):
                job = forward_model.add_job(job_description[ConfigKeys.NAME])
                job.set_private_args_as_string(
                    job_description.get(ConfigKeys.ARGLIST))
                job.convertToCReference(None)

            # SIMULATION_JOB_KEY
            for job_description in config_dict.get(ConfigKeys.SIMULATION_JOB,
                                                   []):
                job = forward_model.add_job(job_description[ConfigKeys.NAME])
                job.set_private_args_as_string(
                    job_description.get(ConfigKeys.ARGLIST))
                job.convertToCReference(None)

            # OBS_CONFIG_KEY
            obs_config = config_dict.get(ConfigKeys.OBS_CONFIG)
            if obs_config is not None:
                obs_config = os.path.realpath(obs_config)

            # TIME_MAP_KEY
            time_map = None
            time_map_file = config_dict.get(ConfigKeys.TIME_MAP)
            if time_map_file is not None and not os.path.isfile(
                    os.path.realpath(time_map_file)):
                raise ValueError("Error: Time map is not a file")
            elif time_map_file is not None:
                time_map = TimeMap()
                time_map.fload(filename=os.path.realpath(time_map_file))

            # RFTPATH_KEY
            rft_path = config_dict.get(ConfigKeys.RFTPATH)
            if rft_path is not None:
                rft_path = os.path.realpath(rft_path)

            # GEN_KW_EXPORT_NAME_KEY
            gen_kw_export_name = config_dict.get(ConfigKeys.GEN_KW_EXPORT_NAME)

            # HISTORY_SOURCE_KEY
            history_source = config_dict.get(ConfigKeys.HISTORY_SOURCE)

            c_ptr = self._alloc_full(
                max_resample,
                num_realizations,
                run_path,
                data_root,
                ens_path,
                job_name,
                forward_model,
                obs_config,
                time_map,
                rft_path,
                gen_kw_export_name,
                history_source,
                joblist,
                refcase,
            )

            # Fix ownership
            forward_model.convertToCReference(None)
            if time_map is not None:
                time_map.convertToCReference(None)

        if c_ptr is None:
            raise ValueError("Failed to construct ModelConfig instance.")

        super().__init__(c_ptr, is_reference=is_reference)