Example #1
0
def get_test_parameters(infile=None, count=0):
    infile = infile or INPUT_FILE

    try:
        with open(".ROOT_DIR", "r") as fp:
            root_dir = fp.read()
    except IOError:
        root_dir = "."
    if count > 1:
        raise RuntimeError()
    count += 1

    bmi = Bmi()
    with cd(root_dir):
        bmi.initialize(infile)

        in_names = set(bmi.get_input_var_names())
        out_names = set(bmi.get_output_var_names())
        grids = set(all_grids(bmi))

        # in_names = set()
        # out_names = set()
        # grids = set()

        bmi.finalize()

    meta = {
        "gid": grids,
        "var_name": in_names | out_names,
        "in_var_name": in_names - out_names,
        "out_var_name": out_names,
    }

    return meta
Example #2
0
    def finalize(self):
        """Finalize the event.

        Run the `finalize` method of the underlying port.
        """
        with cd(self._run_dir):
            status = {"name": self.name, "time": None, "status": "finishing"}
            self._status_fp.write(yaml.dump(status))
            self._port.finalize()
        status = {"name": self.name, "time": None, "status": "finished"}
        self._status_fp.write(yaml.dump(status))
        self._status_fp.close()
Example #3
0
File: port.py Project: csdms/pymt
    def finalize(self):
        """Finalize the event.

        Run the `finalize` method of the underlying port.
        """
        with cd(self._run_dir):
            status = {"name": self.name, "time": None, "status": "finishing"}
            self._status_fp.write(yaml.dump(status))
            self._port.finalize()
        status = {"name": self.name, "time": None, "status": "finished"}
        self._status_fp.write(yaml.dump(status))
        self._status_fp.close()
Example #4
0
    def initialize(self):
        """Initialize the event.

        Run the underlying port's initialization method in its *run_dir*. The event's *init_args* are passed to
        the initialize method as arguments.
        """
        with cd(self._run_dir):
            status = {"name": self.name, "time": 0.0, "status": "initializing"}
            self._status_fp.write(yaml.dump(status))
            try:
                self._port.initialize(*self._init_args)
            except Exception:
                raise
Example #5
0
File: port.py Project: csdms/pymt
    def initialize(self):
        """Initialize the event.

        Run the underlying port's initialization method in its *run_dir*. The event's *init_args* are passed to
        the initialize method as arguments.
        """
        with cd(self._run_dir):
            status = {"name": self.name, "time": 0.0, "status": "initializing"}
            self._status_fp.write(yaml.dump(status))
            try:
                self._port.initialize(*self._init_args)
            except Exception:
                raise
Example #6
0
    def run(self, time):
        """Run the event.

        Call the `run` method of the underlying port using *time* as the run time.

        Parameters
        ----------
        time : float
            Time to run the event to.
        """
        with cd(self._run_dir):
            status = {"name": self.name, "time": time, "status": "running"}
            print(yaml.dump(status), file=self._status_fp)
            self._status_fp.flush()
            sys.stdout.flush()
            sys.stderr.flush()
            print(yaml.dump(status))

            self._port.run(time)

            sys.stdout.flush()
            sys.stderr.flush()
Example #7
0
File: port.py Project: csdms/pymt
    def run(self, time):
        """Run the event.

        Call the `run` method of the underlying port using *time* as the run time.

        Parameters
        ----------
        time : float
            Time to run the event to.
        """
        with cd(self._run_dir):
            status = {"name": self.name, "time": time, "status": "running"}
            print(yaml.dump(status), file=self._status_fp)
            self._status_fp.flush()
            sys.stdout.flush()
            sys.stderr.flush()
            print(yaml.dump(status, default_flow_style=True))

            self._port.run(time)

            sys.stdout.flush()
            sys.stderr.flush()
Example #8
0
 def update(self, time):
     with cd(self._run_dir):
         self._port.update_until(time)
Example #9
0
 def finalize(self):
     with cd(self._run_dir):
         self._printer.close()
Example #10
0
 def run(self, time):
     with cd(self._run_dir):
         self._printer.write()
Example #11
0
 def initialize(self, *args):
     with cd(self._run_dir):
         self._printer = PortPrinter.from_dict(self._kwds)
         self._printer.open()
Example #12
0
def main(
    entry_point,
    root_dir,
    bmi_version,
    config_file,
    manifest,
    quiet,
    verbose,
    pytest_args,
    help_pytest,
):
    """Validate a BMI implementation.

    \b
    Examples:

      Test a BMI for the class *Hydrotrend* in module *pymt_hydrotrend*,

        $ bmi-test pymt_hydrotrend:Hydrotrend

    This will test the BMI with a default set of input files as obtained
    through the model metadata associated with the component.

    If the component you would like to test does not have model metadata
    that bmi-tester recognizes, or you would like to test with a non-default
    set of input files, use the *--root-dir* and *--config-file* options.

        $ bmi-tests pymt_hydrotrend:Hydrotrend --root-dir=my_files/ --config-file=config.txt

    where *my_files* is a folder that contains the input files to test with
    and *config.txt* is the configuration file, which will be passed to the
    *initialize* method.
    """
    if root_dir and not config_file:
        err("using --root-dir but no config file specified (use --config-file)")
        raise click.Abort()

    module_name, class_name = entry_point.split(":")

    try:
        Bmi = load_component(entry_point)
    except ImportError:
        err(f"unable to import BMI implementation, {class_name}, from {module_name}")
        raise click.Abort()

    if root_dir:
        stage_dir = root_dir
        if manifest is None:
            manifest = os.listdir(stage_dir)
    else:
        stage_dir = tempfile.mkdtemp()
        try:
            config_file, manifest = _stage_component(entry_point, stage_dir)
        except MetadataNotFoundError:
            config_file, manifest = _stage_component(class_name, stage_dir)

    stages = sorted(
        [pathlib.Path(pkg_resources.resource_filename(__name__, "bootstrap"))]
        + list(
            pathlib.Path(pkg_resources.resource_filename(__name__, "tests")).glob(
                "stage_*"
            )
        )
    )

    if not quiet:
        out("Location of tests:")
        for stage_path in (str(stage_path) for stage_path in stages):
            out(f"- {stage_path}")
        out(f"Entry point: {entry_point}")
        out(repr(Bmi()))
        out(f"BMI version: {bmi_version}")
        out(f"Stage folder: {stage_dir}")
        out(f"> tree -d {stage_dir}")
        if manifest:
            out(_tree(manifest))
        out(f"> cat {stage_dir}/{config_file}")
        with open(os.path.join(stage_dir, config_file), "r") as fp:
            out(fp.read())

    with cd(stage_dir):
        for stage_path in sorted(stages):
            status = check_bmi(
                entry_point,
                tests_dir=str(stage_path),
                # tests_dir=tests_dir,
                input_file=config_file,
                manifest=manifest,
                bmi_version=bmi_version,
                extra_args=pytest_args + ("-vvv",),
                help_pytest=help_pytest,
            )
            if status != ExitCode.OK:
                break

    if not quiet:
        if status == ExitCode.OK:
            out("🎉 All tests passed!")
        else:
            err("😞 There were errors")

    sys.exit(status)
Example #13
0
File: port.py Project: csdms/pymt
 def update(self, time):
     with cd(self._run_dir):
         self._port.update_until(time)