Skip to content

Commit

Permalink
Merge a3ef5a0 into 7aabde7
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego committed Jul 13, 2018
2 parents 7aabde7 + a3ef5a0 commit 600f800
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
21 changes: 17 additions & 4 deletions reana_commons/models.py
Expand Up @@ -34,6 +34,8 @@
from sqlalchemy_utils import JSONType, UUIDType
from sqlalchemy_utils.models import Timestamp

from reana_commons.utils import build_workspace_path

Base = declarative_base()


Expand All @@ -57,6 +59,13 @@ def __repr__(self):
"""User string represetantion."""
return '<User %r>' % self.id_

def get_user_workspace(self):
"""Build user's workspace directory path.
:return: Path to the user's workspace directory.
"""
return build_workspace_path(self.id_)


class UserOrganization(Base):
"""Relationship between Users and Organizations."""
Expand Down Expand Up @@ -90,7 +99,6 @@ class Workflow(Base, Timestamp):
id_ = Column(UUIDType, primary_key=True)
name = Column(String(255))
run_number = Column(Integer)
workspace_path = Column(String(2048))
status = Column(Enum(WorkflowStatus), default=WorkflowStatus.created)
owner_id = Column(UUIDType, ForeignKey('user_.id_'))
specification = Column(JSONType)
Expand All @@ -100,13 +108,11 @@ class Workflow(Base, Timestamp):
__table_args__ = UniqueConstraint('name', 'owner_id', 'run_number',
name='_user_workflow_run_uc'),

def __init__(self, id_, name, workspace_path,
owner_id, specification, parameters, type_,
def __init__(self, id_, name, owner_id, specification, parameters, type_,
logs, status=WorkflowStatus.created):
"""Initialize workflow model."""
self.id_ = id_
self.name = name
self.workspace_path = workspace_path
self.status = status
self.owner_id = owner_id
self.specification = specification
Expand All @@ -127,6 +133,13 @@ def __repr__(self):
"""Workflow string represetantion."""
return '<Workflow %r>' % self.id_

def get_workflow_run_workspace(self):
"""Build workflow run directory path.
:return: Path to the workflow run workspace directory.
"""
return build_workspace_path(self.owner_id, self.id_)

@staticmethod
def update_workflow_status(db_session, workflow_uuid, status,
new_logs, message=None):
Expand Down
26 changes: 16 additions & 10 deletions reana_commons/utils.py
Expand Up @@ -52,16 +52,6 @@ def click_table_printer(headers, _filter, data):
click.echo(formatted_output.format(*row))


def get_user_analyses_dir(org, user):
"""Build the analyses directory path for certain user and organization.
:param org: Organization which user is part of.
:param user: Working directory owner.
:return: Path to the user's analyses directory.
"""
return fs.path.join(org, user, 'analyses')


def calculate_hash_of_dir(directory, file_list=None):
"""Calculate hash of directory."""
SHAhash = hashlib.md5()
Expand Down Expand Up @@ -114,3 +104,19 @@ def calculate_file_access_time(workflow_workspace):
filepath = os.path.join(subdir, file)
access_times[filepath] = os.stat(filepath).st_atime
return access_times


def build_workspace_path(user_id, workflow_id=None):
"""Builds user's workspace relative path.
:param user_id: Owner of the workspace.
:param workflow_id: Optional parameter, if provided gives the path to the
workflow run workspace instead the path just to the user workspace.
:return: String that represents the workspace the OS independent path.
i.e. users/0000/workflow_runs/0034
"""
workspace_path = os.path.join('users', str(user_id), 'workflow_runs')
if workflow_id:
workspace_path = os.path.join(workspace_path, str(workflow_id))

return workspace_path

0 comments on commit 600f800

Please sign in to comment.