B
    Khh              	   @   sR  d 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mZm	Z	 ddl
mZ ddlmZ ddlmZmZmZmZ ddl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 ddlm Z  ddl!m"Z"m#Z# ddddZ$ed ddddZ%ej&ddddZ'eddee e(eee#ee) ddddZ*ee e(eedej&ddddd Z+dS )!z
Formatting many files at once via multiprocessing. Contains entrypoint and utilities.

NOTE: this module is only imported if we need to format several files at once.
    N)ExecutorProcessPoolExecutorThreadPoolExecutor)Manager)Path)AnyIterableOptionalSet)
mypyc_attr)	WriteBackformat_file_in_place)Cachefilter_cached
read_cachewrite_cache)Mode)err)ChangedReport)returnc              C   s.   yddl } |   W n tk
r(   Y nX dS )zIf our environment has uvloop installed we use it.

    This is called only from command-line entry points to avoid
    interfering with the parent process if Black is used as a library.
    r   N)uvloopinstallImportError)r    r   4/tmp/pip-install-vv81h98y/black/black/concurrency.pymaybe_install_uvloop   s
    r   zasyncio.Task[Any])tasksr   c             C   s"   t d x| D ]}|  qW dS )zFasyncio signal handler that cancels all `tasks` and reports to stderr.zAborted!N)r   cancel)r   taskr   r   r   r   (   s    
r   )loopr   c          	   C   sr   zLdd t | D }|sdS x|D ]}|  q$W | t j|ddi W dtd}|tj | 	  X dS )zFCancel all pending tasks on `loop`, wait for them, and close the loop.c             S   s   g | ]}|  s|qS r   )done).0r   r   r   r   
<listcomp>3   s    zshutdown.<locals>.<listcomp>Nreturn_exceptionsTzconcurrent.futures)
asyncioZ	all_tasksr   run_until_completegatherlogging	getLoggersetLevelCRITICALclose)r    Z	to_cancelr   Z	cf_loggerr   r   r   shutdown/   s    

r-   T)Z	patchable)sourcesfast
write_backmodereportworkersr   c             C   s   t   |dkrt pd}tjdkr.t|d}yt|d}W n$ ttt	fk
r`   t
dd}Y nX t }t| z |t| ||||||d W dzt| W dtd X |dk	r|  X dS )z4Reformat multiple files using a ProcessPoolExecutor.N   win32<   )max_workers)r.   r/   r0   r1   r2   r    executor)r   os	cpu_countsysplatformminr   r   NotImplementedErrorOSErrorr   r%   Znew_event_loopZset_event_loopr&   schedule_formattingr-   )r.   r/   r0   r1   r2   r3   r8   r    r   r   r   reformat_manyE   s4    



rA   r   r   )r.   r/   r0   r1   r2   r    r8   r   c                s  i }t jt jfkrJt}t|| \} }xt|D ]}	||	tj q4W | sRdS g }
g }dt jt jfkr|t	 }|
  fddt| D }| }y$tjt| tjt| W n tk
r   Y nX x|rtj|tjdI dH \}}x|D ]}||}	| r*|
| np| rJ||	t|  nP| rZtjntj}t jkst jkr|tjkr||	 ||	| qW qW |
rtj|
ddiI dH  |rt || dS )zRun formatting of `sources` in parallel using the provided `executor`.

    (Use ProcessPoolExecutors for actual parallelism.)

    `write_back`, `fast`, and `mode` options are passed to
    :func:`format_file_in_place`.
    Nc                s*   i | ]"}|t  t|qS r   )r%   Zensure_futureZrun_in_executorr   )r"   src)r8   r/   lockr    r1   r0   r   r   
<dictcomp>   s   z'schedule_formatting.<locals>.<dictcomp>)Zreturn_whenr$   T)!r   ZDIFFZ
COLOR_DIFFr   r   sortedr!   r   ZCACHEDr   LockkeysZadd_signal_handlersignalSIGINTr   SIGTERMr>   r%   waitZFIRST_COMPLETEDpop	cancelledappend	exceptionfailedstrresultZYESNOZCHECKr'   r   )r.   r/   r0   r1   r2   r    r8   cachecachedrB   rM   Zsources_to_cachemanagerr   pendingr!   _r   changedr   )r8   r/   rC   r    r1   r0   r   r@   w   sN    




r@   ),__doc__r%   r(   r9   rH   r;   concurrent.futuresr   r   r   multiprocessingr   Zpathlibr   typingr   r   r	   r
   Zmypy_extensionsr   Zblackr   r   Zblack.cacher   r   r   r   Z
black.moder   Zblack.outputr   Zblack.reportr   r   r   r   ZAbstractEventLoopr-   boolintrA   r@   r   r   r   r   <module>   sD   +