Skip to content

Commit

Permalink
Merge f1021a2 into ca395f6
Browse files Browse the repository at this point in the history
  • Loading branch information
audrium committed Sep 23, 2020
2 parents ca395f6 + f1021a2 commit b29de3c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
21 changes: 17 additions & 4 deletions reana_commons/utils.py
Expand Up @@ -15,6 +15,7 @@
import subprocess
import time
import uuid
import fs
from hashlib import md5

import click
Expand All @@ -27,6 +28,7 @@
REANA_COMPONENT_TYPES,
REANA_CVMFS_PVC_TEMPLATE,
REANA_CVMFS_SC_TEMPLATE,
SHARED_VOLUME_PATH,
)


Expand Down Expand Up @@ -187,8 +189,19 @@ def build_caching_info_message(
return caching_info_message


def get_workspace_disk_usage(workspace, summarize=False, block_size=None):
"""Retrieve disk usage information of a workspace."""
def get_disk_usage(directory, summarize=False, block_size=None):
"""Retrieve directory disk usage information.
:param directory: Disk usage directory.
:param summarize: Displays a total size of a directory.
:param block_size: Scales sizes in a specified format.
:return: List of dicts with file name and size.
"""
reana_fs = fs.open_fs(SHARED_VOLUME_PATH)
if not reana_fs.exists(directory):
raise ValueError("Directory does not exist.")
absolute_path = reana_fs.getospath(directory)
command = ["du"]
if block_size in ["b", "k"]:
command.append("-{}".format(block_size))
Expand All @@ -198,15 +211,15 @@ def get_workspace_disk_usage(workspace, summarize=False, block_size=None):
command.append("-s")
else:
command.append("-a")
command.append(workspace)
command.append(absolute_path)
disk_usage_info = subprocess.check_output(command).decode().split()
# create pairs of (size, filename)
filesize_pairs = list(zip(disk_usage_info[::2], disk_usage_info[1::2]))
filesizes = []
for filesize_pair in filesize_pairs:
size, name = filesize_pair
# trim workspace path in every file name
filesizes.append({"name": name[len(workspace) :], "size": size})
filesizes.append({"name": name[len(absolute_path) :], "size": size})
return filesizes


Expand Down
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -42,6 +42,7 @@
"bravado>=10.2,<10.4",
"checksumdir>=1.1.4,<1.2",
"click>=7.0",
"fs>=2.0",
"jsonschema[format]>=3.0.1",
"kombu>=4.6,<4.7",
"mock>=3.0",
Expand Down

0 comments on commit b29de3c

Please sign in to comment.