# 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.#
[docs]classGet_OS:""" A class to obtain information about the OS. Attributes: field (str): The name of the field to operate on. sudo (bool): If `True`, the commands will be executed with `sudo` privileges. su (bool): If `True`, the commands will be executed with `su` privileges. **Examples:** .. code:: python yield Get_OS("NAME") Usage: This class is designed to be used in a generator-based workflow where commands are yielded for execution. It supports adding or removing packages based on the `present` flag and allows privilege escalation via `sudo` or `su`. Notes: - Commands are constructed based on the `present`, `sudo`, and `su` flags. - The `changed` flag is set if the package state changes after execution. """def__init__(self,field:str="PRETTY_NAME",guard:bool=True,sudo:bool=False,su:bool=False):self.field=fieldself.guard:bool=guardself.sudo:bool=sudoself.su:bool=sudefexecute(self):fromreemote.operations.server.shellimportShellimportre# Execute the shell command to read /etc/os-releaser0=yieldShell("cat /etc/os-release")# Define a regex pattern for extracting the specified fieldfield_pattern=rf'{self.field}="([^"]+)"'# Search for the specified field in the outputmatch=re.search(field_pattern,r0.cp.stdout)ifmatch:# Extract and return the value of the specified fieldresult=match.group(1)# Handle special cases for rolling releases (if applicable)ifself.field=="VERSION_ID"andresult.lower()=="rolling":result="Rolling Release"r0.cp.stdout=resultelse:# Handle failure to extract the specified fieldr0.cp.stdout=f"Failed to extract field '{self.field}' from OS details."returnr0.cp.stdoutdef__repr__(self):return(f"Get_OS(field={self.field!r}, "f"guard={self.guard!r}, "f"sudo={self.sudo!r}, "f"su={self.su!r}"f")")