Skip to content

Commit

Permalink
Rudimentary version for symlinks
Browse files Browse the repository at this point in the history
  • Loading branch information
abhaasgoyal committed Feb 28, 2024
1 parent 3783439 commit c32237e
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 5 deletions.
7 changes: 6 additions & 1 deletion benchcab/benchcab.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from benchcab.utils.pbs import render_job_script
from benchcab.utils.repo import create_repo
from benchcab.utils.subprocess import SubprocessWrapper, SubprocessWrapperInterface
from benchcab.workdir import setup_fluxsite_directory_tree
from benchcab.workdir import clean_directory_tree, setup_fluxsite_directory_tree


class Benchcab:
Expand Down Expand Up @@ -308,6 +308,11 @@ def fluxsite(self, config_path: str, no_submit: bool, skip: list[str]):
else:
self.fluxsite_submit_job(config_path, skip)

def clean(self):
"""Endpoint for cleaning runs uirectory ig."""
# TODO: Make benchcab clean, have it in documentation
clean_directory_tree()

Check warning on line 314 in benchcab/benchcab.py

View check run for this annotation

Codecov / codecov/patch

benchcab/benchcab.py#L314

Added line #L314 was not covered by tests

def spatial(self, config_path: str):
"""Endpoint for `benchcab spatial`."""

Expand Down
10 changes: 10 additions & 0 deletions benchcab/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,14 @@ def generate_parser(app: Benchcab) -> argparse.ArgumentParser:
)
parser_spatial.set_defaults(func=app.spatial)

# subcommand: 'benchcab clean'
parser_spatial = subparsers.add_parser(
"clean",
parents=[args_help, args_subcommand],
help="Clean benchcab runs.",
description="""Removes src/ and runs/ directories.""",
add_help=False,
)
parser_spatial.set_defaults(func=app.clean)

return main_parser
11 changes: 9 additions & 2 deletions benchcab/data/config-schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ realisations:
schema:
git:
type: "dict"
excludes: "svn"
excludes: ["svn", "local"]
schema:
branch:
type: "string"
Expand All @@ -37,13 +37,20 @@ realisations:
required: false
svn:
type: "dict"
excludes: "git"
excludes: ["git", "local"]
schema:
branch_path:
type: "string"
required: true
revision:
type: "integer"
local:
type: "dict"
excludes: ["git", "svn"]
schema:
local_path:
type: "string"
required: true
name:
nullable: true
type: "string"
Expand Down
4 changes: 2 additions & 2 deletions benchcab/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from benchcab.environment_modules import EnvironmentModules, EnvironmentModulesInterface
from benchcab.utils import get_logger
from benchcab.utils.fs import chdir, copy2, rename
from benchcab.utils.repo import GitRepo, Repo
from benchcab.utils.repo import GitRepo, LocalRepo, Repo
from benchcab.utils.subprocess import SubprocessWrapper, SubprocessWrapperInterface


Expand Down Expand Up @@ -62,7 +62,7 @@ def __init__(
# TODO(Sean) we should not have to know whether `repo` is a `GitRepo` or
# `SVNRepo`, we should only be working with the `Repo` interface.
# See issue https://github.com/CABLE-LSM/benchcab/issues/210
if isinstance(repo, GitRepo):
if isinstance(repo, (GitRepo, LocalRepo)):
self.src_dir = Path("src")

@property
Expand Down
45 changes: 45 additions & 0 deletions benchcab/utils/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,49 @@ def get_branch_name(self) -> str:
"""


class LocalRepo(Repo):
"""Concrete implementation of the `Repo` class using local path backend."""

def __init__(self, local_path: str, path: str) -> None:
"""Return a LocalRepo instance.
Parameters
----------
path : str
Path of local CABLE branch
"""
self.name = Path(local_path).name
self.local_path = local_path
self.path = path / self.name if path.is_dir() else path
self.logger = get_logger()

Check warning on line 63 in benchcab/utils/repo.py

View check run for this annotation

Codecov / codecov/patch

benchcab/utils/repo.py#L60-L63

Added lines #L60 - L63 were not covered by tests

def checkout(self):
"""Checkout the source code."""
self.path.symlink_to(self.local_path)
self.logger.info(f"Created symlink from to {self.path} named {self.name}")

Check warning on line 68 in benchcab/utils/repo.py

View check run for this annotation

Codecov / codecov/patch

benchcab/utils/repo.py#L67-L68

Added lines #L67 - L68 were not covered by tests

def get_revision(self) -> str:
"""Return the latest revision of the source code.
Returns
-------
str
Human readable string describing the latest revision.
"""
return f"Using local CABLE branch: {self.name}"

Check warning on line 79 in benchcab/utils/repo.py

View check run for this annotation

Codecov / codecov/patch

benchcab/utils/repo.py#L79

Added line #L79 was not covered by tests

def get_branch_name(self) -> str:
"""Return the branch name of the source code.
Returns
-------
str
Branch name of the source code.
"""
return Path(self.path).absolute()

Check warning on line 90 in benchcab/utils/repo.py

View check run for this annotation

Codecov / codecov/patch

benchcab/utils/repo.py#L90

Added line #L90 was not covered by tests

class GitRepo(Repo):
"""A concrete implementation of the `Repo` class using a Git backend.
Expand Down Expand Up @@ -236,4 +279,6 @@ def create_repo(spec: dict, path: Path) -> Repo:
return GitRepo(path=path, **spec["git"])
if "svn" in spec:
return SVNRepo(svn_root=internal.CABLE_SVN_ROOT, path=path, **spec["svn"])
if "local" in spec:
return LocalRepo(path=path, **spec["local"])

Check warning on line 283 in benchcab/utils/repo.py

View check run for this annotation

Codecov / codecov/patch

benchcab/utils/repo.py#L282-L283

Added lines #L282 - L283 were not covered by tests
raise RepoSpecError
3 changes: 3 additions & 0 deletions benchcab/workdir.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
def clean_directory_tree():
"""Remove pre-existing directories in current working directory."""
if internal.SRC_DIR.exists():
for realisation in internal.SRC_DIR.iterdir():
if realisation.is_symlink():
realisation.unlink()

Check warning on line 17 in benchcab/workdir.py

View check run for this annotation

Codecov / codecov/patch

benchcab/workdir.py#L16-L17

Added lines #L16 - L17 were not covered by tests
shutil.rmtree(internal.SRC_DIR)

if internal.RUN_DIR.exists():
Expand Down

0 comments on commit c32237e

Please sign in to comment.