Skip to content

Commit

Permalink
fix(archive): check that basepath, mount in config, exists before con…
Browse files Browse the repository at this point in the history
…tinuing (#39)

* fix(archive): check that basepath, mount in config, exists before continuing

raise file not found error and cancel if it doesn't exist

Signed-off-by: Tarik Zegmott <[email protected]>

* fix(archiver): move checks for basepath down to posix functions

Signed-off-by: Tarik Zegmott <[email protected]>

* test(archive): directories needed to mimic expected format

Signed-off-by: Tarik Zegmott <[email protected]>

---------

Signed-off-by: Tarik Zegmott <[email protected]>
  • Loading branch information
tjzegmott authored Jun 4, 2024
1 parent 15e0e7e commit 87fe370
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
6 changes: 3 additions & 3 deletions tests/test_archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,15 @@ def test_posix_bypass(self):
def test_posix_copy(self, work, directory):
"""Test the copy method."""
file = work.plots[0]
path = directory / "posix" / "method" / "copy"
path = directory / "workflow/20240501" / "posix" / "method" / "copy"
result = posix.copy(path, [file])
assert result is True
assert (path / file).exists()

def test_posix_delete(self, work, directory):
"""Test the delete method."""
file = work.plots[0]
path = directory / "posix" / "method" / "delete"
path = directory / "workflow/20240501" / "posix" / "method" / "delete"
result = posix.delete(path, work.plots)
assert result is True
assert not Path(file).exists()
Expand All @@ -160,7 +160,7 @@ def test_posix_delete(self, work, directory):
def test_posix_move(self, work, directory):
"""Test the move method."""
file = work.plots[0]
path = directory / "posix" / "method" / "move"
path = directory / "workflow/20240501" / "posix" / "method" / "move"
result = posix.move(path, [file])
assert result is True
assert (path / file).exists()
Expand Down
7 changes: 4 additions & 3 deletions workflow/lifecycle/archive/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ def run(work: Work, workspace: Dict[str, Any]) -> None:
}
if work.creation:
date: str = datetime.fromtimestamp(work.creation).strftime("%Y%m%d")
path: Path = Path(
f"{mounts.get(work.site)}/workflow/{date}/{work.pipeline}/{work.id}"
)
else:
raise NameError("Creation date not found in work object.")
basepath: Path = Path(f"{mounts.get(work.site)}")
path: Path = basepath / f"/workflow/{date}/{work.pipeline}/{work.id}"

if (
work.config.archive.products
Expand Down
48 changes: 48 additions & 0 deletions workflow/lifecycle/archive/posix.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""POSIX archive functions."""

import os
import re
import shutil
import subprocess
from pathlib import Path
Expand All @@ -11,6 +12,49 @@
log = logger.get_logger("workflow.lifecycle.archive.posix")


def extract_basepath(path: Path):
"""Extract the base path from a given path.
Uses the fact that the path is build using the following format,
path = basepath / f"/workflow/{date}/{work.pipeline}/{work.id}"
Args:
path: Path to extract from.
Returns:
Path: Base path.
"""
try:
split_path = re.split(r"/workflow/\d+", path.as_posix())
basepath = split_path[0]
return Path(basepath)
except Exception as e:
raise e


def check_basepath(path: Path):
"""Validates if the basepath exists.
If the path given contains '/workflow' it is not the basepath and so the basepath
is extracted before the evaluation.
Args:
path: Path to validate.
Raises:
FileNotFoundError: If the path doesn't exist.
Returns:
bool: True if the path exists.
"""
if "/workflow" in path.as_posix():
path = extract_basepath(path)
if not path.exists():
raise FileNotFoundError(f"Mount {path} does not exist.")
else:
return True


def bypass(path: Path, payload: Optional[List[str]]) -> bool:
"""Bypass the archive.
Expand All @@ -30,6 +74,7 @@ def copy(path: Path, payload: Optional[List[str]]) -> bool:
payload (List[str]): List of files to copy.
"""
try:
check_basepath(path)
path.mkdir(parents=True, exist_ok=True)
if not path.exists() or not path.is_dir() or not os.access(path, os.W_OK):
log.error("Destination path is invalid or not writable.")
Expand Down Expand Up @@ -58,6 +103,7 @@ def move(path: Path, payload: Optional[List[str]]) -> bool:
"""
status: bool = False
try:
check_basepath(path)
path.mkdir(parents=True, exist_ok=True)
if path.exists() and path.is_dir() and os.access(path, os.W_OK) and payload:
for index, item in enumerate(payload):
Expand All @@ -82,6 +128,7 @@ def delete(path: Path, payload: None | List[str]) -> bool:
"""
status: bool = False
try:
check_basepath(path)
if payload:
for item in payload:
os.remove(item)
Expand All @@ -99,6 +146,7 @@ def permissions(path: Path, config: Dict[str, Any]) -> bool:
"""Set the permissions for the work products in the archive."""
status: bool = False
try:
check_basepath(path)
if not path.exists():
raise NotADirectoryError

Expand Down

0 comments on commit 87fe370

Please sign in to comment.