ó
à™xTc           @   s¦   d  d l  m Z d  d l Z d  d l Z d  d l Z d  d l Z e j Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z d „  Z e d	 k r¢ e d
 e j d ƒ n  d S(   iÿÿÿÿ(   t   divisionNc         C   s(   d d  l  } |  J| j d |  ƒ |  Jd  S(   Niÿÿÿÿt   file(   t	   tracebackt	   print_exc(   R   R   (    (    sQ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/command_line/parallel_simple.pyt   show_traceback   s    c         C   s   d t  j  ƒ  |  S(   Ns   JOB wall clock time: %.2f s(   t   time(   t   t0(    (    sQ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/command_line/parallel_simple.pyt   fmt_time   s    c            s’  d } ˆ j  | ƒ ‰  ˆ  d k  r( ˆ g Sˆ j  d ˆ  ƒ ‰ ˆ ˆ  k  rM ˆ g Sˆ ˆ  t | ƒ ˆ !j d ƒ } ˆ d 7‰ t | ƒ d k r� ˆ g Sg  ‰ ‡  ‡ ‡ ‡ f d †  } xÝ | D]Õ } t j | ƒ } t j d | ƒ } | d  k rõ | | ƒ qµ t | j	 d ƒ ƒ } | j	 d ƒ } t | j	 d	 ƒ ƒ } d d
 g t | | k ƒ }	 | d k rc| |	 7} n  x$ t
 | | |	 ƒ D] }
 | |
 ƒ qvWqµ Wˆ S(   Ns   $(MULTI:i    t   )t   ,i   c            s'   ˆ j  ˆ ˆ   t |  ƒ ˆ ˆ ƒ d  S(   N(   t   appendt   str(   t   a(   t   it   jt   linet   result(    sQ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/command_line/parallel_simple.pyt   rapp   s    s   (\d+)([-:])(\d+)$i   i   iÿÿÿÿt   -(   t   findt   lent   splitt   opt
   expandvarst   ret   matcht   Nonet   intt   groupt   xrange(   R   t   patt   fldsR   t   fldt   mt   ft   ot   lt   sR   (    (   R   R   R   R   sQ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/command_line/parallel_simple.pyt   process_dollar_multi   s6       
  c         C   si  t  j  ƒ  } d d l m } |  j d k r„ d G|  j GHt j j ƒ  y | j d |  j ƒ Wn t	 d t j ƒ n Xt
 | ƒ GHHnÔ t j j ƒ  d  } y% | j d |  j d t d t ƒ } Wn3 d d	 l m } | ƒ  } t	 d | ƒ d  } n Xt |  j d
 ƒ } | d  k	 r#| j | j ƒ n  | d  k	 rE| j | j ƒ  ƒ n  | t
 | ƒ IJ~ t j j ƒ  d  S(   Niÿÿÿÿ(   t   easy_runi    s   command:t   commandR   t   join_stdout_stderrt   stdout_splitlines(   t   StringIOt   w(   R   t   libtbxR'   t   indext   cmdt   syst   stdoutt   flusht   callR   R   R   t   fully_bufferedt   Truet   Falset	   cStringIOR+   t   opent   logt   writet   stdout_buffert   getvalue(   t   cmd_infoR   R'   t   siot   buffersR+   R"   (    (    sQ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/command_line/parallel_simple.pyt   run_one_cmd+   s>    			
c         C   sH  t  j ƒ  } zt j |  j ƒ } t  j | ƒ t  j | ƒ d d l m } | j	 d t
 d d ƒ ƒ t j d  d k  rŠ d d l m } n d d  l } t
 d	 d ƒ } t j ƒ  } y; | j d
 |  j d t d d d | d | d t ƒ j ƒ  Wn t d | ƒ n X| t | ƒ IJt j j ƒ  Wd  t j | ƒ rCt  j | ƒ n  Xd  S(   Niÿÿÿÿ(   t   printenvt   outt   os_environ_at_startR,   i   i   (   t
   subprocessR9   t   argst   shellt   bufsizeR1   t   stderrt   universal_newlinesR   (   i   i   (   t   ost   getcwdR   t   dirnameR9   t   mkdirt   chdirt   libtbx.command_lineRA   t   showR8   R0   t   version_infot   libtbx.easy_runRD   R   t   PopenR/   R5   t   waitR   R   R1   R2   t   isdir(   R=   t   cwd_on_entryt   dRA   RD   R9   R   (    (    sQ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/command_line/parallel_simple.pyt
   run_in_dirM   s4    		c      	      sŽ  t  |  ƒ d k r d g }  n  d d  l ‰ d d l m } | d d ˆ j j ƒ j d  d d d	 d
 d d d ƒj d  d d d d d ƒj d  d d d d
 d d d ƒj d d d d	 d
 d d d ƒj d |  ƒ } | j	 ‰ d d l
 m } d d  l ‰ d d  l ‰ d d l m } d d l m } g  } xm | j D]b } t j | ƒ } | j d d ƒ }	 t  |	 ƒ d k r{| j | ƒ q2|	 \ }
 } | t j |
 <q2Wg  ‰  ‡  ‡ f d †  } ˆ j d  k	 rãx! ˆ j D] } | d  | ƒ qÉWn  xí | D]å } t j | ƒ } xÍ t | ƒ j ƒ  j ƒ  D]³ } | j ƒ  } | j d! ƒ r?qn  | j d" ƒ rTqn  | j d# ƒ r²| j d  d ƒ }	 t  |	 ƒ d k r—|	 j d$ ƒ n  |	 d t j |	 d <qn  | d  | j d% | ƒ ƒ qWqêWt  t  ˆ  ƒ ˆ j! j" ƒ  ƒ } ˆ j# d  k	 rt$ d t  ˆ j# | ƒ ƒ } n  d& G| GHd' Gt  ˆ  ƒ GHHt% j& j' ƒ  ˆ j( j) d( d) ƒ } ‡ f d* †  ‰ ‡  ‡ ‡ f d+ †  } ˆ j* d  k r2x1 ˆ  D]) } d, | j+ | _, ˆ j( j- | j, ƒ q“W| d k  rx` ˆ  D], } d- G| j. GHt/ d. | ƒ ˆ d. | ƒ qÓWqt| j0 d/ | d0 t/ d ˆ  d1 d ƒ | ƒ  nBg  } x{ ˆ  D]s } d2 ˆ j* | j+ f } t j1 | ƒ r�ˆ j2 s�t% j3 d3 | | ƒ IJn  | j | ƒ n  t j4 | d4 ƒ | _, q?Wt  | ƒ d k rKˆ j2 sà| d5 ƒ ‚ n  d d6 l5 m6 } | d7 | ƒ } t  | ƒ d k rKx% | D] } t% j3 d8 | | ƒ IJqW| d9 ƒ ‚ qKn  | j0 d/ | d0 t7 d ˆ  d1 d ƒ | ƒ  H| ƒ  Ht% j& j' ƒ  d  S(:   Ni    s   --helpiÿÿÿÿ(   t   option_parsert   usages<   %s [options] [var=value] [...] [file_listing_commands] [...]s   --dirst   actiont   storet   typeR   t   helps$   create a sub-directory for each run.s   --force_clean_dirst
   store_trues7   forces removal of existing directories before creation.s	   --commandR
   s0   command to be executed, e.g. 'echo $(MULTI:1-4)'s   -js   --jobsR   s4   maximum number of parallel jobs (default: all CPUs).RE   (   t   easy_mp(   t   Sorry(   t   show_stringt   =i   i   c      	      sL   xE t  |  ƒ D]7 } t ˆ  ƒ } ˆ  j ˆ j d | d | d d  ƒ ƒ q Wd  S(   NR.   R/   R9   (   R&   R   R
   t
   group_argsR   (   R   R$   R.   (   t	   cmd_infosR-   (    sQ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/command_line/parallel_simple.pyt   cmd_infos_append•   s    R   t   #s   set s   setenv t    s
   $(DIRNAME)s   Number of processors:s   Number of jobs:t
   time_startt   nowc            sñ   |  j  d k p ˆ  j d  k	 } |  j d  k rQ | rí d |  j  GHd |  j GHqí nœ t j |  j ƒ s{ | rí d G|  j GHqí nr t |  j ƒ j ƒ  j	 ƒ  } t
 | ƒ d k r° d GHn  t
 | ƒ d k rÓ d j | ƒ GHn  t
 | ƒ d k rí d GHn  d  S(	   Ni    s+   MISSING: output of command with index %03d:s     %ss   MISSING:i
   s   @BEGINs   
s   @END(   R.   t   dirsR   R9   R/   R   t   isfileR8   t   readt
   splitlinesR   t   join(   R=   t   need_logt   lines(   t   co(    sQ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/command_line/parallel_simple.pyt   show_log°   s    c             sR   xK ˆ  D]C }  |  j  d k r1 ˆ j d  k r1 q n  d G|  j GHˆ d |  ƒ q Wd  S(   Ni    s   command:R=   (   R.   Rk   R   R/   (   R=   (   Re   Rr   Rs   (    sQ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/command_line/parallel_simple.pyt	   show_logsÁ   s
     s   log%03ds   command:R=   t	   processest   funct	   chunksizes   %s%03ds   exists already: %sR9   sV   Please remove the existing directories or files, or use a different --dirs assignment.(   t2   remove_or_rename_files_and_directories_if_possiblet   pathss   unable to remove or rename: %ss&   Failure removing existing directories.(8   R   t   libtbx.load_envt   libtbx.option_parserRY   t   envt   dispatcher_namet   optionR   t   processt   optionsR-   R`   t   libtbx.introspectiont   libtbx.utilsRa   t   libtbx.str_utilsRb   RE   R   R   R   R
   RJ   t   environR(   RL   R8   Rm   Rn   t   lstript
   startswitht   replacet   mint   introspectiont   number_of_processorst   jobst   maxR0   R1   R2   t   utilst
   show_timesRk   R.   R9   t   remove_filesR/   R@   t   pool_mapt   existst   force_clean_dirsRH   Ro   t   libtbx.clear_pathsRx   RX   (   RE   RY   t   command_lineR`   Ra   Rb   t   files_listing_commandst   argt   eargR   t   kt   vRf   R   t	   file_namet   file_dirt   llt   n_procRŽ   Rt   R=   t   old_dirsRW   Rx   t	   remaining(    (   Re   Rr   R-   Rs   sQ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/command_line/parallel_simple.pyt   runk   sÔ     									   !		
				t   __main__RE   i   (   t
   __future__R    R   R   R0   RJ   t   pathR   R   R   R&   R@   RX   R    t   __name__t   argv(    (    (    sQ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/command_line/parallel_simple.pyt   <module>   s   					"		‡