ó
ßüÚ\c           @   s®   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 j e ƒ Z d „  Z d d „ Z d d d d „ Z d S(   iÿÿÿÿN(   t   import_modulei   (   t   import_filec   
      C   s©  | r! |  j  j d d ƒ r! | S| rÂ |  j  j d d ƒ rÂ g  | D] } | j d ƒ rD | ^ qD } x | D] } t j | ƒ ‚ ql Wt j d t | ƒ d k r¦ d n d d j | ƒ f ƒ ‚ n  t	 |  j  j d ƒ ƒ } g  | j
 ƒ  D]& } t | d t j ƒ rç | d ^ qç } t | ƒ d k rGt j d	 t | j ƒ  ƒ ƒ ‚ n  | rf| rft j d
 ƒ ‚ n  | sp| S| d }	 t j |	 d t ƒ}  |	 j |  t | ƒ ƒ | S(   sK   Click option callback providing validation of preload subcommand arguments.t   preloadt   -s%   Got unexpected extra argument%s: (%s)i   t   st    t    t
   dask_setups<   Multiple --preload modules with click-configurable setup: %ssP   Unknown argument specified: %r Was click-configurable --preload target provided?i    t   allow_extra_argsN(   t   paramst   gett   Nonet
   startswitht   clickt   NoSuchOptiont
   UsageErrort   lent   joint   _import_modulest   valuest
   isinstancet   Commandt   listt   keyst   Contextt   Falset
   parse_args(
   t   ctxt   paramt   valuet   vt   unexpected_argst   at   preload_modulest   mt   preload_commandst   preload_command(    (    s5   lib/python2.7/site-packages/distributed/preloading.pyt   validate_preload_argv   s4    (4&
c            s  i  } x|  D]û } | j  d ƒ rÅ | d k	 r² t j j | ƒ } t j j | | ƒ } t j j | ƒ r t j | | ƒ s t	 j
 d | ƒ q n  t j | | ƒ t | ƒ d ‰  qî t | ƒ d ‰  n) | t j k rá t | ƒ n  t j | ‰  ‡  f d †  d Dƒ | | <q W| S(	   sÍ   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.
    s   .pys   File name collision: %si    c            s%   i  |  ] } t  ˆ  | d  ƒ | “ q S(   N(   t   getattrR   (   t   .0t   attrname(   t   module(    s5   lib/python2.7/site-packages/distributed/preloading.pys
   <dictcomp>h   s   	R   t   dask_teardownN(   R   R*   (   t   endswithR   t   ost   patht   basenameR   t   existst   filecmpt   cmpt   loggert   errort   shutilt   copyR   t   syst   modulesR    (   t   namest   file_dirt   result_modulest   nameR.   t   copy_dst(    (   R)   s5   lib/python2.7/site-packages/distributed/preloading.pyR   =   s$    c   
      C   s×   t  |  d | ƒ} x¾ | j ƒ  D]° \ } } | j d d ƒ } | j d d ƒ } | r® t | t j ƒ r¡ | j d t | ƒ d t	 ƒ}	 | j
 | |	 j |	 j Ž q® | | ƒ n  | d r t j | d | ƒ q q Wd S(   s   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
    R9   R   R*   R   N(   R   t   itemsR
   R   R   R   R   t   make_contextR   R   t   callbackt   argsR	   t   atexitt   register(
   R8   t	   parameterR9   t   argvt   imported_modulesR;   t	   interfaceR   R*   t   context(    (    s5   lib/python2.7/site-packages/distributed/preloading.pyR!   o   s    
(   RA   t   loggingR,   R4   R6   R0   t	   importlibR    R   t   utilsR   t	   getLoggert   __name__R2   R%   R   R   R!   (    (    (    s5   lib/python2.7/site-packages/distributed/preloading.pyt   <module>   s   	-2