관리-도구
편집 파일: dataloader.cpython-39.opt-1.pyc
a �����)g�M����������������������@���s ��d�dl�mZmZmZ�eZd�dlZd�dlZd�dlZd�dl Z d�dl Z d�dlmZ �d�dlmZmZ�d�dlmZ�d�dlmZmZ�d�dlmZmZmZ�d�dlmZ�d�d lmZ�d�d lmZm Z m!Z!m"Z"m#Z#�d�dl$m%Z%�d�dl&m'Z'�e'��Z(e �)d ej*j+ej*j+f��Z,G�dd��d�Z-dS�)�����)�absolute_import�division�print_functionN)� constants)�AnsibleFileNotFound�AnsibleParserError)� is_executable)�binary_type� text_type)�to_bytes� to_native�to_text)�unquote)� from_yaml)�VaultLib�b_HEADER�is_encrypted�is_encrypted_file�parse_vaulttext_envelope)�unfrackpath)�Displayz(?:^|%s)+tasks%s?$c�������������������@���s����e�Zd�ZdZdd��Zdd��Zd4d d �Zd5dd�Zd d��Zdd��Z dd��Z dd��Zdd��Zd6dd�Z dd��Zdd��Zdd��Zd d!��Zd"d#��Zd7d$d%�Zd8d&d'�Zd(d)��Zd9d*d+�Zd,d-��Zd.d/��Zd:d0d1�Zd2d3��ZdS�);� DataLoadera �� The DataLoader class is used to load and parse YAML or JSON content, either from a given file name or from a string that was previously read in through other means. A Vault password can be specified, and any vault-encrypted files will be decrypted. Data read from files will also be cached, so the file will never be read from disk more than once. Usage: dl = DataLoader() # optionally: dl.set_vault_password('foo') ds = dl.load('...') ds = dl.load_from_file('/path/to/file') c�����������������C���s2���d|�_�t��|�_t��|�_i�|�_t��|�_|��d���d�S�)N�.) �_basedir�dict�_FILE_CACHE�set� _tempfilesZ_vaultsr����_vault�set_vault_secrets��self��r"����>/usr/lib/python3.9/site-packages/ansible/parsing/dataloader.py�__init__5���s����zDataLoader.__init__c�����������������C���s���||�j�_d�S��N)r����secrets)r!���Z vault_secretsr"���r"���r#���r���K���s����zDataLoader.set_vault_secrets�<string>TFc�����������������C���s���t�||||�jj|d�S�)zBackwards compat for now)� json_only)r���r���r&���)r!����data� file_name�show_contentr(���r"���r"���r#����loadN���s����zDataLoader.loadc����������� ������C���s~���|���|�}t�d|���|r2||�jv�r2|�j|�}n6|��|�\}}t|dd�}|�j||||d�}||�j|<�|rp|S�t�|�S�dS�)zA Loads data from a file, which can contain either JSON or YAML. zLoading data from %s�surrogate_or_strict��errors)r)���r*���r+���r(���N) � path_dwim�display�debugr����_get_file_contentsr ���r,����copy�deepcopy) r!���r*����cacheZunsafer(���Zparsed_dataZb_file_datar+���Z file_datar"���r"���r#����load_from_fileR���s���� zDataLoader.load_from_filec�����������������C���s���|���|�}tj�t|dd��S��Nr-���r.���)r0����os�path�existsr����r!���r:���r"���r"���r#����path_existsl���s���� zDataLoader.path_existsc�����������������C���s(���|���|�}tj�t|dd��p&|tjkS�r8���)r0���r9���r:����isfiler����devnullr<���r"���r"���r#����is_filep���s���� zDataLoader.is_filec�����������������C���s���|���|�}tj�t|dd��S�r8���)r0���r9���r:����isdirr���r<���r"���r"���r#����is_directoryt���s���� zDataLoader.is_directoryc�����������������C���s���|���|�}t�|�S�r%���)r0���r9����listdirr<���r"���r"���r#����list_directoryx���s���� zDataLoader.list_directoryc�����������������C���s���|���|�}t|�S�)zis the given path executable?)r0���r���r<���r"���r"���r#���r���|���s���� zDataLoader.is_executableNc����������� ������C���s@���t�|�sd}||fS�t|�\}}}}|�jj||d�}d}||fS�)zMDecrypt b_vault_data if encrypted and return b_data and the show_content flagT��filenameF)r���r���r����decrypt) r!���Zb_vault_data�b_file_namer+���Zb_ciphertextZ b_versionZcipher_nameZvault_idZb_datar"���r"���r#����_decrypt_if_vault_data����s����z!DataLoader._decrypt_if_vault_datac�������������� ���C���s����|rt�|ttf�s"tdt|����t|��|��}|��|�sFtd|d��zDt |d��&}|� ��}|��||�W��d����W�S�1�s~0����Y��W�n@�tt fy��}�z$td|t|�f�|d��W�Y�d}~n d}~0�0�dS�)a �� Reads the file contents from the given file name If the contents are vault-encrypted, it will decrypt them and return the decrypted data :arg file_name: The name of the file to read. If this is a relative path, it will be expanded relative to the basedir :raises AnsibleFileNotFound: if the file_name does not refer to a file :raises AnsibleParserError: if we were unable to read the file :return: Returns a byte string of the file contents �Invalid filename: '%s'z Unable to retrieve file contents�r*����rbN�8an error occurred while trying to read the file '%s': %s�Zorig_exc)� isinstancer ���r ���r���r���r���r0���r=���r����open�readrI����IOError�OSError)r!���r*���rH����fr)����er"���r"���r#���r3�������s���� 0zDataLoader._get_file_contentsc�����������������C���s���|�j�S�)z returns the current basedir )r���r ���r"���r"���r#����get_basedir����s����zDataLoader.get_basedirc�����������������C���s���|durt�|�|�_dS�)zK sets the base directory, used to find files when a relative path is given N)r ���r���)r!����basedirr"���r"���r#����set_basedir����s����zDataLoader.set_basedirc�����������������C���s^���t�|�}t|dd�}|�ttjj��s0|�d�r6|}nt|�jdd�}tj�||�}t|dd�S�)z= make relative paths work like folks expect. r-���r.����~F��follow) r���r ���� startswithr9���r:����sepr����joinr���)r!���Zgivenr:���rW���r"���r"���r#���r0�������s����zDataLoader.path_dwimc����������� ������C���s,��t�|dd�}tj�|�}t�t|dd�dd�}tj�|d�tj�|d�tj�|d�f}tj�|d�tj�|d �tj�|d �tj�|d�tj�|d�tj�|d �tj�|d�tj�|d �tj�|d �tj�|d�tj�|d�tj�|d �f}ttjj|�}ttjj|�}t� |��rt |��s$t |��r(dS�dS�)zR imperfect role detection, roles are still valid w/o tasks|meta/main.yml|yaml|etc r-���r.���FrZ���s���main.ymls ���main.yamls���mains���tasks/main.ymls���tasks/main.yamls ���tasks/mains ���meta/main.ymls���meta/main.yamls ���meta/mainT)r���r9���r:����dirnamer���r^����mapr;����RE_TASKS�search�any) r!���r:����b_pathZb_path_dirname�b_upathZuntasked_pathsZtasked_pathsZexists_untaskedZ exists_taskedr"���r"���r#����_is_role����s2������ zDataLoader._is_rolec����������� ������C���s���g�}t�|dd�}|�t�tjj��s,|�d�rB|�t|dd����n|�tj�|||���t|dd�}|sr|��|�}|r�t � |�r�ttj�|�dd�}|�j}|�� |��|�ttj�|||�dd���|�� |��|r�|�|�s�|�ttj�|d|�dd���|�ttj�||�dd���|�ttj�||�dd���|�|��tj�||����|�|��|���|D�]"}tj�t|dd���r^��q��q^|S�)z� find one file in either a role or playbook dir with or without explicitly named dirname subdirs Used in action plugins and lookups to find supplemental files that could be in either place. r-���r.���rY���FrZ���Ztasks)r ���r\���r9���r:���r]����appendr���r^���rf���ra���rb���r_���r���rX����endswithr0���r;���r���) r!���r:���r_����source�is_rolerb���rW���Zcur_basedir� candidater"���r"���r#����path_dwim_relative����s0���� zDataLoader.path_dwim_relativec�����������������C���s��t�|dd�}t�|dd�}d}g�}|du�r6t�d���n�|rz|�d�sR|�tjj�rzt|dd�} tj�t�| dd��rv| }�n|t� dd � |����|D�]�} t| dd�}t�|dd�}tj�|�} |s�|��| ��r | � d ��r |�tj� tj�| �||���|�tj� | |���q�|�d�d�|k�r4|�tj� |||���|�tj� ||���q�|�d�d�|k�r�|�tj� t�|����dd�||���|�tj� t�|����dd�|���t� d td� |�����|D�]8}t�d|t|�f���tj�|��r�t|�}��q��q�|du��rt|dd��|D��d��|S�)au�� find one file in first path in stack taking roles into account and adding play basedir as fallback :arg paths: A list of text strings which are the paths to look for the filename in. :arg dirname: A text string representing a directory. The directory is prepended to the source to form the path to search for. :arg source: A text string which is the filename to search for :rtype: A text string :returns: An absolute path to the filename ``source`` if found :raises: An AnsibleFileNotFound Exception if the file is found to exist in the search paths r-���r.���Nz:Invalid request to find a file that matches a "null" valuerY���FrZ���zevaluation_path: %sz s���/tasks����/r���zsearch_path: %ss��� zlooking for "%s" at "%s"c�����������������S���s���g�|�]}t�|��qS�r"���)r���)�.0�pr"���r"���r#���� <listcomp>U�������z7DataLoader.path_dwim_relative_stack.<locals>.<listcomp>)r*����paths)r���r1����warningr\���r9���r:���r]���r���r;���r2���r^���r_���rf���rh���rg����splitrV���r ���Zvvvvvr���)r!���rr���r_���ri���rj���Z b_dirnameZb_source�resultrb���Z test_pathr:���Zupathre���Z b_pb_base_dirZb_candidater"���r"���r#����path_dwim_relative_stack��sB����" z#DataLoader.path_dwim_relative_stackc�������������� ���C���s����t�jtjd�\}}t�|d�}t|�}zRz|�|��W�n6�tyl�}�zt� |��t|��W�Y�d}~n d}~0�0�W�|� ���n |� ���0�|S�)z. Create a tempfile containing defined content )�dir�wbN)�tempfileZmkstemp�CZDEFAULT_LOCAL_TMPr9����fdopenr����write� Exception�remove�close)r!���Zcontent�fdZcontent_tempfilerT����errr"���r"���r#����_create_content_tempfileY��s���� z#DataLoader._create_content_tempfilec�������������� ���C���s0��|rt�|ttf�s"tdt|����t|dd�}|��|�rB|��|�sLt|d��|�� |�}z�|r�t t|�d��f}t|tt �d�r�|���}|�jjs�tdt|����|�jj||d�}|��|�}|�j�|��W�d ����n1�s�0����Y��|W�S��ttf�y*�}�z(td t|�t|�f�|d��W�Y�d }~n d }~0�0�d S�)z� If the file is vault encrypted return a path to a temporary decrypted file If the file is not encrypted then the path is returned Temporary files are cleanup in the destructor rJ���r-���r.���rK���rL���)�countz:A vault password or secret must be specified to decrypt %srE���NrM���rN���)rO���r ���r ���r���r���r���r=���r@���r���r0���rP���r����lenr���rQ���r���r&���rG���r����r����addrR���rS���)r!���� file_pathrG���Zb_file_pathZ real_pathrT���r)���rU���r"���r"���r#���� get_real_fileg��s&���� *zDataLoader.get_real_filec�����������������C���s$���||�j�v�r t�|��|�j��|��dS�)z� Removes any temporary files created from a previous call to get_real_file. file_path must be the path returned from a previous call to get_real_file. N)r���r9����unlinkr~���)r!���r����r"���r"���r#����cleanup_tmp_file���s���� zDataLoader.cleanup_tmp_filec�����������������C���sZ���t�|�j�D�]J}z|��|��W�q �tyR�}�zt�dt|����W�Y�d}~q d}~0�0�q dS�)z� Removes all temporary files that DataLoader has created NOTE: not thread safe, forks also need special handling see __init__ for details. z Unable to cleanup temp files: %sN)�listr���r����r}���r1���rs���r ���)r!���rT���rU���r"���r"���r#����cleanup_all_tmp_files���s ����z DataLoader.cleanup_all_tmp_filesc����������� ������C���s����t�tj�||��}g�}|du�r*dgtj�}|D�]x}d|v�rH|t�|��}n|r`d�|t�|�g�}n|}|��|�r.|��|�r�|r.|�|�� t |�|���q�q.n |�|���q�q.|S�)z� Find vars files in a given path with specified name. This will find files in a dir named <name>/ or a file called <name> ending in known extensions. N��r�������.)r���r9���r:���r^���rz���ZYAML_FILENAME_EXTENSIONSr=���rB����extend�_get_dir_vars_filesr ���rg���) r!���r:����name� extensionsZ allow_dirrd����found�ext� full_pathr"���r"���r#����find_vars_files���s$���� zDataLoader.find_vars_filesc�����������������C���s����g�}t�|��|��D�]|}|�d�s|�d�stj�|�d�}tj�||�}|��|�rj|sj|� |�� ||���q|��|�r|r�t|�|v�r|� |��q|S�)Nr���rY������)�sortedrD���r\���rh���r9���r:����splitextr^���rB���r����r����r@���r ���rg���)r!���r:���r����r����Zspathr����Z full_spathr"���r"���r#���r�������s����zDataLoader._get_dir_vars_files)r'���TF)TFF)N)F)F)T)NT)�__name__� __module__�__qualname__�__doc__r$���r���r,���r7���r=���r@���rB���rD���r���rI���r3���rV���rX���r0���rf���rl���rv���r����r����r����r����r����r����r"���r"���r"���r#���r���"���s0��� " 7 ; ( "r���).Z __future__r���r���r����typeZ __metaclass__r4���r9���Zos.path�rery���Zansibler���rz���Zansible.errorsr���r���Zansible.module_utils.basicr���Zansible.module_utils.sixr ���r ���Zansible.module_utils._textr���r���r ���Zansible.parsing.quotingr���Zansible.parsing.utils.yamlr���Zansible.parsing.vaultr���r���r���r���r���Zansible.utils.pathr���Zansible.utils.displayr���r1����compiler:���r]���ra���r���r"���r"���r"���r#����<module>���s&���