Source code for reemote.operations.filesystem.directory
# 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.#importasyncsshfromreemote.commandimportCommandfromreemote.resultimportResult
[docs]classDirectory:""" A class to encapsulate idempotent operations on a directory in Unix-like operating systems. Attributes: path (str): The directory path to create or remove. attrs (str): asyncssh SFTPAttrs object for directory attributes. present (bool): Whether the directory should exist or not. **Examples:** .. code:: python yield Directory( path='/home/user/hfs', present=True, attrs=asyncssh.SFTPAttrs(), ) Usage: This class is designed to be used in a generator-based workflow where commands are yielded for execution. Notes: If hosts is None or empty, the operation will execute on the current host. """def__init__(self,path:str,present:bool,# attrs: asyncssh.SFTPAttrs = None,):self.path=pathself.present=present# self.attrs = attrsdef__repr__(self):return(f"Directory(path={self.path!r}, "# f"attrs={self.attrs!r}, "f"present={self.present!r}, ")defexecute(self):fromreemote.facts.sftp.isdirimportIsdirfromreemote.operations.sftp.mkdirimportMkdirfromreemote.operations.sftp.rmdirimportRmdirr0=Result()r1=Result()r2=Result()r3=Result()r0=yieldCommand(f"{self}",composite=True)r0.executed=Truer0.changed=Falser1=yieldIsdir(path=self.path)r0.executed=Truer0.changed=Falsefound=r1.cp.stdout# Add or remove directory based on the `present` flagr2.changed=Falseifself.presentandnotfound:# r2 = yield Mkdir(path=self.path, attrs=asyncssh.SFTPAttrs())r2=yieldMkdir(path=self.path)r3.executed=Truer2.changed=Truer3.changed=Falseif(notself.present)andfound:r3=yieldRmdir(path=self.path)r3.executed=Truer3.changed=True# Set the `changed` flag iff the state has changedifr2.changedorr3.changed:r0.executed=Truer0.changed=True