Skip to content

Commit

Permalink
Directory changers skip retrieval of missing files with a user warnin…
Browse files Browse the repository at this point in the history
…g. (#636)

Add check if the file that is being retrieved on a directory changer
exists and skip the copy if it does not exist. This is useful in instances
where optional files are requested when executing a physics code and
not all files are created because the user settings differ than the
default behavior.
  • Loading branch information
jakehader committed Apr 29, 2022
1 parent a857635 commit b47186e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
7 changes: 6 additions & 1 deletion armi/utils/directoryChangers.py
Expand Up @@ -50,7 +50,8 @@ class DirectoryChanger:
filesToMove : list of str, optional
Filenames to bring from the CWD into the destination
filesToRetrieve : list of str, optional
Filenames to bring back from the destination to the cwd
Filenames to bring back from the destination to the cwd. Note that if any of these
files do not exist then the file will be skipped and a warning will be provided.
dumpOnException : bool, optional
Flag to tell system to retrieve the entire directory if an exception
is raised within a the context manager.
Expand Down Expand Up @@ -185,6 +186,10 @@ def _transferFiles(initialPath, destinationPath, fileList):

for fromName, destName in copies:
fromPath = os.path.join(initialPath, fromName)
if not os.path.exists(fromPath):
runLog.warning(f"{fromPath} does not exist and will not be copied.")
continue

toPath = os.path.join(destinationPath, destName)
runLog.extra("Copying {} to {}".format(fromPath, toPath))
shutil.copy(fromPath, toPath)
Expand Down
16 changes: 16 additions & 0 deletions armi/utils/tests/test_directoryChangers.py
Expand Up @@ -142,3 +142,19 @@ def f(name):
self.assertTrue(os.path.exists(f("file2.txt")))
os.remove(f("file1.txt"))
os.remove(f("file2.txt"))

def test_file_retrieval_missing_file(self):
"""Tests that the directory changer still returns a subset of files even if all do not exist."""

def f(name):
"""Utility to avoid test clashes during cleanups"""
return self._testMethodName + name

with directoryChangers.TemporaryDirectoryChanger(
filesToRetrieve=[f("file1.txt"), f("file2.txt")]
):
Path(f("file1.txt")).touch()

self.assertTrue(os.path.exists(f("file1.txt")))
self.assertFalse(os.path.exists(f("file2.txt")))
os.remove(f("file1.txt"))

0 comments on commit b47186e

Please sign in to comment.