# Copyright (c) 2025 Kim Jarvis TPF Software Services S.A. kim.jarvis@tpfsystems.com # This software is licensed under the MIT License. See the LICENSE file for details.#importasyncsshfromasyncsshimportSFTPAttrsfromreemote.commandimportCommand
[docs]classMakedirs:""" A class to encapsulate the functionality of makedirs (recursive directory creation). Attributes: path (str): The directory path to create recursively. attrs (SFTPAttrs): asyncssh SFTPAttrs object for directory attributes. exist_ok (bool): Whether to raise an error if the target directory already exists. **Examples:** .. code:: python yield Makedirs(path='/home/user/hfs/subdir1/subdir2', attrs=SFTPAttrs(permissions=0o755), exist_ok=True, ) Usage: This class is designed to be used in a generator-based workflow where commands are yielded for execution. Notes: This will create all intermediate directories in the path if they don't exist. """@staticmethodasyncdef_makedirs_callback(host_info,global_info,command,cp,caller):"""Static callback method for directory creation"""# Validate host_inforequired_keys=['host','username','password']forkeyinrequired_keys:ifkeynotinhost_infoorhost_info[key]isNone:raiseValueError(f"Missing or invalid value for '{key}' in host_info.")# Validate caller attributesifcaller.pathisNone:raiseValueError("The 'path' attribute of the caller cannot be None.")try:asyncwithasyncssh.connect(**host_info)asconn:asyncwithconn.start_sftp_client()assftp:# Create the remote directory recursivelyawaitsftp.makedirs(path=caller.path,attrs=caller.attrs,exist_ok=caller.exist_ok)returnf"Successfully created directory {caller.path} on {host_info['host']}"except(OSError,asyncssh.Error)asexc:raise# Re-raise the exception to handle it in the callerdef__init__(self,path:str,attrs:SFTPAttrs=None,exist_ok:bool=False):self.path=path# Set default SFTPAttrs if none providedself.attrs=attrsifattrsisnotNoneelseSFTPAttrs()self.exist_ok=exist_okdef__repr__(self):return(f"Makedirs(path={self.path!r}, "f"attrs={self.attrs!r}, "f"exist_ok={self.exist_ok!r})")defexecute(self):r=yieldCommand(f"{self}",local=True,callback=self._makedirs_callback,caller=self)r.executed=Truer.changed=Falsereturnr