o nnh@sddlmZddlZddlZddlmZddlmZddlm Z m Z m Z ddgZ dd dZ dd dZd d ZdddZdddZdS)) annotationsN)EEXIST) AnsibleError)to_bytes to_nativeto_text unfrackpath makedirs_safeTcCst|ddd}|durttdd}n tj|r tj|}tjtjt|dd}tj|s;tj ||}|rCtj |}t tj |ddS)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_strictpassthru)errors nonstringNr ) rosgetcwdpathisfiledirname expanduser expandvarsisabsjoinrealpathrnormpath)rfollowbasedir b_basedir b_final_pathr=/usr/local/lib/python3.10/dist-packages/ansible/utils/path.pyrs    c Cst|}t|}tj|sFz|rt||WdSt|WdStyE}z|jtkr:t dt |t |fWYd}~dSd}~wwdS)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) rrrrexistsmakedirsOSErrorerrnorrr)rmoderpathb_rpatherrrr @s  cCsht|dd}d}tj|r|}n|dvrt}n tj|r&tj|}|r.tj|}t|ddS)z- returns directory for inventory or playbook r rN)N.) rrrisdirrrrabspathr)sourcednamerrrr\s       rFc Csz^tj|r\z#tj|rt|ntj|r#t|WWdSWWdSWWdSty[}z%|rOddl m }|}| d t |WYd}~WdSWYd}~WdSd}~wwWdStyhYdSw)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)rrr r*shutilrmtreerunlink Exceptionansible.utils.displayr.displayformatr)rwarnr'r.r4rrrcleanup_tmp_filens*     $ r7cCsd}t|dd}t|dd}|rtj|}tj|}|tjj}|tjj}z |dt||k}W|StyAY|Sw)z Compares paths to check if one is contained in the other :arg: child: Path to test :arg parent; Path to test against F)rN)rrrrsplitseplen IndexError)childparentrealtest abs_child abs_parentcprrr is_subpaths     rD)TN)N)F) __future__rrr/r#ransible.errorsr+ansible.module_utils.common.text.convertersrrr__all__rr rr7rDrrrrs     "