o ]Lbl@s2ddlmZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl m Z ddl mZmZddlmZmZmZm Z ddlmZedZe jrYdd lmZndd lmZd d ZGd ddejZGdddeZ ej!"e ddZ#ddZ$GdddeZ%ej&"e%ddZ'e j(rej)dureZ)nej)j*Z)ej+dureZ+ne'ej+j*Z+ej,dureZ,ne'ej,j*Z,e jre-e+Z+e-e,Z,ee+re#e+Z+n,ej)Z)ej+Z+ej,Z,e jre-e+Z+e-e,Z,ee+re jre#e+Z+n e.e+/ddZ+ej0Z0ej1Z2ej3Z3ej4Z4ej5Z5ej6Z6ej7Z7ej8Z8ej9Z9ej:Z:ej;Z;ejZ>Wn e?y:Ynwzej@Z@Wn e?yJYnwe jAZBddZCGdddeZDd^d!d"ZEd#d$ZFd%d&ZGd_d'd(ZHd_d)d*ZId`d+d,ZJd-d.ZKd/d0ZLeLeKd1ZMd2d3ZNdaOd4d5ZPd6d7ZQd8d9ZRd:d;ZSdd?ZUd@dAZVd_dBdCZWe jrdDdEZXejYZZndFdEZXe j[ZZdGdHZ\dadIdJZ]da^dKdLZ_dMdNZ`dOdPZadQdRZbejcdSdTZde jrdUZeeeejfBZg V   W  dbdXdYZhdS V   W  dbdZd[Zi V   W  dbd\d]Zje j(reiZhdSejZhdS)c)absolute_importN_)getattropen)encodingerrorpolicypycompat) resourceutilosutil)windows)posixcCs z|WStyYdSw)NF)isattyAttributeError)fpr:/usr/lib/python3/dist-packages/mercurial/utils/procutil.pyr.s   rc@s eZdZdZddZddZdS)BadFilez3Dummy file object to simulate closed stdio behaviorcC ttjdNzBad file descriptorIOErrorerrnoEBADFselfbrrrreadinto8 zBadFile.readintocCrrrrrrrwrite;r z BadFile.writeN)__name__ __module__ __qualname____doc__rr!rrrrr5s rc@$eZdZddZddZddZdS)LineBufferedWrappercC ||_dSNorigrr+rrr__init__@ zLineBufferedWrapper.__init__cC t|j|Sr)rr+rattrrrr __getattr__Cr zLineBufferedWrapper.__getattr__cCs&|j}||}|dr||S)N )r+r!endswithflush)rsr+resrrrr!Fs   zLineBufferedWrapper.writeNr"r#r$r-r3r!rrrrr'? r'cCs,tjr t|tjs |St|tr|St|Sr))r ispy3 isinstanceioBufferedIOBaser'streamrrrmake_line_bufferedSs  rAcCs$t|trt|jtr J|jS|Sr))r<r'r+r?rrrunwrap_line_buffered_s rBc@r&)WriteAllWrappercCr(r)r*r,rrrr-gr.zWriteAllWrapper.__init__cCr/r)r0r1rrrr3jr zWriteAllWrapper.__getattr__cCsD|jj}t|}t|}d}||kr ||||d7}||ks|SNr)r+r! memoryviewlen)rr7write1mtotal_to_write total_writtenrrrr!mszWriteAllWrapper.writeNr9rrrrrCfr:rCcCs0tjsJt|tr |St|tjr|St|Sr))r r;r<rCr=r>r?rrr_make_write_all|s   rKwbcCs"|dkr td|Std| S)zpreturn a message describing a subprocess status (codes from kill are negative - not os.system/wait encoding)rsexited with status %dskilled by signal %dr)coderrr explainexits rOc@s@eZdZdZddZddZddZdd Zd d Zd d Z dS)_pfilez;File-like wrapper for a stream opened by subprocess.Popen()cCs||_||_dSr))_proc_fp)rprocrrrrr-s z_pfile.__init__cCs|j|jSr))rRcloserQwaitrrrrrTs  z _pfile.closecCs t|jSr))iterrRrVrrr__iter__r.z_pfile.__iter__cCr/r))rrRr1rrrr3r z_pfile.__getattr__cC|Sr)rrVrrr __enter__z_pfile.__enter__cCs |dSr))rT)rexc_type exc_valueexc_tbrrr__exit__r z_pfile.__exit__N) r"r#r$r%r-rTrXr3rZr_rrrrrPs rPrbcCs2|dkr t||S|dkrt||Std|)Nr`swbsunsupported mode: %r) _popenreader _popenwriterr ProgrammingError)cmdmodebufsizerrrpopens   rhcC&tjt|d|ttjd}t||jS)NT)shellrg close_fdsstdout) subprocessPopen tonativestrclosefdsPIPErPrlrergprrrrb rbcCri)NT)rjrgrkstdin)rmrnrorprqrPrurrrrrrcrtrcc Cs0tjt|ddttjtjt|d}|j|jfS)NTra)rjrgrkrurlenv)rmrnrorprq tonativeenvrurl)rervrsrrrpopen2s rxcCst||\}}}}|||fSr))popen4)rervrurlstderrrsrrrpopen3!s r{c Cs:tjt|d|ttjtjtjt|d}|j|j|j|fS)NT)rjrgrkrurlrzrv) rmrnrorprqrwrurlrz)rervrgrsrrrry&s rycCs.tjt|dttjtjd}||\}}|S)z9filter string S through command CMD, returning its outputT)rjrkrurl)rmrnrorprq communicate)r7rerspoutperrrrr pipefilter4srcCsd\}}ztjdd\}}t|d}|||tjdd\}}t||d|}|d|}t|}tjdkrD|d @rDd }|rSt t d |t |ft |d 4}|Wd Wz |rlt|Wn tyvYnwz |rt|WSWStyYSw1swYWz |rt|Wn tyYnwz |rt|Wd SWd StyYd Swz |rt|Wn tyYnwz |rt|WwWwtyYww)zfilter string S through a pair of temporary files with CMD. CMD is used as a template to create the real command to be run, with the strings INFILE and OUTFILE replaced by the real names of the temporary files generated.)NNs hg-filter-in-)prefixrLshg-filter-out-sINFILEsOUTFILEOpenVMSrMrscommand '%s' failed: %sr`N)r mkstemposfdopenr!rTreplacesystem sysplatformr AbortrrOrreadunlinkOSError)r7reinnameoutnameinfdroutfdrNrrr tempfilterAs~               r)s tempfile:spipe:cCsFttD]\}}||r|||t|dSqt||S)zMfilter a string through a command that transforms its input to its outputN)r iteritems _filtertable startswithrFlstripr)r7renamefnrrrfilterls   rcCstdur[tjd}tjd}|rt|tStr3t tdddkr,ttjdtStt j tSt j sLt jt |ddd krLtt |jtSttd pYt jt jd tS) z]return location of the 'hg' executable. Defaults to $HG or 'hg' in the search path. NHG__main__frozen macosx_appEXECUTABLEPATH__file__hgshgr) _hgexecutablerenvirongetsysmodules_sethgexecutabler mainfrozenrr sysexecutable iswindowsrpathbasenamefsencoderfindexesysargv)rmainmodrrr hgexecutablexs*    rcCs|adS)z#set location of the 'hg' executableN)r)rrrrrsrcCs8t|dd}z |o||kWStjyYdSw)NfilenoF)rrr=UnsupportedOperation)fstdfrrrr _testfilenos rcC t|tjSr))rr __stdin__rrrrisstdinr rcCrr))rr __stdout__rrrrisstdoutr rcCs|||}}t|tr0t|}ttjtj}t ||t |t |d}t|t rLt|}t t |t |d}||fS)aDuplicate streams and redirect original if (uin, uout) are stdio If uin is stdin, it's redirected to /dev/null. If uout is stdout, it's redirected to stderr so the output is still readable. Returns (fin, fout) which point to the original (uin, uout) fds, but may be copy of (uin, uout). The returned streams can be considered "owned" in that print(), exec(), etc. never reach to them. rbrL)r6rrurduprrdevnullO_RDONLYdup2rTrrlrz)uinuoutfinfoutnewfdnullfdrrr protectstdios      rcCsJ|||f||ffD]\}}||ur"t|||q dS)z@Restore (uin, uout) streams from possibly duplicated (fin, fout)N)r6rrrrT)rrrrruifrrr restorestdiosrcsBddttj}|r|fddt|Dt|d<|S)z>return environ with optional override, useful for shelling outcSs*|dus|dur dS|durdSt|S)z9convert python object into string that is useful to shellNF0T1)r bytestr)valrrrpy2shells  zshellenviron..py2shellc3s |] \}}||fVqdSr)r).0kvrrr szshellenviron..r)dictrrupdater rr)rrvrrr shellenvirons   rcCst|t|Sr))platform shelltocmdexerrervrrr shelltonativesrcCrYr)rrrrrrr[cCs tt|S)zHconvert the environment from bytes to strings suitable for Popen(), etc.)r rapplyro)rvrrrrws rwc CsztWn tyYnwt|}|dust|r/tjt|dtt |t t|d}n,tj t|dtt |t t|tj tjd}t|jjdD]}||qL||j}t jdkrf|d@rfd}|S) zenhanced shell command execution. run with environment maybe modified, maybe in different dir. if out is specified, it is assumed to be a file-like object that has a write() method. stdout and stderr will be redirected to out.NT)rjrkrvcwd)rjrkrvrrlrzrrMr)rlr6 Exceptionrrrmcallrorprwr rrnrqSTDOUTrWreadliner!rU returncoder)rerroutrvrcrSlinerrrrs<     rcCsJtjrdtjvr dSttddrtSdStjp$tjdp$tjdS)zAre we running in a GUI?sSSH_CONNECTIONFisguiNTsDISPLAYsWAYLAND_DISPLAY) r isdarwinrrrr rrrrrrr_guis    rcCstdurtatSr))_is_guirrrrrgui0srcCs2trttdddkrtjdgStjgStS)zReturn the command used to execute current hg This is different from hgexecutable() because on Windows we want to avoid things opening new shell windows like batch files, so we get either the python call or current executable. rNrr) r rrrrrr r _gethgcmdrrrrhgcmd7s  rc stfdd}d}ttdd}|durt||}z9t|}|sF|vs+t|s>|s>W|dur.handlerNSIGCHLDrag?)setrsignal spawndetachedtestpidrtimesleep)argscondfnr prevhandlerrpidrrr rundetachedGs,     rc #sttjggfdd}z+z ttj|Wn ty(d=YnwdVWr8ttjdr.disabledsiginthandlerrN)r getsignalr ValueErrorKeyboardInterrupt)rrrrruninterruptiblels(   rFTc CszAd}|durt}||||dtjtt||t |dt |||d} |dur5|| j W|dur@| dSdS|durK| ww)z1Spawn a command without waiting for it to finish.NrT)rjrvrk creationflagsrurlrz) r unnamedtempfiler!r6seekrmrnrrorw_creationflagsrUrT) scriptrvrjrlrz ensurestart record_wait stdin_bytesrursrrr runbgcommands2      rc stjrt|r |}nt|tr|g}ddd|D}|dur(d|}d} nd} d}d} z]z7|dur8tj} nt} | || | d|durQtj}|durXtj}tj |d|d| ||| d Wnt yv|duru|d wW|dur| durt| trJ| n|dur| durt| trJ| www|stjfd d d } d| _| dSj} |dur|| dSdS)Spawn a command without waiting for it to finish. When `record_wait` is not None, the spawned process will not be fully detached and the `record_wait` argument will be called with a the `Subprocess.wait` function for the spawned process. This is mostly useful for developers that need to make sure the spawned process finished before a certain point. (eg: writing test) css|]}t|VqdSr)) shellquote)rxrrrrsz"runbgcommandpy3..Ns( %s ) &TFr)rjrvrkrurlrzstart_new_sessioncsjSr))rUrrsrrsz!runbgcommandpy3..target)r rrr<bytesjoinrmDEVNULLr r!r6r rnrintrT threadingThreaddaemonstartrU) rervrjrlrzrrrrrrutrrrrrunbgcommandpy3sp       r&c stjrt|durYtrY|s'fdd}tj|d} d| _| dSt d\} } t | r:t | } nt |  } | dkrW| dkrKt j} t| d|t| fdSd} d} zzR|durgt|durrttjd } nt} | || | d|durttjd }|durttjd }tj|||d| ||d }|dur||jd} Wn'ty}z|j d@} | dkrd} WYd}~nd}~wtyd} YnwW| dur| |durt| dSdS| dur| |durt| ww) rNcstddSrD)rwaitpidrrrr_do_wait:sz!runbgcommandpy2.._do_waitrTrrserror running %r: %srw)rjrvrkrurlrz)r rrrforkr!r"r#r$r' WIFEXITED WEXITSTATUSWTERMSIGrEINVALrstrerrorsetsidrrr r!r6r rmrnrUEnvironmentErrorrrT_exit)rervrjrlrzrrrr)r%_pidstatusrrursexrr(rrunbgcommandpy2s               r8)r`rar))Nra)NNN)FNNTNN)k __future__r contextlibrr=rrrmrr!ri18nrr rrrrr r utilsr importmodr rrrrr RawIOBaserobjectr'r>registerrArBrCIOBaserKr;rubufferrlrz winstdoutrrrgethgcmdrgetusergetpid hidewindowreadpipe setbinarysetsignalhandlerr shellsplitrsshargsr setprocnamer unblocksignalisposixrprOrPrhrbrcrxr{ryrrrrrrrrrrrrrr strfromlocalroidentityrwrrrrrrcontextmanagerrDETACHED_PROCESSCREATE_NEW_PROCESS_GROUPr rr&r8rrrrs                          &   &% !, W r