Skip to content

Commit

Permalink
copy report file instead of symlink
Browse files Browse the repository at this point in the history
  • Loading branch information
b97pla committed Jan 18, 2024
1 parent 6e932c4 commit d94875c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 17 deletions.
13 changes: 10 additions & 3 deletions delivery/models/runfolder.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ def __init__(
self,
file_path,
base_path=None,
file_checksum=None
file_checksum=None,
file_operation="symlink"
):
"""
A `RunfolderFile` object representing a file in the runfolder
Expand All @@ -54,11 +55,15 @@ def __init__(
:param file_path: the path to the file
:param base_path: a path relative to which the file will be considered
:param file_checksum: a computed checksum for the file
:param file_operation: the file operation to perform, symlink (default) or copy
"""
self.file_path = os.path.abspath(file_path)
self.file_name = os.path.basename(file_path)
self.base_path = base_path or os.path.dirname(self.file_path)
self.checksum = file_checksum
self.file_operation = file_operation \
if file_operation and file_operation in ["symlink", "copy"] \
else "symlink"

@classmethod
def create_object_from_path(
Expand All @@ -68,7 +73,8 @@ def create_object_from_path(
filesystem_service,
metadata_service,
base_path=None,
checksums=None
checksums=None,
file_operation="symlink"
):
checksums = checksums or {}
relative_file_path = filesystem_service.relpath(
Expand All @@ -83,5 +89,6 @@ def create_object_from_path(
return cls(
file_path,
base_path=base_path,
file_checksum=checksum
file_checksum=checksum,
file_operation=file_operation
)
12 changes: 9 additions & 3 deletions delivery/models/sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ def __init__(
read_no=None,
is_index=None,
base_path=None,
checksum=None):
checksum=None,
file_operation="symlink"
):
"""
A `SampleFile` object
Expand All @@ -64,11 +66,13 @@ def __init__(
:param is_index: if True, the sequence file contains index sequences
:param base_path: a path relative to which the file will be considered
:param checksum: the MD5 checksum for this SampleFile
:param file_operation: the file operation to perform, symlink (default) or copy
"""
super(SampleFile, self).__init__(
sample_path,
base_path=base_path,
file_checksum=checksum
file_checksum=checksum,
file_operation=file_operation
)
self.sample_name = sample_name
self.sample_index = sample_index
Expand All @@ -88,4 +92,6 @@ def __hash__(self):
self.lane_no,
self.read_no,
self.is_index,
self.checksum))
self.checksum,
self.file_operation
))
3 changes: 2 additions & 1 deletion delivery/repositories/project_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,8 @@ def get_project_readme(
filesystem_service=self.filesystem_service,
metadata_service=self.metadata_service,
base_path=self.filesystem_service.dirname(readme_file),
checksums=checksums
checksums=checksums,
file_operation="copy"
)
]
except FileNotFoundError:
Expand Down
14 changes: 14 additions & 0 deletions delivery/services/file_system_service.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

import os
import logging
import shutil

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -91,6 +92,19 @@ def symlink(self, source, link_name):
self.makedirs(self.dirname(link_name), exist_ok=True)
return os.symlink(source, link_name)

@staticmethod
def copy(source, dest):
"""
Shadows shutil.copyfile
:param source:
:param dest:
:return: None
"""
try:
return shutil.copyfile(source, dest)
except IsADirectoryError:
return shutil.copytree(source, dest, symlinks=True)

@staticmethod
def mkdir(path):
"""
Expand Down
33 changes: 23 additions & 10 deletions delivery/services/organise_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,11 @@ def organise_project(self, runfolder, project, organised_projects_path, lanes):

def organise_project_file(self, project_file, organised_project_path):
"""
Find and symlink the project report to the organised project directory.
Find and symlink or copy the project-associated files to the organised project directory.
:param project: a Project instance representing the project before organisation
:param organised_project: a Project instance representing the project after organisation
:param project_file: a RunfolderFile instance representing the project-associated file
before organisation
:param organised_project_path: path where the project will be organised
"""

# the relative path from the project file base to the project file (e.g. plots/filename.png)
Expand All @@ -145,11 +146,16 @@ def organise_project_file(self, project_file, organised_project_path):
organised_project_path,
relpath
)
# the relative path from the symlink to the original file
link_path = self.file_system_service.relpath(
project_file.file_path,
self.file_system_service.dirname(link_name))
self.file_system_service.symlink(link_path, link_name)
if project_file.file_operation == "copy":
self.file_system_service.copy(project_file.file_path, link_name)
else:
# the relative path from the symlink to the original file
link_path = self.file_system_service.relpath(
project_file.file_path,
self.file_system_service.dirname(link_name)
)
self.file_system_service.symlink(link_path, link_name)

return RunfolderFile(
link_name,
base_path=organised_project_path,
Expand Down Expand Up @@ -203,8 +209,15 @@ def organise_sample_file(self, sample_file, organised_sample_path, lanes):

# create the symlink in the supplied directory and relative to the file's original location
link_name = os.path.join(organised_sample_path, sample_file.file_name)
relative_path = self.file_system_service.relpath(sample_file.file_path, organised_sample_path)
self.file_system_service.symlink(relative_path, link_name)
if sample_file.file_operation == "copy":
self.file_system_service.copy(sample_file.file_path, link_name)
else:
relative_path = self.file_system_service.relpath(
sample_file.file_path,
organised_sample_path
)
self.file_system_service.symlink(relative_path, link_name)

return SampleFile(
sample_path=link_name,
sample_name=sample_file.sample_name,
Expand Down

0 comments on commit d94875c

Please sign in to comment.