diff --git a/dsjobs/__init__.py b/dsjobs/__init__.py index d97d4cc..3ac84b2 100644 --- a/dsjobs/__init__.py +++ b/dsjobs/__init__.py @@ -1,2 +1,2 @@ from .dir import get_ds_path_uri -from .jobs import get_status, runtime_summary, generate_job_info +from .jobs import get_status, runtime_summary, generate_job_info, get_archive_path diff --git a/dsjobs/jobs.py b/dsjobs/jobs.py index 0f690d3..3cfb9d3 100644 --- a/dsjobs/jobs.py +++ b/dsjobs/jobs.py @@ -165,3 +165,34 @@ def generate_job_info( } return job_info + + +def get_archive_path(ag, job_id): + """ + Get the archive path for a given job ID and modifies the user directory + to '/home/jupyter/MyData'. + + Args: + ag (object): The Agave object to interact with the platform. + job_id (str): The job ID to retrieve the archive path for. + + Returns: + str: The modified archive path. + + Raises: + ValueError: If the archivePath format is unexpected. + """ + + # Fetch the job info. + job_info = ag.jobs.get(jobId=job_id) + + # Try to split the archive path to extract the user. + try: + user, _ = job_info.archivePath.split("/", 1) + except ValueError: + raise ValueError(f"Unexpected archivePath format for jobId={job_id}") + + # Construct the new path. + new_path = job_info.archivePath.replace(user, "/home/jupyter/MyData") + + return new_path diff --git a/tests/test_job_get_archive_path.py b/tests/test_job_get_archive_path.py new file mode 100644 index 0000000..f1a5000 --- /dev/null +++ b/tests/test_job_get_archive_path.py @@ -0,0 +1,34 @@ +import unittest +from unittest.mock import patch, Mock +from dsjobs import get_archive_path + + +class TestGetArchivePath(unittest.TestCase): + def test_get_archive_path(self): + # Create a mock Agave object and its method return value + mock_ag = Mock() + mock_job_info = Mock() + mock_job_info.archivePath = "user123/jobdata/somefile" + mock_ag.jobs.get.return_value = mock_job_info + + # Call the function + result = get_archive_path(mock_ag, "dummy_job_id") + + # Check the result + expected_path = "/home/jupyter/MyData/jobdata/somefile" + self.assertEqual(result, expected_path) + + def test_get_archive_path_invalid_format(self): + # Create a mock Agave object with an unexpected format return + mock_ag = Mock() + mock_job_info = Mock() + mock_job_info.archivePath = "invalid_format_path" + mock_ag.jobs.get.return_value = mock_job_info + + # Check if the function raises a ValueError as expected + with self.assertRaises(ValueError): + get_archive_path(mock_ag, "dummy_job_id") + + +if __name__ == "__main__": + unittest.main()