Skip to content

Commit

Permalink
Merge pull request #1394 from pyiron/working_directory_items
Browse files Browse the repository at this point in the history
Files: first check working directory then archive
  • Loading branch information
jan-janssen authored Mar 25, 2024
2 parents 7b1b5d0 + acb98c5 commit 3099b16
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
14 changes: 10 additions & 4 deletions pyiron_base/jobs/job/extension/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,19 @@ def tail(self, file: str, lines: int = 100):
)

def __getitem__(self, item):
if item not in _working_directory_list_files(
working_directory=self._working_directory
if item in _working_directory_list_files(
working_directory=self._working_directory,
include_archive=False,
):
return File(os.path.join(self._working_directory, item))
elif item in _working_directory_list_files(
working_directory=self._working_directory,
include_archive=True,
):
return File(os.path.join(self._working_directory, item))
else:
raise FileNotFoundError(item)

return File(os.path.join(self._working_directory, item))

def __getattr__(self, item):
if item.startswith("__") and item.endswith("__"):
raise AttributeError(item)
Expand Down
10 changes: 6 additions & 4 deletions pyiron_base/jobs/job/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,30 +351,32 @@ def _job_is_compressed(job):
return _working_directory_is_compressed(working_directory=job.working_directory)


def _working_directory_list_files(working_directory):
def _working_directory_list_files(working_directory, include_archive=True):
"""
Returns list of files in the jobs working directory.
If the working directory is compressed, return a list of files in the archive.
Args:
working_directory (str): working directory of the job object to inspect files in
include_archive (bool): include files in the .tag.gz archive
Returns:
list of str: file names
"""
if os.path.isdir(working_directory):
uncompressed_files_lst = os.listdir(working_directory)
if _working_directory_is_compressed(working_directory=working_directory):
if include_archive and _working_directory_is_compressed(
working_directory=working_directory
):
compressed_job_name = _get_compressed_job_name(
working_directory=working_directory
)
with tarfile.open(compressed_job_name, "r") as tar:
job_archive_name = os.path.basename(compressed_job_name)
compressed_files_lst = [
member.name for member in tar.getmembers() if member.isfile()
]
uncompressed_files_lst.remove(job_archive_name)
uncompressed_files_lst.remove(os.path.basename(compressed_job_name))
return uncompressed_files_lst + compressed_files_lst
else:
return uncompressed_files_lst
Expand Down

0 comments on commit 3099b16

Please sign in to comment.