ó
 çƒQc           @   s×   d  d l  m Z 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 „  Z d „  Z	 e j
 d k r‚ d Z n d Z d „  Z d	 „  Z d
 „  Z d „  Z e j
 d k rÊ e Z e Z n  d „  Z d S(   iÿÿÿÿ(   t   _Nc          C   sˆ   y) t  t j d ƒ ƒ }  |  d k r( |  SWn t t f k
 rB n Xy' t  t j d ƒ }  |  d k ri |  SWn t t f k
 rƒ n Xd S(   s-   try to count the number of CPUs on the systemt   SC_NPROCESSORS_ONLNi    t   NUMBER_OF_PROCESSORSi   (   t   intt   ost   sysconft   AttributeErrort
   ValueErrort   environt   KeyError(   t   n(    (    s4   /sys/lib/python2.7/site-packages/mercurial/worker.pyt	   countcpus   s    c         C   s}   |  j  d d ƒ } | rd y  t | ƒ } | d k r7 | SWqd t k
 r` t j t d ƒ ƒ ‚ qd Xn  t t t ƒ  d ƒ d ƒ S(   Nt   workert   numcpusi   s!   number of cpus must be an integeri   i    (	   t   configR   R   t   utilt   AbortR    t   mint   maxR   (   t   uit   sR
   (    (    s4   /sys/lib/python2.7/site-packages/mercurial/worker.pyt   _numworkers    s    t   posixgz®Gáz„?géŒ 9Y>)Fc         C   s6   | | } t  |  ƒ } | t | | | } | d k S(   se   try to determine whether the benefit of multiple processes can
    outweigh the cost of starting themg333333Ã?(   R   t   _startupcost(   R   t	   costperopt   nopst   lineart   workerst   benefit(    (    s4   /sys/lib/python2.7/site-packages/mercurial/worker.pyt
   worthwhile0   s    
c         C   s<   t  |  | t | ƒ ƒ r+ t |  | | | ƒ S| | | f Œ  S(   sF  run a function, possibly in parallel in multiple worker
    processes.

    returns a progress iterator

    costperarg - cost of a single task

    func - function to run

    staticargs - arguments to pass to every invocation of the function

    args - arguments to split into chunks, to pass to individual
    workers
    (   R   t   lent   _platformworker(   R   t
   costperargt   funct
   staticargst   args(    (    s4   /sys/lib/python2.7/site-packages/mercurial/worker.pyR   8   s    c         #   s8  t  j ƒ  \ } } t |  ƒ } t j t j ƒ ‰ t j t j t j ƒ g  d g ‰ ‰ xô t | | ƒ D]ã } t  j ƒ  } | d k r<t j t j ˆ ƒ y\ t  j	 | ƒ x; | | | f Œ  D]& \ }	 }
 t  j
 | d |	 |
 f ƒ q» Wt  j d ƒ Wq<t k
 rt  j d ƒ q<z |  j ƒ  Wd  t  j d ƒ Xq<Xn  ˆ j | ƒ qf Wˆ j ƒ  t  j	 | ƒ t  j | d d ƒ } ‡ f d †  ‰  ‡  ‡ ‡ f d †  } t j d | ƒ ‰ ˆ j ƒ  ‡ ‡ ‡ f d †  } yD x= | D]5 } | j d	 d
 ƒ } t | d ƒ | d
 d  f VqØWWn ˆ  ƒ  | ƒ  ‚  n X| ƒ  d  S(   Ni    s   %d %s
iÿ   t   rbc             sZ   xS ˆ  D]K }  y t  j |  t j ƒ Wq t k
 rQ } | j t j k rR ‚  qR q Xq Wd  S(   N(   R   t   killt   signalt   SIGTERMt   OSErrort   errnot   ESRCH(   t   pt   err(   t   pids(    s4   /sys/lib/python2.7/site-packages/mercurial/worker.pyt   killworkerse   s    c             sL   xE ˆ D]= }  t  t j ƒ  d ƒ } | r ˆ r | ˆ d <ˆ  ƒ  q q Wd  S(   Ni   i    (   t   _exitstatusR   t   wait(   R    t   st(   R.   R-   t   problem(    s4   /sys/lib/python2.7/site-packages/mercurial/worker.pyt   waitforworkersm   s
    
t   targetc             sg   t  j  t  j ˆ  ƒ ˆ j ƒ  ˆ d }  |  rc |  d k  rS t j t j ƒ  |  ƒ n  t j |  ƒ n  d  S(   Ni    (   R&   t   SIGINTt   joinR   R%   t   getpidt   syst   exit(   t   status(   t
   oldhandlerR2   t   t(    s4   /sys/lib/python2.7/site-packages/mercurial/worker.pyt   cleanupu   s    

t    i   iÿÿÿÿ(   R   t   pipeR   R&   t	   getsignalR5   t   SIG_IGNt	   partitiont   forkt   closet   writet   _exitt   KeyboardInterruptt	   tracebackt   appendt   reverset   fdopent	   threadingt   Threadt   startt   splitR   (   R   R!   R"   R#   t   rfdt   wfdR   t   pargst   pidt   it   itemt   fpR3   R=   t   linet   l(    (   R.   R;   R-   R2   R<   s4   /sys/lib/python2.7/site-packages/mercurial/worker.pyt   _posixworkerK   sL     

%c         C   s=   t  j |  ƒ r t  j |  ƒ St  j |  ƒ r9 t  j |  ƒ Sd S(   sˆ   convert a posix exit status into the same form returned by
    os.spawnv

    returns None if the process was stopped instead of exitingN(   R   t	   WIFEXITEDt   WEXITSTATUSt   WIFSIGNALEDt   WTERMSIG(   t   code(    (    s4   /sys/lib/python2.7/site-packages/mercurial/worker.pyt   _posixexitstatus‡   s    t   ntc         c   s{   t  |  ƒ } | | | | } } d } xM t | ƒ D]? } | } | | } | rg | d 7} | d 8} n  |  | | !Vq4 Wd S(   s,   partition a list into N slices of equal sizei    i   N(   R   t   xrange(   t   lstt   nslicesR
   t   chunkt   slopt   endRT   RN   (    (    s4   /sys/lib/python2.7/site-packages/mercurial/worker.pyRB   •   s    

(   t   i18nR    R)   R   R&   R8   RL   R   R   R   t   nameR   R   R   RY   R_   R   R/   RB   (    (    (    s4   /sys/lib/python2.7/site-packages/mercurial/worker.pyt   <module>   s   H						<	
	