o
    i                     @   s   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	d
Zedee	ddf fddZdee	ef defddZdedefddZdd Zdd ZdS )    N)contextmanager)Path)Any	GeneratorIteratorUnion   )Warningspathreturnc              
   c   sP    t  }t |  }tt| z|V  W tt| dS tt| 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current r   J/home/ubuntu/.local/lib/python3.10/site-packages/weasel/util/filesystem.pyworking_dir   s   	"r   c               
   c   s    t t } | V  dd }ztjdkr tjt| |d W dS tjt| |d W dS  tyI } zt	
tjj| |d W Y d}~dS d}~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                 S   s   t |tj | | d S )N)r   chmodstatS_IWRITE)rmfuncr
   exr   r   r   force_remove,   s   z"make_tempdir.<locals>.force_remove)      )onexc)onerror)dirmsgN)r   tempfilemkdtempsysversion_infoshutilrmtreer   PermissionErrorwarningswarnr	   W801format)dr   er   r   r   make_tempdir    s   
$r/   c                 C   s(   t t|   t t   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
   r   r   r   is_cwd9   s   (r2   c                 C   s   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   r1   r   r   r   ensure_pathB   s   
r4   c                 C   s   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)cloudpathlibr5   )r
   r5   r   r   r   ensure_pathyN   s   r7   c                 C   s,   t j| }t j|}t j||g|kS )zD
    Check whether `child` is a path contained within `parent`.
    )r   r
   realpath
commonpath)parentchildparent_realpathchild_realpathr   r   r   is_subpath_ofW   s   r>   )r   r&   r   r$   r"   r)   
contextlibr   pathlibr   typingr   r   r   r   errorsr	   r   r   r/   boolr2   r4   r7   r>   r   r   r   r   <module>   s$     		