Skip to content

Commit

Permalink
Fix deletion without confirmation of other sample sheets (#2973)(patch)
Browse files Browse the repository at this point in the history
- Fix deletion of invalid sample sheets
  • Loading branch information
diitaz93 authored Feb 22, 2024
1 parent b3f1b63 commit 2f4a56a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
30 changes: 15 additions & 15 deletions cg/apps/demultiplex/sample_sheet/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from cg.io.controller import WriteFile, WriteStream
from cg.meta.demultiplex.housekeeper_storage_functions import (
add_and_include_sample_sheet_path_to_housekeeper,
delete_file_from_housekeeper,
delete_sample_sheet_from_housekeeper,
)
from cg.models.flow_cell.flow_cell import FlowCellDirectoryData
from cg.utils.files import get_directories_in_path, link_or_overwrite_file
Expand Down Expand Up @@ -90,20 +90,12 @@ def _use_sample_sheet_from_housekeeper(self, flow_cell: FlowCellDirectoryData) -
raise SampleSheetError(
f"Sample sheet for flow cell {flow_cell.id} does not exist in Housekeeper"
)
try:
self.validate_sample_sheet(
sample_sheet_path=sample_sheet_path, bcl_converter=self.bcl_converter
)
LOG.info("Sample sheet from Housekeeper is valid. Copying it to flow cell directory")
if not self.dry_run:
link_or_overwrite_file(src=sample_sheet_path, dst=flow_cell.sample_sheet_path)
except SampleSheetError:
LOG.info(
f"Sample sheet {sample_sheet_path} failed validation, deleting from Housekeeper"
)
if not self.dry_run:
delete_file_from_housekeeper(file_path=sample_sheet_path, hk_api=self.hk_api)
raise SampleSheetError()
self.validate_sample_sheet(
sample_sheet_path=sample_sheet_path, bcl_converter=self.bcl_converter
)
LOG.info("Sample sheet from Housekeeper is valid. Copying it to flow cell directory")
if not self.dry_run:
link_or_overwrite_file(src=sample_sheet_path, dst=flow_cell.sample_sheet_path)

def _use_flow_cell_sample_sheet(self, flow_cell: FlowCellDirectoryData) -> None:
"""Use the sample sheet from the flow cell directory if it is valid."""
Expand All @@ -112,6 +104,10 @@ def _use_flow_cell_sample_sheet(self, flow_cell: FlowCellDirectoryData) -> None:
)
LOG.info("Sample sheet from flow cell directory is valid. Adding it to Housekeeper")
if not self.dry_run:
try:
delete_sample_sheet_from_housekeeper(flow_cell_id=flow_cell.id, hk_api=self.hk_api)
except HousekeeperFileMissingError:
pass
add_and_include_sample_sheet_path_to_housekeeper(
flow_cell_directory=flow_cell.path,
flow_cell_name=flow_cell.id,
Expand Down Expand Up @@ -153,6 +149,10 @@ def _create_sample_sheet_file(self, flow_cell: FlowCellDirectoryData) -> None:
file_format=FileFormat.CSV,
file_path=flow_cell.sample_sheet_path,
)
try:
delete_sample_sheet_from_housekeeper(flow_cell_id=flow_cell.id, hk_api=self.hk_api)
except HousekeeperFileMissingError:
pass
add_and_include_sample_sheet_path_to_housekeeper(
flow_cell_directory=flow_cell.path, flow_cell_name=flow_cell.id, hk_api=self.hk_api
)
Expand Down
7 changes: 4 additions & 3 deletions cg/meta/demultiplex/housekeeper_storage_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,10 @@ def add_and_include_sample_sheet_path_to_housekeeper(
)


def delete_file_from_housekeeper(file_path: Path, hk_api: HousekeeperAPI) -> None:
"""Delete a file from Housekeeper database and disk given its path."""
file: File = hk_api.get_file_insensitive_path(file_path)
def delete_sample_sheet_from_housekeeper(flow_cell_id: str, hk_api: HousekeeperAPI) -> None:
"""Delete a sample sheet from Housekeeper database and disk given its path."""
sample_sheet_file_path: Path = hk_api.get_sample_sheet_path(flow_cell_id)
file: File = hk_api.get_file_insensitive_path(sample_sheet_file_path)
hk_api.delete_file(file_id=file.id)


Expand Down

0 comments on commit 2f4a56a

Please sign in to comment.