Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix clone with relative restart paths #516

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]:
aidanheerdegen marked this conversation as resolved.
Show resolved Hide resolved
"""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 @@ -244,7 +257,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 @@ -267,6 +280,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 @@ -583,6 +596,34 @@ def test_clone_startpoint(start_point_type):
assert source_repo_commit != cloned_repo.active_branch.object.hexsha


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
Loading