a
    hr                     @   s   d 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Zddlm	Z	 e
eZeejjd ZedddZeed	d
dZdd Zdd ZG dd dejZdd Zdd ZdS )zModule to implement logics used for initializing extensions.

The implementations here should be stateless.
They should not depend on external state.
Anything that depends on external state should happen in __init__.py
    N)Path)eval_envlib)r   c                 C   s(   t jdkrdnd}t|  d|  }|S )NntZpydso.)osname_LIB_DIR)r   suffixpath r   I/var/www/auris/lib/python3.9/site-packages/torchaudio/_extension/utils.py_get_lib_path   s    r   )r   returnc                 C   s$   t | }| sdS tj| dS )a  Load extension module

    Note:
        In case `torchaudio` is deployed with `pex` format, the library file
        is not in a standard location.
        In this case, we expect that `libtorchaudio` is available somewhere
        in the search path of dynamic loading mechanism, so that importing
        `_torchaudio` will have library loader find and load `libtorchaudio`.
        This is the reason why the function should not raising an error when the library
        file is not found.

    Returns:
        bool:
            True if the library file is found AND the library loaded without failure.
            False if the library file is not found (like in the case where torchaudio
            is deployed with pex format, thus the shared library file is
            in a non-standard location.).
            If the library file is found but there is an issue loading the library,
            (such as missing dependency) then this function raises the exception as-is.

    Raises:
        Exception:
            If the library file is found, but there is an issue loading the library file,
            (when underlying `ctype.DLL` throws an exception), this function will pass
            the exception as-is, instead of catching it and returning bool.
            The expected case is `OSError` thrown by `ctype.DLL` when a dynamic dependency
            is not found.
            This behavior was chosen because the expected failure case is not recoverable.
            If a dependency is missing, then users have to install it.
    FT)r   existstorchopsZload_library)r   r   r   r   r   	_load_lib   s
    r   c                  C   sN   t jdkrtdtdds$tdd} tj| s<tdtd t| S )	Nr   z)sox extension is not supported on WindowsZTORCHAUDIO_USE_SOXTz1sox extension is disabled. (TORCHAUDIO_USE_SOX=0)ztorchaudio.lib._torchaudio_soxzfTorchAudio is not built with sox extension. Please build TorchAudio with libsox support. (BUILD_SOX=1)Zlibtorchaudio_sox)	r   r	   RuntimeErrorr   	importlibutil	find_specr   import_module)extr   r   r   _import_sox_ext@   s    

r   c                  C   s`   t  } | d dd l}tjj  |tjjj g d}|D ]}t	| |t
tjj| q@| S )Nr   )get_infoZload_audio_fileZsave_audio_fileZapply_effects_tensorZapply_effects_file)r   set_verbosityatexitr   r   Ztorchaudio_soxZinitialize_sox_effectsregisterZshutdown_sox_effectssetattrgetattr)r   r   keyskeyr   r   r   	_init_soxT   s    
r$   c                       sH   e Zd ZdZ fddZdd Zdd Zdd	 Zd
d Zdd Z	  Z
S )_LazyImporterzLazily import module/extension.c                    s   t  | || _d | _d S N)super__init__import_funcmodule)selfr	   r)   	__class__r   r   r(   p   s    z_LazyImporter.__init__c                 C   s   |    t| j|S r&   )_import_oncer!   r*   )r+   itemr   r   r   __getattr__x   s    z_LazyImporter.__getattr__c                 C   s4   | j d u r*d| j d| jj d| j dS t| j S )Nz	<module 'r   z("z")'>)r*   
__module__r-   __name__r	   reprr+   r   r   r   __repr__|   s    
 z_LazyImporter.__repr__c                 C   s   |    t| jS r&   )r.   dirr*   r4   r   r   r   __dir__   s    z_LazyImporter.__dir__c                 C   s(   | j d u r$|  | _ | j| j j d S r&   )r*   r)   __dict__updater4   r   r   r   r.      s    

z_LazyImporter._import_oncec                 C   s&   z|    W n ty    Y dS 0 dS )NFT)r.   	Exceptionr4   r   r   r   is_available   s
    z_LazyImporter.is_available)r2   r1   __qualname____doc__r(   r0   r5   r7   r.   r;   __classcell__r   r   r,   r   r%   m   s   	r%   c               	   C   sL   t jdddD ]2} t j| rzt |  W q tyD   Y q0 qd S )NPATH ;)r   environgetsplitr   r   Zadd_dll_directoryr:   )r   r   r   r   _init_dll_path   s    rE   c                  C   s   dd l } | jj }|d urtjjd urt|}|d d  d|d  }tjjd}|d  d|d  }||krt	d| d| d|S )	Nr   r      zjDetected that PyTorch and TorchAudio were compiled with different CUDA versions. PyTorch has CUDA version z% whereas TorchAudio has CUDA version zJ. Please install the TorchAudio version that matches your PyTorch version.)
Ztorchaudio.lib._torchaudior   Z_torchaudioZcuda_versionr   versionZcudastrrD   r   )Z
torchaudiorI   version_strZ
ta_versionZ	t_versionr   r   r   _check_cuda_version   s     rL   )r=   r   loggingr   typespathlibr   r   Z!torchaudio._internal.module_utilsr   	getLoggerr2   Z_LG__file__parentr
   rJ   r   boolr   r   r$   
ModuleTyper%   rE   rL   r   r   r   r   <module>   s    
&)