a ì)gôã@sddlmZmZmZeZddlZddlZddlm Z ddl m Z ddl m Z mZmZddgZdd d„Zdd d„Zd d „Zddd„Zddd„ZdS)é)Úabsolute_importÚdivisionÚprint_functionN)ÚEEXIST)Ú AnsibleError)Úto_bytesÚ to_nativeÚto_textÚ unfrackpathÚ makedirs_safeTcCsšt|ddd}|dur(tt ¡dd}ntj |¡r@tj |¡}tj tj t|dd¡¡}tj |¡svtj  ||¡}|r†tj  |¡}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_strictZpassthru)ÚerrorsZ nonstringN©r ) rÚosÚgetcwdÚpathÚisfileÚdirnameÚ expanduserÚ expandvarsÚisabsÚjoinÚrealpathr Únormpath)rÚfollowÚbasedirZ b_basedirZ b_final_path©rú6/usr/lib/python3.9/site-packages/ansible/utils/path.pyr s    c Csˆt|ƒ}t|ƒ}tj |¡s„z |r0t ||¡n t |¡WnFty‚}z.|jtkrnt dt |ƒt |ƒfƒ‚WYd}~n d}~00dS)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 rrrÚexistsÚmakedirsÚOSErrorÚerrnorrr)rÚmodeZrpathZb_rpathÚerrrr As  cCsht|dd}d}tj |¡r"|}n*|dvr4t ¡}ntj |¡rLtj |¡}|r\tj |¡}t|ddS)z- returns directory for inventory or playbook r rN)NÚÚ.) rrrÚisdirrrrÚabspathr )ÚsourceZdnamerrrr]s      rFc Csªz’tj |¡rz2tj |¡r(t |¡ntj |¡r>t |¡WnNtyŽ}z6|rzddl m }|ƒ}|  d  t |ƒ¡¡WYd}~n d}~00Wnty¤Yn0dS)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)rrrr&ÚshutilÚrmtreerÚunlinkÚ ExceptionZansible.utils.displayr)ÚdisplayÚformatr )rÚwarnr#r)r.rrrÚcleanup_tmp_fileos      . r1cCs„d}t|dd}t|dd}|r8tj |¡}tj |¡}| tjj¡}| tjj¡}z|dt|ƒ…|k}Wnty~Yn0|S)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)r rrrÚsplitÚsepÚlenÚ IndexError)ÚchildÚparentÚrealÚtestZ abs_childZ abs_parentÚcÚprrrÚ is_subpath‰s     r<)TN)N)F)F)Z __future__rrrÚtypeZ __metaclass__rr*r!rZansible.errorsrZansible.module_utils._textrrr Ú__all__r r rr1r<rrrrÚs   "