Skip to content

Commit

Permalink
Black reapplied. #103
Browse files Browse the repository at this point in the history
  • Loading branch information
bschroeter committed Feb 13, 2024
1 parent 222d16d commit 9b49c13
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 17 deletions.
14 changes: 7 additions & 7 deletions benchcab/benchcab.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from benchcab.utils import get_logger
from benchcab.utils.fs import mkdir, next_path
from benchcab.utils.pbs import render_job_script
from benchcab.utils.repo import SVNRepo, create_repo
from benchcab.utils.repo import create_repo
from benchcab.utils.subprocess import SubprocessWrapper, SubprocessWrapperInterface
from benchcab.workdir import setup_fluxsite_directory_tree

Expand All @@ -50,6 +50,7 @@ def __init__(
Path to the executable.
validate_env : bool, optional
Validate the environment, by default True
"""
self.benchcab_exe_path = benchcab_exe_path
self.validate_env = validate_env
Expand Down Expand Up @@ -106,11 +107,10 @@ def _validate_environment(self, project: str, modules: list):
paths = list(internal.MET_DIR.glob(f"{site_id}*"))
if not paths:
self.logger.error(

Check warning on line 109 in benchcab/benchcab.py

View check run for this annotation

Codecov / codecov/patch

benchcab/benchcab.py#L109

Added line #L109 was not covered by tests
[
f"Failed to infer met file for site id '{site_id}' in "
f"{internal.MET_DIR}."
]
f"Failed to infer met file for site id '{site_id}' in "
)
self.logger.error(f"{internal.MET_DIR}.")

Check warning on line 112 in benchcab/benchcab.py

View check run for this annotation

Codecov / codecov/patch

benchcab/benchcab.py#L112

Added line #L112 was not covered by tests

sys.exit(1)
if len(paths) > 1:
self.logger.error(

Check warning on line 116 in benchcab/benchcab.py

View check run for this annotation

Codecov / codecov/patch

benchcab/benchcab.py#L116

Added line #L116 was not covered by tests
Expand Down Expand Up @@ -262,7 +262,7 @@ def fluxsite_run_tasks(self, config_path: str):
ncpus = config.get("pbs", {}).get(

Check warning on line 262 in benchcab/benchcab.py

View check run for this annotation

Codecov / codecov/patch

benchcab/benchcab.py#L256-L262

Added lines #L256 - L262 were not covered by tests
"ncpus", internal.FLUXSITE_DEFAULT_PBS["ncpus"]
)
run_tasks_in_parallel(tasks, n_processes=ncpus, verbose=verbose)
run_tasks_in_parallel(tasks, n_processes=ncpus)

Check warning on line 265 in benchcab/benchcab.py

View check run for this annotation

Codecov / codecov/patch

benchcab/benchcab.py#L265

Added line #L265 was not covered by tests
else:
run_tasks(tasks)
self.logger.info("Successfully ran fluxsite tasks")

Check warning on line 268 in benchcab/benchcab.py

View check run for this annotation

Codecov / codecov/patch

benchcab/benchcab.py#L267-L268

Added lines #L267 - L268 were not covered by tests
Expand Down Expand Up @@ -290,7 +290,7 @@ def fluxsite_bitwise_cmp(self, config_path: str):
ncpus = config["fluxsite"]["pbs"]["ncpus"]
except KeyError:
ncpus = internal.FLUXSITE_DEFAULT_PBS["ncpus"]
run_comparisons_in_parallel(comparisons, n_processes=ncpus, verbose=verbose)
run_comparisons_in_parallel(comparisons, n_processes=ncpus)

Check warning on line 293 in benchcab/benchcab.py

View check run for this annotation

Codecov / codecov/patch

benchcab/benchcab.py#L283-L293

Added lines #L283 - L293 were not covered by tests
else:
run_comparisons(comparisons)
self.logger.info("Successfully ran comparison tasks")

Check warning on line 296 in benchcab/benchcab.py

View check run for this annotation

Codecov / codecov/patch

benchcab/benchcab.py#L295-L296

Added lines #L295 - L296 were not covered by tests
Expand Down
9 changes: 3 additions & 6 deletions benchcab/comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def __init__(
Files.
task_name : str
Name of the task.
"""
self.files = files
self.task_name = task_name
Expand All @@ -57,12 +58,8 @@ def run(self) -> None:
with output_file.open("w", encoding="utf-8") as file:
file.write(exc.stdout)

self.logger.error(
[
f"Failure: files {file_a.name} {file_b.name} differ. ",
f"Results of diff have been written to {output_file}",
]
)
self.logger.error(f"Failure: files {file_a.name} {file_b.name} differ. ")
self.logger.error(f"Results of diff have been written to {output_file}")

sys.stdout.flush()

Expand Down
5 changes: 5 additions & 0 deletions benchcab/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def __init__(self, validator: Validator):
----------
validator: cerberus.Validator
A validation object that has been used and has the errors attribute.
"""
# Nicely format the errors.
errors = [f"{k} = {v}" for k, v in validator.errors.items()]
Expand Down Expand Up @@ -51,6 +52,7 @@ def validate_config(config: dict) -> bool:
------
ConfigValidationError
Raised when the configuration file fails validation.
"""
# Load the schema
schema = bu.load_package_data("config-schema.yml")
Expand Down Expand Up @@ -81,6 +83,7 @@ def read_optional_key(config: dict):
----------
config : dict
The configuration file with with/without optional keys
"""
if "project" not in config:
config["project"] = os.environ.get("PROJECT", None)
Expand Down Expand Up @@ -118,6 +121,7 @@ def read_config_file(config_path: str) -> dict:
-------
dict
Configuration dict
"""
# Load the configuration file.
with Path.open(Path(config_path), "r", encoding="utf-8") as file:
Expand All @@ -143,6 +147,7 @@ def read_config(config_path: str) -> dict:
------
ConfigValidationError
Raised when the configuration file fails validation.
"""
# Read configuration file
config = read_config_file(config_path)
Expand Down
4 changes: 4 additions & 0 deletions benchcab/environment_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def module_is_avail(self, *args: str) -> bool:
-------
bool
True if available, False otherwise.
"""
return module("is-avail", *args)

Expand All @@ -82,6 +83,7 @@ def module_is_loaded(self, *args: str) -> bool:
-------
bool
True if loaded, False otherwise.
"""
return module("is-loaded", *args)

Expand All @@ -92,6 +94,7 @@ def module_load(self, *args: str) -> None:
------
EnvironmentModulesError
Raised when module fails to load.
"""
if not module("load", *args):
raise EnvironmentModulesError("Failed to load modules: " + " ".join(args))
Expand All @@ -103,6 +106,7 @@ def module_unload(self, *args: str) -> None:
------
EnvironmentModulesError
Raised when module fails to unload.
"""
if not module("unload", *args):
raise EnvironmentModulesError("Failed to unload modules: " + " ".join(args))
4 changes: 3 additions & 1 deletion benchcab/fluxsite.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
from benchcab import __version__, internal
from benchcab.comparison import ComparisonTask
from benchcab.model import Model
from benchcab.utils import get_logger
from benchcab.utils.fs import chdir, mkdir
from benchcab.utils.subprocess import SubprocessWrapper, SubprocessWrapperInterface
from benchcab.utils import get_logger

# fmt: off
# ======================================================
Expand All @@ -44,6 +44,7 @@ def deep_update(mapping: Dict[KeyType, Any], *updating_mappings: Dict[KeyType, A
-------
Dict[KeyType, Any]
Updated mapping.
"""
updated_mapping = mapping.copy()
for updating_mapping in updating_mappings:
Expand Down Expand Up @@ -129,6 +130,7 @@ def __init__(
Science configuration ID.
sci_config : dict
Science configuration.
"""
self.model = model
self.met_forcing_file = met_forcing_file
Expand Down
1 change: 1 addition & 0 deletions benchcab/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def parse_and_dispatch(parser):
----
parser : argparse.ArgumentParser
Parser object.
"""
args = vars(parser.parse_args(sys.argv[1:] if sys.argv[1:] else ["-h"]))

Expand Down
1 change: 1 addition & 0 deletions benchcab/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def __init__(
Build script, by default None
model_id : Optional[int], optional
Model ID, by default None
"""
self.repo = repo
self.name = name if name else repo.get_branch_name()
Expand Down
6 changes: 4 additions & 2 deletions benchcab/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

"""Top-level utilities."""
import json
import logging
import os
import pkgutil
import sys
from importlib import resources
from pathlib import Path

import yaml
import logging
import sys

# List of one-argument decoding functions.
PACKAGE_DATA_DECODERS = dict(json=json.loads, yml=yaml.safe_load)
Expand All @@ -25,6 +25,7 @@ def get_installed_root() -> Path:
-------
Path
Path to the installed root.
"""
return Path(resources.files("benchcab"))

Expand All @@ -36,6 +37,7 @@ def load_package_data(filename: str) -> dict:
----------
filename : str
Filename of the file to load out of the data directory.
"""
# Work out the encoding of requested file.
ext = filename.split(".")[-1]
Expand Down
1 change: 1 addition & 0 deletions benchcab/utils/fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def mkdir(new_path: Path, **kwargs):
Path to the directory to be created.
**kwargs : dict, optional
Additional options for `pathlib.Path.mkdir()`
"""
get_logger().debug(f"Creating {new_path} directory")
new_path.mkdir(**kwargs)
11 changes: 11 additions & 0 deletions benchcab/utils/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def get_revision(self) -> str:
-------
str
Human readable string describing the latest revision.
"""

@abstractmethod
Expand All @@ -41,6 +42,7 @@ def get_branch_name(self) -> str:
-------
str
Branch name of the source code.
"""


Expand All @@ -51,6 +53,7 @@ class GitRepo(Repo):
----------
subprocess_handler: SubprocessWrapper
Object for handling subprocess calls.
"""

subprocess_handler = SubprocessWrapper()
Expand All @@ -73,6 +76,7 @@ def __init__(
commit: str, optional
Commit hash (long). When specified the repository will reset to this
commit when cloning.
"""
self.url = url
self.branch = branch
Expand Down Expand Up @@ -103,6 +107,7 @@ def get_revision(self) -> str:
-------
str
Human readable string describing the latest revision.
"""
repo = git.Repo(self.path)
return f"commit {repo.head.commit.hexsha}"
Expand All @@ -114,6 +119,7 @@ def get_branch_name(self) -> str:
-------
str
Branch name of the source code.
"""
return self.branch

Expand All @@ -125,6 +131,7 @@ class SVNRepo(Repo):
----------
subprocess_handler: SubprocessWrapper
Object for handling subprocess calls.
"""

subprocess_handler: SubprocessWrapperInterface = SubprocessWrapper()
Expand Down Expand Up @@ -152,6 +159,7 @@ def __init__(
revision: int, optional
SVN revision number. When specified the branch will be set to this
revision on checkout.
"""
self.svn_root = svn_root
self.branch_path = branch_path
Expand Down Expand Up @@ -181,6 +189,7 @@ def get_revision(self) -> str:
-------
str
Human readable string describing the latest revision.
"""
proc = self.subprocess_handler.run_cmd(
f"svn info --show-item last-changed-revision {self.path}",
Expand All @@ -195,6 +204,7 @@ def get_branch_name(self) -> str:
-------
str
Branch name of the source code.
"""
return Path(self.branch_path).name

Expand All @@ -218,6 +228,7 @@ def create_repo(spec: dict, path: Path) -> Repo:
-------
Repo
A subclass instance of `Repo`.
"""
if "git" in spec:
if "url" not in spec["git"]:
Expand Down
1 change: 1 addition & 0 deletions benchcab/utils/subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def run_cmd(
-------
subprocess.CompletedProcess
_description_
"""
# Use the logging level (10 = Debug) to determine verbosity.
verbose = get_logger().getEffectiveLevel() == DEBUG_LEVEL
Expand Down
1 change: 1 addition & 0 deletions tests/test_benchcab.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""`pytest` tests for `benchcab.py`."""

import re
from contextlib import nullcontext as does_not_raise
from unittest import mock
Expand Down
2 changes: 1 addition & 1 deletion tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""`pytest` tests for config.py."""

import os
import re
from contextlib import nullcontext as does_not_raise
from pathlib import Path
from pprint import pformat
Expand Down

0 comments on commit 9b49c13

Please sign in to comment.