
    i                         d dl Z d dlZd dlZd dl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mZ ddlmZ edeee	f   dee	   fd	       Zedee	ddf   fd
       Zdee	ef   defdZdedefdZd Zd Zy)    N)contextmanager)Path)Any	GeneratorIteratorUnion   )Warningspathreturnc              #   .  K   t        j                         }t        |       j                         }t        j                  t        |             	 | t        j                  t        |             y# t        j                  t        |             w xY ww)aV  Change current working directory and returns to previous on exit.

    path (str / Path): The directory to navigate to.
    YIELDS (Path): The absolute path to the current working directory. This
        should be used if the block needs to perform actions within the working
        directory, to prevent mismatches with relative paths.
    N)r   cwdresolveoschdirstr)r   prev_cwdcurrents      g/var/www/vps2.regionflexible.com/Desarrollo/venv/lib/python3.12/site-packages/weasel/util/filesystem.pyworking_dirr      s_      xxzH4j  "GHHS\ 
XXs   ABA2 B2 BBc               #     K   t        t        j                               } |  d }	 t        j                  dk\  r!t        j                  t        |       |       yt        j                  t        |       |       y# t        $ r>}t        j                  t        j                  j                  | |             Y d}~yd}~ww xY ww)zExecute a block in a temporary directory and remove the directory and
    its contents at the end of the with block.

    YIELDS (Path): The path of the temp directory.
    c                 \    t        j                  |t        j                          | |       y )N)r   chmodstatS_IWRITE)rmfuncr   exs      r   force_removez"make_tempdir.<locals>.force_remove,   s    
t}}%t    )      )onexc)onerror)dirmsgN)r   tempfilemkdtempsysversion_infoshutilrmtreer   PermissionErrorwarningswarnr
   W801format)dr   es      r   make_tempdirr3       s      	X A
G:w&MM#a&5MM#a&,7 :hmm**qa*899:s:   %C3A= C A= <C=	C4B?:C?CCc                     t        t        |       j                               j                         t        t        j                         j                               j                         k(  S )zCheck whether a path is the current working directory.

    path (Union[Path, str]): The directory path.
    RETURNS (bool): Whether the path is the current working directory.
    )r   r   r   lowerr   r   s    r   is_cwdr7   9   sG     tDz!!#$**,DHHJ4F4F4H0I0O0O0QQQr   c                 <    t        | t              rt        |       S | S )zEnsure string is converted to a Path.

    path (Any): Anything. If string, it's converted to Path.
    RETURNS: Path or original argument.
    )
isinstancer   r   r6   s    r   ensure_pathr:   B   s     $Dzr   c                     ddl m}  ||       S )zTemporary helper to prevent importing cloudpathlib globally (which was
    originally added due to a slow and annoying Google Cloud warning with
    Pathy)r   )AnyPath)cloudpathlibr<   )r   r<   s     r   ensure_pathyr>   N   s     %4=r   c                     t         j                  j                  |       }t         j                  j                  |      }t         j                  j                  ||g      |k(  S )zD
    Check whether `child` is a path contained within `parent`.
    )r   r   realpath
commonpath)parentchildparent_realpathchild_realpaths       r   is_subpath_ofrF   W   sK     gg&&v.OWW%%e,N77?@OSSr   )r   r*   r   r(   r&   r-   
contextlibr   pathlibr   typingr   r   r   r   errorsr
   r   r   r3   boolr7   r:   r>   rF    r   r   <module>rM      s    	   
   %  2 2   eCI&  8D>    " :idD 01 : :0RtSy! Rd R	c 	c 	
Tr   