Skip to content

Commit

Permalink
Fix clone with relative restart paths
Browse files Browse the repository at this point in the history
  • Loading branch information
jo-basevi committed Sep 18, 2024
1 parent 68d8482 commit 6b86c14
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 7 deletions.
31 changes: 24 additions & 7 deletions payu/branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,23 @@
Where BRANCH_NAME is the name of the branch"""


def check_restart(restart_path: Optional[Path],
archive_path: Path) -> Optional[Path]:
"""Checks for valid prior restart path. Returns resolved restart path
if valid, otherwise returns None"""
def check_restart(restart_path: Path,
archive_path: Optional[Path] = None) -> Optional[Path]:
"""Checks if restart path exists and whether the archive already
has pre-existing restarts. Returns a resolved restart path
Parameters
----------
restart_path: Path
Absolute, or relative, restart path to start experiment from
archive_path: Optional[Path], default None
Experiment archive directory to check for pre-existing restarts files
Returns
----------
Optional[Path]
Absolute restart path if a valid path, otherwise None
"""

# Check for valid path
if not restart_path.exists():
Expand All @@ -51,7 +64,7 @@ def check_restart(restart_path: Optional[Path],
restart_path = restart_path.resolve()

# Check for pre-existing restarts in archive
if archive_path.exists():
if archive_path and archive_path.exists():
if len(list_archive_dirs(archive_path, dir_type="restart")) > 0:
warnings.warn((
f"Pre-existing restarts found in archive: {archive_path}."
Expand Down Expand Up @@ -136,7 +149,7 @@ def checkout_branch(branch_name: str,
start_point: Optional[str]
Branch name or commit hash to start new branch from
restart_path: Optional[Path]
Absolute restart path to start experiment from
Restart path to start experiment from
config_path: Optional[Path]
Path to configuration file - config.yaml
control_path: Optional[Path]
Expand Down Expand Up @@ -243,7 +256,7 @@ def clone(repository: str,
lab_path: Optional[Path]
Path to laboratory directory
restart_path: Optional[Path]
Absolute restart path to start experiment from
Restart path to start experiment from
parent_experiment: Optional[str]
Parent experiment UUID to add to generated metadata
Expand All @@ -262,6 +275,10 @@ def clone(repository: str,
# git clone the repository
repo = git_clone(repository, control_path, branch)

if restart_path:
# Check path exists and resolve to an absolute path
restart_path = check_restart(restart_path)

owd = os.getcwd()
try:
# cd into cloned directory
Expand Down
41 changes: 41 additions & 0 deletions test/test_branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,19 @@ def test_add_restart_to_config(test_config, expected_config):
assert updated_config == expected_config


def test_check_restart_relative_path():
"""Test an relative restart path is resolved to an absolute path"""
restart_path = tmpdir / "archive" / "tmpRestart"
restart_path.mkdir(parents=True)

with cd(tmpdir):
relative_restart_path = Path("archive") / "tmpRestart"
assert not relative_restart_path.is_absolute()

resolved_path = check_restart(relative_restart_path)
assert resolved_path.is_absolute()


def test_check_restart_with_non_existent_restart():
"""Test restart path that does not exist raises a warning"""
restart_path = tmpdir / "restartDNE"
Expand Down Expand Up @@ -536,6 +549,34 @@ def test_clone(mock_uuid):
assert [head.name for head in cloned_repo2.heads] == ["Branch1", "Branch2"]


def test_clone_with_relative_restart_path():
"""Test clone with a restart path that is relative with respect to
the directory in which the clone command is run from"""
# Create a repo to clone
source_repo_path = tmpdir / "sourceRepo"
source_repo_path.mkdir()
setup_control_repository(path=source_repo_path)

# Create restart path
restart_path = tmpdir / "archive" / "tmpRestart"
restart_path.mkdir(parents=True)
relative_restart_path = Path("archive") / "tmpRestart"

cloned_repo_path = tmpdir / "clonedRepo"
with cd(tmpdir):
# Run clone
clone(repository=str(source_repo_path),
directory=cloned_repo_path,
lab_path=labdir,
restart_path=relative_restart_path)

# Test restart was added to config.yaml file
with cd(cloned_repo_path):
config = read_config()

assert config["restart"] == str(restart_path)


def add_and_commit_metadata(repo, metadata):
"""Helper function to create/update metadata file and commit"""
metadata_path = ctrldir / "metadata.yaml"
Expand Down

0 comments on commit 6b86c14

Please sign in to comment.