[docs]classLocalFilerStrategy(FilerStrategy):"""Local filer strategy."""def__init__(self,payload:TesInput|TesOutput):"""Initialize the local filer strategy. Args: payload: The payload to instantiate the strategy implementation. """super().__init__(payload)self.input=self.payloadifisinstance(self.payload,TesInput)elseNoneself.output=self.payloadifisinstance(self.payload,TesOutput)elseNone
[docs]asyncdefdownload_input_file(self,container_path:str):"""Download file from storage and mount to PVC."""logger.info(f"Starting local file download to {container_path}")assertself.inputandself.input.urlsource_path=urlparse(self.input.url).pathself._copy_file(source_path,container_path)
[docs]asyncdefdownload_input_directory(self,container_path:str):"""Download input directory from a local path."""logger.info(f"Starting local directory download to {container_path}")assertself.inputandself.input.urlsource_path=urlparse(self.input.url).pathself._copy_directory(source_path,container_path)
[docs]asyncdefupload_output_file(self,container_path:str):"""Dummy upload output (local)."""logger.info(f"Starting local file upload from {container_path}")assertself.outputandself.output.urldestination_path=urlparse(self.output.url).pathself._copy_file(container_path,destination_path)
[docs]asyncdefupload_output_directory(self,container_path:str):"""Upload output directory to a local path."""logger.info(f"Starting local directory upload from {container_path}")assertself.outputandself.output.urldestination_path=urlparse(self.output.url).pathself._copy_directory(container_path,destination_path)
[docs]asyncdefupload_glob(self,glob_files:list[tuple[str,str,bool]]):"""Upload files and directories using wildcard pattern. Args: glob_files: List of tuples containing (file_path, relative_path, is_directory) """assertself.outputisnotNoneforfile_path,relative_path,is_directoryinglob_files:assertself.outputandself.output.urldestination_base=urlparse(self.output.url).pathdestination_path=os.path.join(destination_base,relative_path)ifis_directory:logger.warning(f"Glob pattern matched directory '{file_path}' - uploading as"f"directory (this may not be the intended behavior)")# Ensure the destination directory existsos.makedirs(destination_path,exist_ok=True)# Copy directory contents recursivelyself._copy_directory(file_path,destination_path)else:# Ensure the destination directory existsos.makedirs(os.path.dirname(destination_path),exist_ok=True)logger.info(f"Uploading file {file_path} to {destination_path}")self._copy_file(file_path,destination_path)
def_copy_file(self,src:str,dst:str):"""Copy a file from src to dst with validation."""ifnotos.path.exists(src):logger.error(f"File {src} not found")raiseFileNotFoundError(f"File {src} not found.")ifnotos.path.isfile(src):logger.error(f"Source path {src} is not a file")raiseIsADirectoryError(f"Source path {src} is not a file.")shutil.copy2(src,dst)logger.info(f"Copied file from {src} to {dst}")def_copy_directory(self,src:str,dst:str):"""Copy a directory from src to dst with validation."""ifnotos.path.exists(src):logger.error(f"Directory {src} not found")raiseFileNotFoundError(f"Directory {src} not found.")ifnotos.path.isdir(src):logger.error(f"Source path {src} is not a directory")raiseNotADirectoryError(f"Source path {src} is not a directory.")shutil.copytree(src,dst,dirs_exist_ok=True)logger.info(f"Copied directory from {src} to {dst}")