Skip to content

Commit

Permalink
Apply changes from code review by @khsrali
Browse files Browse the repository at this point in the history
  • Loading branch information
GeigerJ2 committed Nov 25, 2024
1 parent b1a0560 commit 6bdee29
Showing 1 changed file with 14 additions and 30 deletions.
44 changes: 14 additions & 30 deletions src/aiida/orm/nodes/data/remote/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,6 @@ def get_authinfo(self):
return AuthInfo.get_collection(self.backend).get(dbcomputer=self.computer, aiidauser=self.user)

def get_size_on_disk(self, relpath: Path | None = None) -> str:
if relpath is None:
relpath = Path('.')
"""
Connects to the remote folder and returns the total size of all files in the directory recursively in a
human-readable format.
Expand All @@ -215,18 +213,22 @@ def get_size_on_disk(self, relpath: Path | None = None) -> str:

from aiida.common.utils import format_directory_size

if relpath is None:
relpath = Path('.')

authinfo = self.get_authinfo()
full_path = Path(self.get_remote_path()) / relpath
computer_label = self.computer.label if self.computer is not None else ''

with authinfo.get_transport() as transport:
if not transport.isdir(str(full_path)) and not transport.isfile(str(full_path)):
if not transport.path_exists(str(full_path)):
exc_message = (
f'The required remote folder {full_path} on Computer <{computer_label}>'
'does not exist, is not a directory or has been deleted.'
)
raise FileNotFoundError(exc_message)


try:
total_size: int = self._get_size_on_disk_du(full_path, transport)

Expand Down Expand Up @@ -267,7 +269,7 @@ def _get_size_on_disk_du(self, full_path: Path, transport: 'Transport') -> int:
else:
raise RuntimeError(f'Error executing `du` command: {stderr}')

def _get_size_on_disk_lstat(self, full_path, transport) -> int:
def _get_size_on_disk_lstat(self, full_path: Path, transport: 'Transport') -> int:
"""
Connects to the remote folder and returns the total size of all files in the directory recursively in bytes
using ``lstat``. Note that even if a file is only 1 byte, on disk, it still occupies one full disk block size.
Expand All @@ -283,37 +285,19 @@ def _get_size_on_disk_lstat(self, full_path, transport) -> int:
:return: Total size of directory recursively in bytes.
:rtype: int
"""

def _get_remote_recursive_size(path: Path, transport: 'Transport') -> int:
"""
Helper function for recursive directory traversal to obtain the `listdir_withattributes` result for all
subdirectories.
:param path: Path to be traversed.
:type path: Path
:param transport: Open transport instance.
:type transport: Transport
:return: Total size of directory files in bytes as obtained via ``lstat``.
:rtype: int
"""

try:
total_size = 0
contents = self.listdir_withattributes(full_path)

contents = self.listdir_withattributes(path)
for item in contents:
item_path = os.path.join(path, item['name'])
if item['isdir']:
# Include size of direcotry
total_size += item['attributes']['st_size']
# Recursively traverse directory
total_size += _get_remote_recursive_size(item_path, transport)
else:
total_size += item['attributes']['st_size']
item_path = full_path / item['name']
# Add size of current item (file or directory metadata)
total_size += item['attributes']['st_size']

return total_size
# If it's a directory, recursively get size of contents
if item['isdir']:
total_size += self._get_size_on_disk_lstat(item_path, transport)

try:
total_size: int = _get_remote_recursive_size(full_path, transport)
return total_size
except OSError:
raise

0 comments on commit 6bdee29

Please sign in to comment.