From 2f4a56a14dcdfa49f87ab71f4fdcf21a378b725d Mon Sep 17 00:00:00 2001 From: Sebastian Diaz Date: Thu, 22 Feb 2024 14:47:14 +0100 Subject: [PATCH] Fix deletion without confirmation of other sample sheets (#2973)(patch) - Fix deletion of invalid sample sheets --- cg/apps/demultiplex/sample_sheet/api.py | 30 +++++++++---------- .../housekeeper_storage_functions.py | 7 +++-- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/cg/apps/demultiplex/sample_sheet/api.py b/cg/apps/demultiplex/sample_sheet/api.py index 10e3c36617..f939adee5b 100644 --- a/cg/apps/demultiplex/sample_sheet/api.py +++ b/cg/apps/demultiplex/sample_sheet/api.py @@ -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 @@ -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.""" @@ -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, @@ -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 ) diff --git a/cg/meta/demultiplex/housekeeper_storage_functions.py b/cg/meta/demultiplex/housekeeper_storage_functions.py index c1d1c01ea0..697e363a88 100644 --- a/cg/meta/demultiplex/housekeeper_storage_functions.py +++ b/cg/meta/demultiplex/housekeeper_storage_functions.py @@ -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)