o
    Ni                  
   @   s  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l
mZ ddlmZmZ ddlmZ d"d	ed
ee defddZ	d"dedee de	eee f fddZ	d#dedee dedee fddZdejfddZdedefddZdedefddZdedefd d!ZdS )$    N)sha256)Path)ListOptionalTuple)urlparse   )	PathOrStrget_cache_dir)Metaresourceetagreturnc                 C   sH   t | d}t|}| }|r"|d}t|}|d|  7 }|S )z
    Convert a ``resource`` into a hashed filename in a repeatable way.
    If ``etag`` is specified, append its hash to the resources', delimited
    by a period.

    THis is essentially the inverse of :func:`filename_to_url()`.
    zutf-8.)strencoder   	hexdigest)r   r   resource_bytesresource_hashfilename
etag_bytes	etag_hash r   D/home/ubuntu/.local/lib/python3.10/site-packages/cached_path/util.pyresource_to_filename   s   
r   r   	cache_dirc                 C   sn   |r|nt  }tj|| }tj|std||d }tj|s,td|t|}|j	|j
fS )z
    Return the URL and etag (which may be ``None``) stored for ``filename``.
    Raises :exc:`FileNotFoundError` if ``filename`` or its stored metadata do not exist.

    This is essentially the inverse of :func:`resource_to_filename()`.
    zfile {} not found.json)r
   ospathjoinexistsFileNotFoundErrorformatr   	from_pathr   r   )r   r   
cache_path	meta_pathmetadatar   r   r   filename_to_url    s   	
r'   Furlverbosec                 C   s   t |r|nt }t| }g }|| dD ]$}|r#t||j|j |jdv s.|jdr/q| j	}|
||f q|jdd dd |rM|d d S d	S )
zH
    Get the path to the latest cached version of a given resource.
    *>   r   .lockz
-extractedc                 S   s   | d S )Nr   r   )xr   r   r   <lambda>G   s    z$find_latest_cached.<locals>.<lambda>T)keyreverser   N)r   r
   r   globprintsuffixnameendswithstatst_mtimeappendsort)r(   r   r)   r   
candidatesr   mtimer   r   r   find_latest_cached6   s   
r;   tar_filec                    s   t jdd t j  dtdtf fdd}| D ]a}| s?| s?| s?| s?|	 s?t
dt| j d|j d	||j}t j |g krYt
dt| j d
| sa|	 r{||j}t j |g kr{t
dt| j dqdS )a"  Tar files can contain files outside of the extraction directory, or symlinks that point
    outside the extraction directory. We also don't want any block devices fifos, or other
    weird file types extracted. This checks for those issues and throws an exception if there
    is a problem.tmppathtestr   r   c                    s6   |  d} | dtj} tj | } tj| } | S )N/)rstripreplacer   sepr   r   normpath)r   	base_pathr   r   normalize_pathU   s
   
z%check_tarfile.<locals>.normalize_pathz	Tar file z contains invalid member r   z@ is trying to create a file outside of its extraction directory.zA is trying to link to a file outside of its extraction directory.N)r   r   r   rC   r   isregisdirisfileislnkissym
ValueErrorr3   commonprefixlinkname)r<   rF   tarinfotarget_pathr   rD   r   check_tarfileM   s<   

rQ   url_or_filenamec                 C   sH   | du rdS ddl m} tjt| } t| }|j| v p#tj| S )z
    Given something that might be a URL or local path,
    determine if it's actually a url or the path to an existing file.
    NFr   )get_supported_schemes)	schemesrS   r   r   
expanduserr   r   schemer    )rR   rS   parsedr   r   r   is_url_or_existing_filew   s   rX   r$   c                 C      | j | jd  S )Nr+   parentr3   r$   r   r   r   _lock_file_path      r]   c                 C   rY   )Nr   rZ   r\   r   r   r   _meta_file_path   r^   r_   )N)NF)r   tarfilehashlibr   pathlibr   typingr   r   r   urllib.parser   commonr	   r
   metar   r   r   r'   boolr;   TarFilerQ   rX   r]   r_   r   r   r   r   <module>   s>    

*