o
    }oik7                     @   s   d dl Z d dlmZ d dlmZmZ d dlmZmZ d dl	m
Z
mZmZmZ d dlmZ g dZedd	d
d ZdedefddZdeeB defddZdeeB dededefddZd#deeB 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)	lru_cache)LocalTunnel	SSHTunnel)
DictConfig	OmegaConf)add_taskcheck_if_mountedget_mounts_from_configrun_exp)logging)r   r   r	   r
   
get_tunneladd_mount_pathcreate_remote_directorycreate_remote_configcheck_remote_mount_directoriesget_unmounted_filepathget_mounted_filepath   )maxsizec                  K   s   t di | S )z!
    Establishing ssh tunnel
    N )r   )
ssh_tunnelr   r   `/home/ubuntu/.local/lib/python3.10/site-packages/nemo/collections/common/parts/nemo_run_utils.pyr   '   s   r   mount_source
mount_destc                 C   s   |du rt dd|v rGt|}d}|D ]}|d\}}|| kr(||kr( dS q|sE|d |  d|  td|  d| d dS dS t d)	a  
    Add a mount path to the cluster config.

    Args:
        mount_source: The source filepath on the local/remote machine.
        mount_dest: The destination filepath on the remote/local machine. Must be an absolute path.
        cluster_config: The cluster config dictionary.
    NCluster config is not provided.mountsF:zAdded mount path: ``zGNo mounts found in cluster config, can only add to existing mount list.)
ValueErrorr	   splitappendr   info)r   r   cluster_configoriginal_mountsadded_mount
mount_pathsourcedestinationr   r   r   r   /   s   r   	directoryr"   c                 C   s  |du rt dt| tr| g} |ddkr9t| d d}| D ]}|jd| dd	d
 td| d q dS |ddkr||dd}|du rNt dd|vrX| d |d< tdi |d }| D ]}|jd| dd	d
 td| d qcdS t d|d )a  
    Create a remote directory on the cluster using the cluster config.

    **Note**: The ssh tunnel config must be provided in the cluster config for remote directory creation.

    Args:
        directory: The directory path to be created on the remote cluster. Can be a single directory path or a list
            of directory paths.
        cluster_config: The cluster config dictionary.
    Nr   executorlocalr   job_dir	mkdir -p FThidewarnzCreated directory:  in local filesystem.slurmr   :`ssh_tunnel` sub-config is not provided in cluster_config.r,    on remote cluster.Unsupported executor: r   )	r   
isinstancestrgetr   runr   r!   r   )r(   r"   tunneldir_pathssh_tunnel_configr   r   r   r   T   s,   
r   configconfig_nameconfig_directoryc                 C   s  |du rt d|ds| d}t|tr|g}t| tr$t| } |ddkrpt|d d}|D ]9}t	j
||}|jd| d	d
d |jd| d	d
d |jdt|  d| d	d
d td| d q4dS |ddkr|dd}|du rt dd|vr|d |d< tdi |d }|D ]9}t	j
||}|jd| d	d
d |jd| d	d
d |jdt|  d| d	d
d td| d qdS t d|d )a  
    Utility to write a remote config file on the cluster using the cluster config.

    Args:
        config: The config dictionary to be written to the file. Can be OmegaConf DictConfig or a dictionary.
        config_name: The name of the config file to be created.
        config_directory: The directory path where the config file will be created on the remote machine.
            Can be a single directory path or a list of directory paths to copy the config file to.
        cluster_config: The cluster config dictionary.
    Nr   z.yamlr)   r*   r   r+   r-   FTr.   ztouch zecho 'z' > zCreated config file: r1   r2   r   r3   r,   r4   r5   r   )r   endswithr6   r7   dictr   creater8   r   ospathjoinr9   to_yamlr   r!   r   )r=   r>   r?   r"   r:   r;   config_filepathr<   r   r   r   r      s@   




""r   Tdirectoriesexit_on_failurec                 C   s  |du rt dt| tr| g} |ddkrXtdd}g }| D ]}|jd| dddd	}d
|jvr8|| q t|dkrT|rVdd |D }d	|}t
d| dS dS |ddkr|dd}|du rmt dd|vrwt |d< tdi |d }g }| D ]}|jd| dddd	}d
|jvr|| qt|dkr|rdd |D }d	|}t
d| dS dS t d|d )a  
    Check if files and directories at the source location exist for later mounting on the cluster.

    Args:
        directories: The directory path to be checked on the local/remote machine. Can be a single directory
            path or a list. Can be either a file or a directory.
        cluster_config: The cluster config dictionary.
        exit_on_failure: If True, will raise an exception if the directories do not exist at the source location.
    Nr   r)   r*   r+   ztest -e z && echo "Directory Exists"Tr.   zDirectory Existsr   c                 S      g | ]}| d qS z% DOES NOT exist at source destinationr   .0locr   r   r   
<listcomp>       
z2check_remote_mount_directories.<locals>.<listcomp>
zOSome files or directories do not exist at the source location for mounting !!

r2   r   r3   r,   c                 S   rJ   rK   r   rL   r   r   r   rO     rP   r5   r   )r   r6   r7   r8   r   r9   stdoutr    lenrE   FileNotFoundErrorrC   getcwdr   )rH   r"   rI   r:   missing_source_locationsr(   resultr<   r   r   r   r      s`   







r   filepathc                 C   sx   d}| d D ]}| d\}}||r|} nq|du r)td| d| d  | d\}}||t|d  }|S )ar  
    Resolve the mounted filepath using the cluster config to merge the mount source path to the filepath.
    Raises an exception if the mount path is not found for the file path.

    Args:
        cluster_config: The cluster config dictionary.
        filepath: The filepath to be unmounted using the cluster config.

    Returns:
        str: unmounted filepath
    Nr   r   /Could not find a mount path for the file path ``. Below paths are mounted: 
r   
startswithr   rS   r"   rX   r%   mountr   r   r   r   r   r   "      
r   c                 C   sx   d}| d D ]}| d\}}||r|} nq|du r)td| d| d  | d\}}||t|d  }|S )as  
    Resolve the mounted filepath using the cluster config to merge the mount destination path to the filepath.
    Raises an exception if the mount path is not found for the file path.

    Args:
        cluster_config: The cluster config dictionary.
        filepath: The filepath to be mounted using the cluster config.

    Returns:
        str: mounted filepath
    Nr   r   rY   rZ   r[   r]   r   r   r   r   C  r_   r   )T)rC   	functoolsr   nemo_run.core.tunnelr   r   	omegaconfr   r   *nemo.collections.common.parts.skills_utilsr   r   r	   r
   
nemo.utilsr   __all__r   r7   r   listrA   r   r   boolr   r   r   r   r   r   r   <module>   s   
%5ET!