
    Vh                    z    d dl mZ d dlZd dlZd dlmZ d dlmZ d dlm	Z	m
Z
mZ ddgZddZdd	Zd
 ZddZddZy)    )annotationsN)EEXIST)AnsibleError)to_bytes	to_nativeto_textunfrackpathmakedirs_safec                   t        |dd      }| t        t        j                         d      }n>t        j                  j	                  |      rt        j                  j                  |      }t        j                  j                  t        j                  j                  t        | d                  }t        j                  j                  |      s t        j                  j                  ||      }|rt        j                  j                  |      }t        t        j                  j                  |      d      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
    :arg basedir: A byte string, text string, PathLike object, or `None`
        representing where a relative path should be resolved from.
        `None` will be substituted for the current working directory.
    :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_strictpassthru)errors	nonstringr   )r   osgetcwdpathisfiledirname
expanduser
expandvarsisabsjoinrealpathr   normpath)r   followbasedir	b_basedirb_final_paths        B/home/dcms/DCMS/lib/python3.12/site-packages/ansible/utils/path.pyr	   r	      s    & )>*UIRYY[1FG				"GGOOI.	77%%bgg&8&8$Od9e&fgL77==&ww||I|<ww''5277##L1:OPP    c           	     f   t        |       }t        |      }t        j                  j	                  |      s0	 |rt        j
                  ||       yt        j
                  |       yy# t        $ r@}|j                  t        k7  r#t        dt        |      dt        |            Y d}~yd}~ww xY w)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(z): N)r	   r   r   r   existsmakedirsOSErrorerrnor   r   r   )r   moderpathb_rpathes        r    r
   r
   C   s     EuoG77>>'"	tGT*G$ #  	tww& "S\]bScenopeq#rss !	ts   A' A' '	B006B++B0c                n   t        | d      } d}t        j                  j                  |       r| }nW| dv rt        j                         }n>t        j                  j                  |       rt        j                  j                  |       }|rt        j                  j                  |      }t        |d      S )z- returns directory for inventory or playbook r   r   N)N .)	r   r   r   isdirr   r   r   abspathr   )sourcednames     r    r   r   _   s    f%:;FE	ww}}V	?	"				'&5!677r!   c                   	 t         j                  j                  |       rm	 t         j                  j                  |       rt	        j
                  |        n6t         j                  j                  |       rt        j                  |        yyyy# t        $ rG}|r;ddl	m
}  |       }|j                  dj                  t        |                   Y d}~yY d}~yd}~ww xY w# t        $ r Y yw xY w)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.   shutilrmtreer   unlink	Exceptionansible.utils.displayr3   displayformatr   )r   warnr*   r3   r9   s        r    cleanup_tmp_filer<   q   s    77>>$
_77==&MM$'WW^^D)IIdO * (    _=%iGOO$J$Q$QRYZ[R\$]^^	 _  s;   C" A)B 	C8CC" C" CC" "	C.-C.c                   d}t        | d      }t        |d      }|r>t        j                  j                  |      }t        j                  j                  |      }|j	                  t        j                  j
                        }|j	                  t        j                  j
                        }	 |dt        |       |k(  }|S # t        $ r Y |S w xY w)z
    Compares paths to check if one is contained in the other
    :arg: child: Path to test
    :arg parent; Path to test against
     F)r   N)r	   r   r   r   splitseplen
IndexError)childparentrealtest	abs_child
abs_parentcps           r    
is_subpathrJ      s     DE%0IVE2JGG$$Y/	WW%%j1
$A%A#a&zQ
 K	  K	s   0C 	CC)TN)N)F)
__future__r   r   r4   r&   r   ansible.errorsr   +ansible.module_utils.common.text.convertersr   r   r   __all__r	   r
   r   r<   rJ    r!   r    <module>rP      sD   " # 	   ' T T /
*"QJt88$4r!   