HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.104.1.lve.el8.x86_64 #1 SMP Tue Feb 10 20:07:30 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.29
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/utils/__pycache__/mac_utils.cpython-310.pyc
o

�N�g�7�@sXdZddlZddlZddlZddlZddlZddlZddlZ	ddl
Z	ddlZ	ddlZ	ddl
Z	ddlZ	ddlZ	ddlZ	ddlmZmZmZzddlZWn	eySYnwe	jj��dZe�e�ZdZe	jj j!e	jj j"d�Z#dd�Z$d	d
�Z%dd�Z&d
d�Z'dd�Z(dd�Z)dd�Z*d%dd�Z+dd�Z,dd�Z-d&dd�Z.d&dd �Z/d&d!d"�Z0d#d$�Z1dS)'zD
Helper functions for use by mac modules
.. versionadded:: 2016.3.0
�N)�CommandExecutionError�SaltInvocationError�TimedProcTimeoutError�shellZ	mac_utils)�cmd.run_allzcmd.runcCstjj��sdStS)z
    Load only on Mac OS
    )FzOThe mac_utils utility could not be loaded: utility only works on MacOS systems.)�salt�utils�platformZ	is_darwin�__virtualname__�rr�H/opt/saltstack/salt/lib/python3.10/site-packages/salt/utils/mac_utils.py�__virtual__/sr
c

Cs�t|t�stjjj|dd�}t|�D]\}}t||t�s%t||�||<qd�|�}t	j
��}dt|dt
jt
jdddd�	}z
tjjj|fi|��}Wnty`}z	td�|||���d}~wwi}z|��Wn(ty�}zt|�|d<d	|d
<d|d<|jj|d
<|WYd}~Sd}~ww|j|j}}	|dur�tjj�|���}|	dur�tjj�|	���}	|jj|d
<|jj|d<||d<|	|d
<|S)z,

    Args:
        cmd:

    Returns:

    F)�posix� NT)	�cwdr�env�stdin�stdout�stderrZwith_communicate�timeout�bgz<Unable to run command '{}' with the context '{}', reason: {}r�r��retcode�pid)�
isinstance�listrr�argsZshlex_split�	enumerate�str�join�os�environ�copy�
DEFAULT_SHELL�
subprocess�PIPEZtimed_subprocessZ	TimedProc�OSErrorr�format�runr�processrrrZstringutilsZto_str�rstrip�
returncode)
�cmd�idx�itemZrun_env�kwargs�proc�exc�ret�out�errrrr�_run_all=sb
	�

�������r6cCs|d��}d|vrdSdS)z�
    helper class to check the launchctl stderr.
    launchctl does not always return bad exit code
    if there is a failure
    rzservice is disabledTF)�lower)r3r5rrr�_check_launchctl_stderr�sr8cCs�t|�}t�d||�|ddksd|d��vr>d|�d�}|d�|d�7}|d	�|d�7}|d
�|d�7}t|��dS)
z�
    Executes the passed command. Returns True if successful

    :param str cmd: The command to run

    :return: True if successful, otherwise False
    :rtype: bool

    :raises: Error if command fails or is not supported
    zExecute return success %s: %rrr�
not supportedr�Command Failed: �
�Return Code: {}
�Output: {}
�
Error: {}
rT)r6�log�debugr7r(r�r-r3�msgrrr�execute_return_success�srCcCsvt|�}|ddksd|d��vr7d|�d�}|d�|d�7}|d�|d�7}|d	�|d
�7}t|��|dS)a
    Executes the passed command. Returns the standard out if successful

    :param str cmd: The command to run

    :return: The standard out of the command if successful, otherwise returns
    an error
    :rtype: str

    :raises: Error if command fails or is not supported
    rrr9rr:r;r<r=r>r)r6r7r(rrArrr�execute_return_result�srDcCs0d|vr|�d�dSd|vr|�d�dS|S)a
    Returns the data portion of a string that is colon separated.

    :param str data: The string that contains the data to be parsed. Usually the
    standard out from a command

    For example:
    ``Time Zone: America/Denver``
    will return:
    ``America/Denver``
    z: rz:
)�split)�datarrr�parse_return�s

rGcCsLt|t�r|��dvrd�|�}t|��|��dvrdSdSt|�r$dSdS)a�
    Helper function to validate the enabled parameter. Boolean values are
    converted to "on" and "off". String values are checked to make sure they are
    either "on" or "off"/"yes" or "no". Integer ``0`` will return "off". All
    other integers will return "on"

    :param enabled: Enabled can be boolean True or False, Integers, or string
    values "on" and "off"/"yes" and "no".
    :type: str, int, bool

    :return: "on" or "off" or errors
    :rtype: str
    )�on�off�yes�nozk
Mac Power: Invalid String Value for Enabled.
String values must be 'on' or 'off'/'yes' or 'no'.
Passed: {})rHrJrHrI)rrr7r(r�bool)�enabledrBrrr�validate_enabled�s
�rNF�cCsPt|�D]!}|r
t|��n|�}t�d||||�||vr dSt�d�qdS)a�
    Wait up to ``wait`` seconds for a system parameter to be changed before
    deciding it hasn't changed.

    :param str value: The value indicating a successful change

    :param function check_fun: The function whose return is compared with
        ``value``

    :param bool normalize_ret: Whether to normalize the return from
        ``check_fun`` with ``validate_enabled``

    :param int wait: The maximum amount of seconds to wait for a system
        parameter to change
    z0Confirm update try: %d func:%r state:%s value:%sTrF)�rangerNr?r@�time�sleep)�valueZ	check_funZ
normalize_ret�wait�i�staterrr�confirm_updated�s�rWcOs�|�dd�}d|g}|�|�|dkrdg|d<d|d<tjjjdi|��}td|fi|��}t|�}|d	s:|r_d
|�d�}|d�|d
�7}|d�|d�7}|d�|d	�7}t	|��|re|d
SdS)a�
    Run a launchctl command and raise an error if it fails

    Args: additional args are passed to launchctl
        sub_cmd (str): Sub command supplied to launchctl

    Kwargs: passed to ``cmd.run_all``
        return_stdout (bool): A keyword argument. If true return the stdout of
            the launchctl command

    Returns:
        bool: ``True`` if successful
        str: The stdout of the launchctl command if requested

    Raises:
        CommandExecutionError: If command fails

    CLI Example:

    .. code-block:: bash

        import salt.utils.mac_service
        salt.utils.mac_service.launchctl('debug', 'org.cups.cupsd')
    �
return_stdoutF�	launchctlZbootout�$Zsuccess_retcodesZpython_shellrrz
Failed to z
 service:
zstdout: {}
rzstderr: {}
rzretcode: {}TNr)
�pop�extendrrrZclean_kwargs�__salt__r8r(r)Zsub_cmdrr0rXr-r3�errorr4rrrrYs"
�rYc
Cs>tj�||�}t�d|�|���d�st�d|�iStj�tj�|��s.t�	d|�iSzt
jj�
|d��
}t�|�}Wd�n1sGwYWn?tjy_t�	d|�iYStyy}zt�d||�iWYd}~Sd}~wtjjjy�t�	d	|�iYSwd
|vr�t�d|�iS|||d�S)
z�
    :param root: The root path of the plist file
    :param file_name: The name of the plist file
    :return:  An empty dictionary if the plist file was invalid, otherwise, a dictionary with plist data
    z)read_plist: Gathering service info for %sz.plistz read_plist: Not a plist file: %sz'read_plist: Ignoring broken symlink: %s�rbNzLread_plist: Unable to parse "%s" as it is invalid XML: InvalidFileException.z4Caught ValueError: '%s', while trying to parse '%s'.zTread_plist: Unable to parse "%s" as it is invalid XML: xml.parsers.expat.ExpatError.�Labelz>read_plist: Service does not contain a Label key. Skipping %s.)�	file_name�	file_path�plist)r!�pathr r?r@r7�endswith�exists�realpath�warningrr�filesZfopen�plistlib�loadZInvalidFileException�
ValueError�xml�parsersZexpatZ
ExpatError)�rootrarb�handlercr5rrr�_read_plist_fileEsR���������rqc	s�dtvr|st�d�dtd<tdShd�}d�|��fdd�t�d	�D��i}|D]%}tjj�	|�D]\}}}|D]}t
||�}|rO|||d
d��<q<q5q,|td<dtd<|S)
a�
    This is a helper function for getting the available macOS services.

    The strategy is to look through the known system locations for
    launchd plist files, parse them, and use their information for
    populating the list of services. Services can run without a plist
    file present, but normally services which have an automated startup
    will have a plist file, so this is a minor compromise.
    �available_servicesz%Found context for available services.TZusing_cached_services>z/Library/LaunchDaemonsz/Library/LaunchAgentsz/System/Library/LaunchDaemonsz/System/Library/LaunchAgentsz/Users/{}/Library/LaunchAgentscs(h|]}tj���|��r��|��qSr)r!rd�isdirr()�.0�user�Z
agent_pathrr�	<setcomp>�s��z&_available_services.<locals>.<setcomp>z/Users/rcr`F)�__context__r?r@�updater!�listdirrrrdZos_walkrqr7)	�refreshZ
launchd_paths�resultZ
launch_dirro�dirsrirarFrrvr�_available_services�s0


��
���r~cCst�d�t|�S)at
    Return a dictionary of all available services on the system

    :param bool refresh: If you wish to refresh the available services
    as this data is cached on the first run.

    Returns:
        dict: All available services

    CLI Example:

    .. code-block:: bash

        import salt.utils.mac_service
        salt.utils.mac_service.available_services()
    zLoading available services)r?r@r~)r{rrrrr�s
rrc	CsDz	t�d�d}Wn
ttfytd��w|r t�|�dS|S)a�
    Gets the UID or Username of the current console user.

    :return: The uid or username of the console user.

    :param bool username: Whether to return the username of the console
    user instead of the UID. Defaults to False

    :rtype: Interger of the UID, or a string of the username.

    Raises:
        CommandExecutionError: If we fail to get the UID.

    CLI Example:

    .. code-block:: bash

        import salt.utils.mac_service
        salt.utils.mac_service.console_user()
    z/dev/console�z)Failed to get a UID for the console user.r)r!�statr'�
IndexErrorr�pwd�getpwuid)�username�uidrrr�console_user�s�r�cCsPzddg}tj|tjtjdd�}t�d�WdStjy't�d�YdSw)	zL
    Return whether macOS git is the standard OS stub or a real binary.
    z/usr/bin/xcode-selectz-pr)rrrz Xcode command line tools presentFz$Xcode command line tools not presentT)r%�
check_callr&r?r@�CalledProcessError)r-�_rrr�git_is_stub�s
�

�r�)FrO)F)2�__doc__�loggingr!rjr%rQZxml.parsers.expatrmZsalt.grains.extrarZsalt.modules.cmdmodZsalt.utils.argsZsalt.utils.filesZsalt.utils.pathZsalt.utils.platformZsalt.utils.stringutilsZsalt.utils.timed_subprocessZsalt.exceptionsrrrr��ImportErrorZgrains�extrarr$�	getLogger�__name__r?r
�modulesZcmdmodZ_run_all_quietZ
_run_quietr]r
r6r8rCrDrGrNrWrYrqr~rrr�r�rrrr�<module>sR�
�D
7
>
0
"