
    mil                         d dl mZ d dlZd dlZd dlZd dlmZ d dlZd dlm	Z	 d dl
Z
d dlZd dlmZ d dlmZ ddlmZ  ej&                  e      Zd	 Zedd
ededefd       Zej2                  dfd
edefdZdedej6                  fdZd
efdZy)    )contextmanagerN)Path)rmtree)BaseContainer)	OmegaConf   )fatalc                 r   dd l }t        | t              r-| j                         D ci c]  \  }}|t	        |       c}}S t        | t
        t        f      r| D cg c]  }t	        |       c}S t        | |j                        r,| j                         j                         j                         S t        | t              rt        |       S |  t        | t        t        t        t        f      r| S t        | t               rt#        j$                  |       S t'        t)        |        d      c c}}w c c}w )Nr   z is not jsonable.)torch
isinstancedictitemsjsonablelisttupleTensordetachcputolistr   strintfloatboolr   r   to_container
ValueErrorrepr)valuer   kvs       D/var/www/stems/demucs_env/lib/python3.12/site-packages/dora/utils.pyr   r      s    %+0;;=941a8A;99	ED%=	)%*+++	E5<<	(||~!!#**,,	E4	 5z	*US%d,CD	E=	)%%e,,DK=(9:;; :+s   D.D4pathmodesuffixc              #      K   t        |       |z   }t        ||      5 }| ddd       t        j                  ||        y# 1 sw Y    xY ww)a?  
    Write to a temporary file with the given suffix, then rename it
    to the right filename. As renaming a file is usually much faster
    than writing it, this removes (or highly limits as far as I understand NFS)
    the likelihood of leaving a half-written checkpoint behind, if killed
    at the wrong time.
    N)r   openosrename)r!   r"   r#   tmp_pathfs        r    write_and_renamer*   -   sI      4y6!H	h	 IIh s   AAAA
Arbc                     	  |t        | |            S # t        t        j                  t        t
        f$ r!}t        j                  d| |       Y d}~yd}~ww xY w)zt
    Try to load from a path using torch.load, and handles various failure cases.
    Return None upon failure.
    zLAn error happened when trying to load from %s, this file will be ignored: %rN)r%   OSErrorpickleUnpicklingErrorRuntimeErrorEOFErrorloggerwarning)r!   loadr"   excs       r    try_loadr6   <   sQ    
Dt$%%V++\8D Z#	 s    #AAAmodule_namereturnc                     	 t        j                  |       S # t        $ r* t        j	                  d| d       t        d|  d       Y y w xY w)NzCould not import module %sT)exc_infozFailed to import module .)	importlibimport_moduleImportErrorr2   infor	   )r7   s    r    import_or_fatalr@   K   sL    9&&{33 90+M(Q789s    0A
	A
c                 2   dfd}t        | |       rn| j                         sJ t        j                  | j                  | j
                  dz   d      }t        j                  d|  d|        | j                  |       y	| j                         rJ y	)
zReliably delete the given folder, trying to remove while ignoring errors,
    and if any files remain, renaming to some trash folder.Fc                 :    dt         j                  d|        y )NTzError deleting file )r2   r3   )func
error_pathr:   errors      r    	_on_errorz"reliable_rmtree.<locals>._on_errorX   s    -j\:;    )onerror__trash)dirprefixr#   zDeletion of z failed, moving to N)	r   existstempfilemkdtempparentnamer2   r3   r'   )r!   rF   target_namerE   s      @r    reliable_rmtreerS   S   s     E<
 4#{{}}&&4;;tyy3W_`dV+>{mLMK ;;=  =rG   )wbz.tmp)
contextlibr   r<   loggingr&   pathlibr   r.   shutilr   rN   typingtpomegaconf.basecontainerr   	omegaconfr   logr	   	getLogger__name__r2   r   r   r*   r4   r6   Anyr@   rS    rG   r    <module>rb      s    &   	      1  			8	$<& 4 s 3   %kkt 4  9 9 9!$ !rG   