File: //proc/self/root/usr/lib/python3.9/site-packages/ansible/utils/__pycache__/path.cpython-39.pyc
a
�)g� � @ s� d dl mZmZmZ eZd dlZd dlZd dlm Z d dl
mZ d dlm
Z
mZmZ ddgZdd d�Zdd
d�Zdd� Zddd�Zddd�ZdS )� )�absolute_import�division�print_functionN)�EEXIST)�AnsibleError)�to_bytes� to_native�to_text�unfrackpath�
makedirs_safeTc C s� t |ddd�}|du r(t t�� dd�}ntj�|�r@tj�|�}tj�tj�t | dd���}tj�|�svtj� ||�}|r�tj�
|�}ttj�|�dd�S )a�
Returns a path that is free of symlinks (if follow=True), environment variables, relative path traversals and symbols (~)
:arg path: A byte or text string representing a path to be canonicalized
:arg follow: A boolean to indicate of symlinks should be resolved or not
:raises UnicodeDecodeError: If the canonicalized version of the path
contains non-utf8 byte sequences.
:rtype: A text string (unicode on pyyhon2, str on python3).
:returns: An absolute path with symlinks, environment variables, and tilde
expanded. Note that this does not check whether a path exists.
example::
'$HOME/../../var/mail' becomes '/var/spool/mail'
�surrogate_or_strictZpassthru)�errorsZ nonstringN�r
)
r �os�getcwd�path�isfile�dirname�
expanduser�
expandvars�isabs�join�realpathr �normpath)r �follow�basedirZ b_basedirZb_final_path� r �6/usr/lib/python3.9/site-packages/ansible/utils/path.pyr
s c
C s� t | �}t|�}tj�|�s�z |r0t�||� n
t�|� W nF ty� } z.|jtkrnt dt
|�t
|�f ��W Y d}~n
d}~0 0 dS )a�
A *potentially insecure* way to ensure the existence of a directory chain. The "safe" in this function's name
refers only to its ability to ignore `EEXIST` in the case of multiple callers operating on the same part of
the directory chain. This function is not safe to use under world-writable locations when the first level of the
path to be created contains a predictable component. Always create a randomly-named element first if there is any
chance the parent directory might be world-writable (eg, /tmp) to prevent symlink hijacking and potential
disclosure or modification of sensitive file contents.
:arg path: A byte or text string representing a directory chain to be created
:kwarg mode: If given, the mode to set the directory to
:raises AnsibleError: If the directory cannot be created and does not already exist.
:raises UnicodeDecodeError: if the path is not decodable in the utf-8 encoding.
z*Unable to create local directories(%s): %sN)r
r r r �exists�makedirs�OSError�errnor r r )r �modeZrpathZb_rpath�er r r r A s
c C sh t | dd�} d}tj�| �r"| }n*| dv r4t�� }ntj�| �rLtj�| �}|r\tj�|�}t|dd�S )z- returns directory for inventory or playbook r r N)N� �.) r r r �isdirr r r �abspathr )�sourceZdnamer r r r ] s
r Fc
C s� z�t j�| �r�z2t j�| �r(t�| � nt j�| �r>t �| � W nN ty� } z6|rzddl m
} |� }|�d�t
|��� W Y d}~n
d}~0 0 W n ty� Y n0 dS )a
Removes temporary file or directory. Optionally display a warning if unable
to remove the file or directory.
:arg path: Path to file or directory to be removed
:kwarg warn: Whether or not to display a warning when the file or directory
cannot be removed
r )�Displayz#Unable to remove temporary file {0}N)r r r r&