HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.109.1.lve.el8.x86_64 #1 SMP Thu Mar 5 20:23:46 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.30
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/utils/__pycache__/extend.cpython-310.pyc
o

�N�g5#�
@sdZddlZddlZddlZddlZddlZddlmZddlm	Z	ddl
ZddlZddl
mZddlmZe�e�ZzddlZdZWneyWZzdZWYdZ[ndZ[wwd	Zd
d�Zdd
�Zdd�Zdd�Zdd�Zdd�Zdd�Z						ddd�Z edkr�e �dSdS)a*
SaltStack Extend
~~~~~~~~~~~~~~~~

A templating tool for extending SaltStack.

Takes a template directory and merges it into a SaltStack source code
directory. This tool uses Jinja2 for templating.

This tool is accessed using `salt-extend`

    :codeauthor: Anthony Shaw <anthonyshaw@apache.org>
�N)�date)�Template)�deserialize)�OrderedDictTFztemplate.ymlcCsPtjj�|d��}t|�}||�dd�|f}Wd�|S1s!wY|S)a-
    Get the contents of a template file and provide it as a module type

    :param path: path to the template.yml file
    :type  path: ``str``

    :param option_key: The unique key of this template
    :type  option_key: ``str``

    :returns: Details about the template
    :rtype: ``tuple``
    �r�description�N)�salt�utils�files�fopenr�get)�pathZ
option_keyZ
template_f�template�info�r�E/opt/saltstack/salt/lib/python3.10/site-packages/salt/utils/extend.py�
_get_template)s

��rcCsxg}t�d|�t�|�D],}tj�||�}tj�|�r9tj�|t�}tj�|�r2|�	t
||��q
t�d|t�q
|S)z�
    Fetch all of the templates in the src directory

    :param src: The source path
    :type  src: ``str``

    :rtype: ``list`` of ``tuple``
    :returns: ``list`` of ('key', 'description')
    zListing contents of %sz Directory does not contain %s %s)�log�debug�os�listdirr�join�isdir�TEMPLATE_FILE_NAME�isfile�appendr)�src�	templates�item�sZ
template_pathrrr�_fetch_templates<s
��r!cCs�t�|�D]=}tj�||�}tj�||�}tj�|�r5t�d||�tj�|�r.t||�qt	�
||�qt�d||�t	�||�qdS)z�
    Akin to shutils.copytree but over existing directories, does a recursive merge copy.

    :param src: The source path
    :type  src: ``str``

    :param dst: The destination path
    :type  dst: ``str``
    �Copying folder %s to %s�Copying file %s to %sN)rrrrrrr�exists�
_mergetree�shutil�copytree�copy2)r�dstrr �drrrr%Ws
�r%c
	Cs4t�|�D]�}tj�||�}tj�||�}tj�|�r;t�d||�tj�|�r/t|||�qt�	|�t|||�q|t
kr�t|��|�}t�d||�t
jj�|d��}t
jj�|���}t|��|�}Wd�n1spwYt
jj�|d��}	|	�t
jj�|��Wd�n1s�wYqdS)ak
    Merge directory A to directory B, apply Jinja2 templating to both
    the file/folder names AND to the contents of the files

    :param src: The source path
    :type  src: ``str``

    :param dst: The destination path
    :type  dst: ``str``

    :param context: The dictionary to inject into the Jinja template as context
    :type  context: ``dict``
    r"r#rN�w)rrrrrrrr$�_mergetreejinja�mkdirrr�renderr	r
rrZstringutilsZ
to_unicode�read�writeZto_str)
rr)�contextrr r*Zsource_fileZsrc_contentsZ
dest_contentsZ	dest_filerrrr,os*
����r,cCstj||d�S)a
    Prompt the user to enter the value of a variable

    :param var_name: The question to ask the user
    :type  var_name: ``str``

    :param default_value: The default value
    :type  default_value: ``str``

    :rtype: ``str``
    :returns: the value from the user
    )�default)�click�prompt)�var_nameZ
default_valuerrr�_prompt_user_variable�s
r6c	Cs~tdd�t|d�D��}|��}d}dd�|��D�}d�d|�d	�d�|�d
�d�|��f�}tj|t�|�|d�}||S)
a�
    Prompt the user to choose between a list of options, index each one by adding an enumerator
    based on https://github.com/audreyr/cookiecutter/blob/master/cookiecutter/prompt.py#L51

    :param var_name: The question to ask the user
    :type  var_name: ``str``

    :param options: A list of options
    :type  options: ``list`` of ``tupple``

    :rtype: ``tuple``
    :returns: The selected user
    css*�|]\}}|ddkr|�|fVqdS)r�testNr)�.0�i�valuerrr�	<genexpr>�s� �z!_prompt_choice.<locals>.<genexpr>��1cSs4g|]}|d�d|dd�d|dd���qS)rz - r<r)r8�crrr�
<listcomp>�s4z"_prompt_choice.<locals>.<listcomp>�
zSelect �:zChoose from {}z, )�typer2)	r�	enumerate�keys�itemsr�formatr3r4ZChoice)r5�optionsZ
choice_map�choicesr2Zchoice_linesr4Zuser_choicerrr�_prompt_choice�s�
��rIcCst|||�dS)aP
    Apply the template from the template directory to the output
    using the supplied context dict.

    :param src: The source path
    :type  src: ``str``

    :param dst: The destination path
    :type  dst: ``str``

    :param context: The dictionary to inject into the Jinja template as context
    :type  context: ``dict``
    N)r,)�template_dirZ
output_dirr1rrr�apply_template�srKcs�tstd�t�d�|durd}ttj�|d��}�dur*td�d}t||�}n�t	t
|��dvr=td	�t�d��fd
d�|D�d}|d}|d}	|dur]td
�tdd�}|durftdd�}d|��}
|}tj
j��j||t���d�t���d�d�}i}
|	�di���D]\}}t|�dd���|�}t|d|�}||
|<q�|��}|�|	�|�|
�|dur�t��}t|
||�|s�|}nt||�|}t�d|�|S)a�
    A template factory for extending the salt ecosystem

    :param extension: The extension type, e.g. 'module', 'state', if omitted, user will be prompted
    :type  extension: ``str``

    :param name: Python-friendly name for the module, if omitted, user will be prompted
    :type  name: ``str``

    :param description: A description of the extension, if omitted, user will be prompted
    :type  description: ``str``

    :param salt_dir: The targeted Salt source directory
    :type  salt_dir: ``str``

    :param merge: Merge with salt directory, `False` to keep separate, `True` to merge trees.
    :type  merge: ``bool``

    :param temp_dir: The directory for generated code, if omitted, system temp will be used
    :type  temp_dir: ``str``
    z0click is not installed, please install using pipr<N�.rz?Choose which type of extension you are developing for SaltStackzExtension typerz!Module extension option not validcsg|]
}|d�kr|�qS)rr)r8�m��	extensionrrr?szrun.<locals>.<listcomp>�z3Enter the short name for the module (e.g. mymodule)zModule namerzShort description of the modulez
templates/z%Y-%m-%dz%Y)�version�module_nameZshort_descriptionZrelease_date�yearZ	questionsr2ZquestionzNew module stored in %s) �	HAS_CLICK�print�sys�exitr!rrrrI�list�zipr6r	rQZSaltStackVersionZnext_release�namer�today�strftimer
rErr.�copy�update�tempfileZmkdtemprKr%rr)rOrZrZsalt_dir�mergeZtemp_dirZMODULE_OPTIONSZextension_typeZchosen_extensionZextension_contextrJrRZ
param_dictZadditional_context�key�valr2Z
prompt_varr1rrrNr�run�s\




�	



rc�__main__)NNNNFN)!�__doc__�loggingrr&rVr_�datetimerZjinja2rZsalt.utils.filesr	Zsalt.versionZsalt.serializers.yamlrZsalt.utils.odictr�	getLogger�__name__rr3rT�ImportErrorZierrr!r%r,r6rIrKrcrrrr�<module>sL
��#!
�b
�