# 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.commandimportCommand
[docs]classGet_statvfs:""" A class to encapsulate the functionality of getting filesystem statistics using SFTP statvfs (stat on a filesystem path) in Unix-like operating systems. Attributes: path (str): The filesystem path to get statistics for. **Examples:** .. code:: python yield Get_statvfs(path="/home/user") Usage: This class is designed to be used in a generator-based workflow where commands are yielded for execution. The filesystem statistics for each host will be returned in the operation result. Notes: The operation will execute on all hosts in the current execution context. The path must be a valid filesystem path accessible via SFTP. """def__init__(self,path:str=None):self.path=pathdef__repr__(self):returnf"Get_statvfs(path={self.path!r})"@staticmethoddef_sftp_vfs_attrs_to_dict(attrs):"""Convert SFTPVFSAttrs object to a JSON-serializable dictionary"""ifattrsisNone:returnNoneresult={}# Add all available VFS attributesifhasattr(attrs,'f_bsize')andattrs.f_bsizeisnotNone:result['f_bsize']=attrs.f_bsize# File system block sizeifhasattr(attrs,'f_frsize')andattrs.f_frsizeisnotNone:result['f_frsize']=attrs.f_frsize# Fundamental builtin system block sizeifhasattr(attrs,'f_blocks')andattrs.f_blocksisnotNone:result['f_blocks']=attrs.f_blocks# Total data blocks in builtin systemifhasattr(attrs,'f_bfree')andattrs.f_bfreeisnotNone:result['f_bfree']=attrs.f_bfree# Free blocks in builtin systemifhasattr(attrs,'f_bavail')andattrs.f_bavailisnotNone:result['f_bavail']=attrs.f_bavail# Free blocks available to unprivileged userifhasattr(attrs,'f_files')andattrs.f_filesisnotNone:result['f_files']=attrs.f_files# Total builtin nodes in builtin systemifhasattr(attrs,'f_ffree')andattrs.f_ffreeisnotNone:result['f_ffree']=attrs.f_ffree# Free builtin nodes in builtin systemifhasattr(attrs,'f_favail')andattrs.f_favailisnotNone:result['f_favail']=attrs.f_favail# Free builtin nodes available to unprivileged userifhasattr(attrs,'f_fsid')andattrs.f_fsidisnotNone:result['f_fsid']=attrs.f_fsid# File system IDifhasattr(attrs,'f_flag')andattrs.f_flagisnotNone:result['f_flag']=attrs.f_flag# Mount flagsifhasattr(attrs,'f_namemax')andattrs.f_namemaxisnotNone:result['f_namemax']=attrs.f_namemax# Maximum filename lengthreturnresult@staticmethodasyncdef_getstatvfs_callback(host_info,global_info,command,cp,caller):"""Static callback method for getting filesystem statistics from a path"""asyncwithasyncssh.connect(**host_info)asconn:asyncwithconn.start_sftp_client()assftp:# Get filesystem statistics using statvfs on the pathifcaller.path:# Convert path to bytes as required by asyncssh statvfspath_bytes=caller.path.encode('utf-8')vfs_attrs=awaitsftp.statvfs(path_bytes)# Convert to JSON-serializable dictionaryreturncaller._sftp_vfs_attrs_to_dict(vfs_attrs)else:raiseValueError("Path must be provided for statvfs operation")defexecute(self):r=yieldCommand(f"{self}",local=True,callback=self._getstatvfs_callback,caller=self)r.executed=Truer.changed=Falsereturnr