B
    °F.\ø  ã               @   sz   d dl Z d dl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dd„ Zddd„Zdd	d
„ZdS )é    N)Úimport_moduleé   )Úimport_filec             C   sþ   |s| j  dd¡s|S |rv| j  dd¡svdd„ |D ƒ}x|D ]}t |¡‚q<W t dt|ƒdkrddndd	 |¡f ¡‚t| j  d¡ƒ}d
d„ | ¡ D ƒ}t|ƒdkrºt dt	| 
¡ ƒ ¡‚|rÌ|sÌt d¡‚|sÔ|S |d }tj|dd} | | t	|ƒ¡ |S )zKClick option callback providing validation of preload subcommand arguments.ZpreloadNc             S   s   g | ]}|  d ¡r|‘qS )ú-)Ú
startswith)Ú.0Úv© r	   ú5lib/python3.7/site-packages/distributed/preloading.pyú
<listcomp>   s    z)validate_preload_argv.<locals>.<listcomp>z%Got unexpected extra argument%s: (%s)r   ÚsÚ ú c             S   s$   g | ]}t |d  tjƒr|d  ‘qS )Ú
dask_setup)Ú
isinstanceÚclickÚCommand)r   Úmr	   r	   r
   r   #   s    z<Multiple --preload modules with click-configurable setup: %szPUnknown argument specified: %r Was click-configurable --preload target provided?r   F)Úallow_extra_args)ÚparamsÚgetr   ZNoSuchOptionZ
UsageErrorÚlenÚjoinÚ_import_modulesÚvaluesÚlistÚkeysZContextÚ
parse_args)ZctxZparamÚvalueZunexpected_argsÚaÚpreload_modulesZpreload_commandsZpreload_commandr	   r	   r
   Úvalidate_preload_argv   s0    
"r!   c                sÀ   i }x¶| D ]®}|  d¡r†|dk	rxtj |¡}tj ||¡}tj |¡r^t ||¡s^t 	d|¡ t
 ||¡ t|ƒd ‰ q¢t|ƒd ‰ n|tjkr˜t|ƒ tj| ‰ ‡ fdd„dD ƒ||< q
W |S )aÍ   Imports modules and extracts preload interface functions.

    Imports modules specified by names and extracts 'dask_setup'
    and 'dask_teardown' if present.


    Parameters
    ----------
    names: list of strings
        Module names or file paths
    file_dir: string
        Path of a directory where files should be copied

    Returns
    -------
    Nest dict of names to extracted module interface components if present
    in imported module.
    z.pyNzFile name collision: %sr   c                s   i | ]}t ˆ |d ƒ|“qS )N)Úgetattr)r   Zattrname)Úmoduler	   r
   ú
<dictcomp>d   s   z#_import_modules.<locals>.<dictcomp>)r   Údask_teardown)ÚendswithÚosÚpathÚbasenamer   ÚexistsÚfilecmpZcmpÚloggerÚerrorÚshutilÚcopyr   ÚsysÚmodulesr   )ÚnamesÚfile_dirZresult_modulesÚnamer)   Zcopy_dstr	   )r#   r
   r   :   s$    




r   c       
      C   sš   t | |d}xˆ| ¡ D ]|\}}| dd¡}| dd¡}|rzt|tjƒrr|jdt|ƒdd}	|j|f|	j	ž|	j
Ž n||ƒ |d rt |d |¡ qW dS )a   Imports modules, handles `dask_setup` and `dask_teardown`.

    Parameters
    ----------
    names: list of strings
        Module names or file paths
    parameter: object
        Parameter passed to `dask_setup` and `dask_teardown`
    argv: [string]
        List of string arguments passed to click-configurable `dask_setup`.
    file_dir: string
        Path of a directory where files should be copied
    )r3   r   Nr%   F)r   )r   Úitemsr   r   r   r   Zmake_contextr   ÚcallbackÚargsr   ÚatexitÚregister)
r2   Z	parameterr3   ÚargvZimported_modulesr4   Z	interfacer   r%   Úcontextr	   r	   r
   r    l   s    r    )N)NNN)r8   Zloggingr'   r.   r0   r+   Ú	importlibr   r   Zutilsr   Z	getLoggerÚ__name__r,   r!   r   r    r	   r	   r	   r
   Ú<module>   s   
*
2