U e @srddlmZddlZddlZddlZddlZddlZddlZddl Z ddl m Z ddl m Z ddl m Z ddl mZddl mZddl mZdd l mZdd lmZdd lmZdd lmZed ZedZddddZd3dddddZddddZdddddZGdddeZd4dddd d!d"d#d$Zej dd%d&d'd(Z!d)d*ed+d,dd-ddd.d/d0d1d2Z"dS)5) annotationsN)Any)Callable) Generator)Iterable)MutableMapping)Sequence)TypeVar) parse_shebang) cmd_output_b) cmd_output_pTArgTRetint)returncCsLzttdWStk r$YnXz tWStk rFYdSXdS)Nr)lenossched_getaffinityAttributeErrormultiprocessing cpu_countNotImplementedErrorrr>/opt/hc_python/lib/python3.8/site-packages/pre_commit/xargs.pyrs rzMutableMapping[str, str] | None)_envrcCsT|dk r |n ttdtj}dt|}|D] \}}|t|t|d7}q.|S)Nenvironb)getattrrenvironritems)rr sizekvrrr _environ_size)s  r%cCsHtjdkr2tddt}tt|dd}|Stjdkr@dSdSdS)Nposix SC_ARG_MAXiiintix)rnamesysconfr%maxmin)maximumrrr_get_platform_max_length1s  r.str)cmdrcGs<d|}tjdkr&t|ddSt|tSdS)N win32zutf-16ler)joinsysplatformrencodegetfilesystemencoding)r0Zfull_cmdrrr_command_length=s  r8c@s eZdZdS)ArgumentTooLongErrorN)__name__ __module__ __qualname__rrrrr9Hsr9z Sequence[str]z int | Noneztuple[tuple[str, ...], ...])r0varargstarget_concurrency _max_lengthrc Cs|pt}tdtt||}t|}g}g}tt|}t|d}|r| }t|d} || |krt||kr| ||| 7}qJ|st |qJ| |t|g}t|d}| |qJ| |t|t|S)Nr) r.r+mathceilrtuplelistreversedr8popappendr9) r0r=r>r?Zmax_argsretZret_cmd total_lengthargZ arg_lengthrrr partitionLs0           rKzYGenerator[Callable[[Callable[[TArg], TRet], Iterable[TArg]], Iterable[TRet]], None, None])maxsizerc cs4|dkrtVn tj|}|jVW5QRXdS)Nr)map concurrentfuturesThreadPoolExecutor)rLexrrr_thread_mapperxsrRFr)colorr>r?tuple[str, ...]boolrztuple[int, bytes])r0r=rSr>r?kwargsrc s |rtntd}d}zt|}Wn8tjk rZ}z|ddWYSd}~XYnXtjdkr|d drt d} dt | t d d }t ||||} d d d fdd } t t | |} t| >} | | | }|D](\}}}t|t|kr|}||7}qW5QRX||fS)zA simplified implementation of xargs. color: Make a pty if on a platform that supports it target_concurrency: Target number of partitions to run concurrently rNrr2)z.batz.cmdzcmd.exei z /c irTztuple[int, bytes, bytes | None])run_cmdrcs|dtjdS)NF)checkstderr) subprocessSTDOUT)rXZcmd_fnrVrrrun_cmd_partitionsz xargs..run_cmd_partition)r r r Z normalize_cmdZExecutableNotFoundErrorZ to_outputr4r5lowerendswithfind_executablerrKr,rRabs)r0r=rSr>r?rVretcodestdouteZcmd_exeZ partitionsr^threadsZ thread_mapresultsZ proc_retcodeZproc_out_rr]rxargss. &   ri)N)N)# __future__rconcurrent.futuresrN contextlibrArrr[r4typingrrrrrrr Z pre_commitr Zpre_commit.utilr r r rrr%r.r8 RuntimeErrorr9rKcontextmanagerrRrirrrrs@             ,