o
    *jl6                     @   s  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mZm	Z	 d dl
mZmZmZ d dlmZ d dlmZ e Zdd Zd	d
 ZdefddZdefddZdefddZdedefddZdedefddZdd ZddeddfddZdeeeeef defddZ	 	!	"d2deeeeef d#ee d$ee d%ee  de!f
d&d'Z"	 	!	"d2d(eeef d#ed$ee d%ee  de!f
d)d*Z#	 	+	!	"d3deeeeef d#ee d,ed$ee d%ee  de!fd-d.Z$d/ede fd0d1Z%dS )4    N)Path)Errorcopy2copystat)BinaryIOOptionalUnion)urlparse)
get_loggerc                 C   sx   t | }|j}|j}|du r|du sdS |jsg n|j}|r*|d dv r*|dn| t|dkr:|d dv r:dS dS )zto decide if a func could receive dict inputs or not

    Args:
        func (class): the target function to be inspected

    Returns:
        bool: if func only has one arg ``input`` or ``inputs``, return True, else return False
    NFr   )selfcls   )inputZinputsT)inspectgetfullargspecvarargsvarkwargspoplen)funcZfull_args_specr   r   r    r   \/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/utils/file_utils.pyfunc_receive_dict_inputs   s   
	r   c                  C   s   t jt ddd} | S )z1
    default base dir: '~/.cache/modelscope'
    z.cacheZ
modelscopeZhub)ospath
expanduserr   homejoinpath)Zdefault_cache_dirr   r   r    get_default_modelscope_cache_dir)   s   r   returnc                   C   s   t jt dt S )zGet modelscope cache dir, default location or
       setting with MODELSCOPE_CACHE

    Returns:
        str: the modelscope cache root.
    ZMODELSCOPE_CACHE)r   r   r   getenvr   r   r   r   r   get_modelscope_cache_dir2   s   r"   c                   C      t jt dS )z[Get model cache root path.

    Returns:
        str: the modelscope model cache root.
    modelsr   r   joinr"   r   r   r   r   get_model_cache_root=   s   r'   c                   C   r#   )zGet dataset raw file cache root path.
    if `MODELSCOPE_CACHE` is set, return `MODELSCOPE_CACHE/datasets`,
    else return `~/.cache/modelscope/hub/datasets`

    Returns:
        str: the modelscope dataset raw file cache root.
    Zdatasetsr%   r   r   r   r   get_dataset_cache_rootF   s   r(   
dataset_idc                 C   $   t  }| du r	|S tj|| d S )zGet the dataset_id's path.
       dataset_cache_root/dataset_id.

    Args:
        dataset_id (str): The dataset id.

    Returns:
        str: The dataset_id's cache root path.
    N/)r(   r   r   r&   )r)   Zdataset_rootr   r   r   get_dataset_cache_dirQ      
r,   model_idc                 C   r*   )zcache dir precedence:
        function parameter > environment > ~/.cache/modelscope/hub/model_id

    Args:
        model_id (str, optional): The model id.

    Returns:
        str: the model_id dir if model_id not None, otherwise cache root dir.
    Nr+   )r'   r   r   r&   )r.   	root_pathr   r   r   get_model_cache_dir`   r-   r0   c                 C   s8   t | d}| }W d    |S 1 sw   Y  |S )Nr)openread)r   ftextr   r   r   	read_fileo   s   

r6   Fc                 C   s  t | }|dur|| |}nt }t j||d g }	|D ]}
|
|v r$qt j| |
}t j||
}zUt j|rpt |}|rOt || t	||| d n7t j
|sY|rYW qt j|rjt||||||d n||| nt j|rt||||||d n||| W q ty } z|	|jd  W Y d}~qd}~w ty } z|	||t|f W Y d}~qd}~ww zt	| | W n$ ty } zt|dddu r|	| |t|f W Y d}~nd}~ww |	rt|	|S )z7copy from py37 shutil. add the parameter dirs_exist_ok.N)exist_ok)follow_symlinks)dirs_exist_okr   winerror)r   listdirsetmakedirsr   r&   islinkreadlinksymlinkr   existsisdircopytree_py37r   extendr   OSErrorappendstrgetattr)srcdstsymlinksignorecopy_functionignore_dangling_symlinksr9   namesignored_nameserrorsnamesrcnamedstnamelinktoerrwhyr   r   r   rC   v   sr   


	 rC   file_path_or_objc                 C   sr   t | ttfrt| }| jS t | trt| S t | tjr5| 	 }| 
dtj | 	 }| 
| |S td)Nr   zCUnsupported type: must be string, Path, bytes, or io.BufferedIOBase)
isinstancerG   r   statst_sizebytesr   ioBufferedIOBasetellseekr   SEEK_END	TypeError)rX   	file_pathcurrent_positionsizer   r   r   get_file_size   s   


rf      [Calculating]Tbuffer_size_mb	tqdm_descdisable_tqdmc              	   C   s,  ddl m} t| }|dkr-d}d}t| ttfr't| tr | nt| }|j}d| d}|d d }t }	||dd	d	d
||d}
t| ttfrd}t	| d(}|
| }rq|	| |t|7 }|
t| |
| }sXW d   n1 s{w   Y  |	 }	||krtd| d| d|   |}nst| tr|	|  |	 }	|
t|  t| }nYt| tjrd}| dtj | 
| }r|	| |t|7 }|
t| | 
| }s|	 }	| dtj ||krtd| d| d|   |}n|
  td|
  | |	|dS )a  Compute SHA256 hash for a file path, bytes, or file-like object.

    Args:
        file_path_or_obj: File path, bytes, or file-like object.
        buffer_size_mb: Read buffer size in MB. Default 16MB.
        tqdm_desc: Progress bar description.
        disable_tqdm: Whether to disable progress bar.

    Returns:
        dict with keys: file_path_or_obj, file_hash, file_size.
    r   tqdmi   @Fz
Large Filez[Validating Hash for ]   TB)totalinitial
unit_scaledynamic_ncolsunitdescdisablerbN4File size changed during hash computation: declared  bytes, actually hashed % bytes. File may have been modified: z5Input must be str, Path, bytes or a io.BufferedIOBaserX   	file_hash	file_size)	tqdm.autorm   rf   rY   rG   r   rR   hashlibsha256r2   r3   updater   	hexdigestloggerwarningr\   r]   r^   r`   r   SEEK_SETclose
ValueError)rX   ri   rj   rk   rm   declared_sizerR   r   buffer_sizer}   progressbytes_hashedr4   Z
byte_chunkr~   r   r   r   get_file_hash   s   






r   rc   c              	      s*  ddl }ddl}ddlm} ttj}|d d  |jdddg fdd}|j	|d	d
}	|	
  t }
d}||||d	dd	dd}	  }|du rVn|
| |t|7 }|t| qM|	  |  d dur{d ||krtd| d| d  |
 |dS )a  Compute SHA256 with async I/O double-buffering for high-latency storage.

    Uses a producer-consumer pattern: a background thread reads file chunks
    into a bounded queue while the main thread computes the hash. This
    overlaps I/O latency with CPU computation.
    r   Nrl   ro      )maxsizec               
      s   zMz&t d} 	 |  }|sn| q	W d    n1 s!w   Y  W n ty= } z
|d< W Y d }~nd }~ww W d  d S W d  d S d  w )Nrx   Tr   )r2   r3   put	Exception)r4   chunker   Zchunk_queuerc   Z
read_errorr   r   	_producerF  s&   

z'_get_file_hash_async.<locals>._producerT)targetdaemonrp   )rq   rv   rw   rt   ru   rs   Zunit_divisorry   rz   r{   r|   )queue	threadingr   rm   rG   r   r   getsizeQueueThreadstartr   r   getr   r   r&   r   r   r   r   )rc   ri   rj   rk   r   r   rm   r   r   Zreader_threadr}   r   r   r   r   r   r   _get_file_hash_async.  s`   

r       async_threshold_mbc                 C   sN   t | ttfrtjt| }||d d krt| |||dS t| |||dS )a  Compute SHA256 hash with automatic optimization for large files.

    For file paths larger than async_threshold_mb, uses async double-buffered
    I/O to overlap disk reads with hash computation. For smaller files or
    non-path inputs (bytes, BinaryIO), falls back to synchronous hashing.

    Args:
        file_path_or_obj: File path, bytes, or file-like object.
        buffer_size_mb: Read buffer size in MB. Default 16MB for NAS optimization.
        async_threshold_mb: File size threshold for async mode. Default 32MB.
        tqdm_desc: Progress bar description.
        disable_tqdm: Whether to disable progress bar.

    Returns:
        dict with keys: file_path_or_obj, file_hash, file_size.
    ro   )ri   rj   rk   )rY   rG   r   r   r   r   r   r   )rX   ri   r   rj   rk   r~   r   r   r   compute_file_hash}  s   r   url_or_filenamec                 C   s   t | jdkotj|  S )z5
    Check if a given string is a relative path.
     )r	   schemer   r   isabs)r   r   r   r   is_relative_path  s   r   )rg   rh   T)rg   r   rh   T)&r   r   r]   r   pathlibr   shutilr   r   r   typingr   r   r   urllib.parser	   Zmodelscope.utils.loggerr
   r   r   r   rG   r"   r'   r(   r,   r0   r6   rC   r\   intrf   booldictr   r   r   r   r   r   r   r   <module>   s   			
I
_

Q
+