o ]Lb\b@slddlmZddlZddlZddlZddlZddlZddlZddlZddl Z z ddl Z e j Wne y>ddl mZ YnwddlmZddlmZddlmZmZmZmZmZmZmZddlmZmZGd d d eZGd d d eZ Gd ddeZ!dddiZ"ddZ#GdddeZ$d"ddZ%GdddeZ&ddZ'ddZ(GdddeZ)Gd d!d!eZ*dS)#)absolute_importN) selectors2)_)getattr)encodingerror loggingutilpycompat repocacheutilvfs)cborutilprocutilc@s4eZdZdZddZeddZddZdd Zd S) channeledoutputz^ Write data to out in the following format: data length (unsigned int), data cC||_||_dSN)outchannel)selfrrr9/usr/lib/python3/dist-packages/mercurial/commandserver.py__init__3 zchanneledoutput.__init__cC d|jSNs <%c-channel>rrrrrname7 zchanneledoutput.namecCs6|sdS|jtd|jt|||jdS)N>cI)rwritestructpackrlenflush)rdatarrrr!;s zchanneledoutput.writecC|dvrt|t|j|SN)isattyfilenotellseek)AttributeErrorrrrattrrrr __getattr__B zchanneledoutput.__getattr__N) __name__ __module__ __qualname____doc__rpropertyrr!r0rrrrr+s  rc@s,eZdZdZdZddZddZddZd S) channeledmessagea  Write encoded message and metadata to out in the following format: data length (unsigned int), encoded message and metadata, as a flat key-value dict. Each message should have 'type' attribute. Messages of unknown type should be ignored. TcCst|||_||_||_dSr)r_coutr _encodefn)rrr encodenameencodefnrrrrVs  zchanneledmessage.__init__cKs0t|}|dur ||d<|j||dS)Nsdata)r byteskwargsr8r!r9)rr&optsrrrr![s zchanneledmessage.writecCs t|j|Sr)rr8r.rrrr0as zchanneledmessage.__getattr__N)r2r3r4r5 structuredrr!r0rrrrr7Hs   r7c@s`eZdZdZdZddZeddZddd Zd d Z dd d Z ddZ ddZ e Z ddZdS)channeledinputa/ Read data from in_. Requests for input are written to out in the following format: channel identifier - 'I' for plain input, 'L' line based (1 byte) how many bytes to send at most (unsigned int), The client replies with: data length (unsigned int), 0 meaning EOF data icCs||_||_||_dSr)in_rr)rr@rrrrrrts zchanneledinput.__init__cCrrrrrrrryrzchanneledinput.namecCsP|dkr!|j}|||j}|}|r|||j}||7}|s|S|||jSNr) maxchunksize_readrrsizesbufrrrread}szchanneledinput.readcCsd|sdS|dks J|jtd|||j|jd}td|d}|s,dS|j|S)Nrr >I)rr!r"r#r%r@rIunpack)rrFrlengthrrrrDs    zchanneledinput._readcCs^|dkr)|j}||d}|}|r'|ds'||d}||7}|r'|dr|S||dS)NrL )rCrDendswithrErrrreadlines   zchanneledinput.readlinecCs|Srrrrrr__iter__zchanneledinput.__iter__cCs|}|st|Sr)rR StopIteration)rlrrrnextszchanneledinput.nextcCr'r()r-rr@r.rrrr0r1zchanneledinput.__getattr__N)rA)r2r3r4r5rCrr6rrIrDrRrSrW__next__r0rrrrr?es     r?scborcCsdt|SNrJ)joinr streamencode)vrrrsr]cCsD|dd}|D]}t|}|r||fSqtdd|)N cmdserversmessage-encodingss"no supported message encodings: %s ) configlist_messageencodersgetrAbortrZ)uiencnamesnfrrr_selectmessageencoders    rhc@sleZdZdZdddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ e e dZ ddZdS)serverzm Listens for commands on fin, runs them and writes the output on a channel based stream to fout. NcCst|_|r|j|_||_|j|_n ||_d|_|_||_t|d|_ t|d|_ t|d|_ t ||d|_ t|d|_|jdddkrV|j|_t|jd|j d d|_|d d d krot|\}}t|d |||_||_|dd|_d|_|jsttjtj|_dSdS)NdeoIrr^log-)repofpuismessage-outputschannelmsshutdown-on-interrupt)rgetcwdcwdbaseuirdrqrepoui_prereposetupsrcdebugcerrcoutr?cincresultconfigcopy setuploggingcmsgrhr7client configbool_shutdown_on_interrupt_old_inthandlersignalSIGINTSIG_IGN)rrdrqfinfout prereposetupsencnameencfnrrrrs8         zserver.__init__cCs|js ttj|jdSdS)z9release and restore resources taken during server sessionN)rrrrrrrrcleanupszserver.cleanupcCs |sdS|j|}|st|SrY)rrIEOFError)rrFr&rrrrDs  z server._readcCs(td|dd}|sdS||S)z[read a string from the channel format: data length (uint32), data rLrKrrJ)r"rMrD)rrNrrr_readstrs zserver._readstrcCs|}|r |dSgS)z6read a list of NULL separated strings from the channel)rsplit)rrGrrr _readlists zserver._readlistc Csddlm}|jr||Sz3zttj|j||WWttjtjStjy/t y7YnwWttjtjn ttjtjw|j t ddS)Nrdispatchs interrupted! rA) rrrrrrrSignalInterruptKeyboardInterruptrdr)rreqrrrr_dispatchcommand$s     $zserver._dispatchcommandc Cs*ddlm}|}|j}|g}|jr7||j_|j|j}|j|_| |||j_|jj _ |j |D]}| t|jdsN|ddddq9|j|dd||j|j|j|j|j|jd }z"||d @}|jtd t|Wd |vrt|jdSdSd |vrt|jww) zireads a list of terminated arguments, executes and writes the return code to the result channelrrsfilenorssnonttystrues commandserverNrs>is--cwd) rrrrdrrqrwrx __class__appenddirstate_ui invalidateall resetstater safehasattrr} setconfigrequestr|r{rryrr~r!r"r#intoschdirrv) rrargscopieduiuisrxrdrretrrr runcommand>sD       zserver.runcommandcCs|jtjdS)z1writes the current encoding to the result channelN)r~r!rrrrr getencodingpszserver.getencodingcCsP|jdd}|r$|j|}|r|||dkSttd||dkS)NrAsunknown command %srJ)rrR capabilitiesrbrrcr)rcmdhandlerrrrserveonets zserver.serveone)s runcommands getencodingcCsddt|j}|d7}|dtj7}|d7}|jr$|d|jj7}|dt7}tt dr?|d7}|dt d 7}|j |z| rR | sJWd SWd Sty^Yd Sw) Nscapabilities: r_rPs encoding: smessage-encoding: %s spid: %dsgetpgidspgid: %drr)rZsortedrrrrgetpidr rrgetpgidr|r!rr)rhellomsgrrrserves*    z server.server)r2r3r4r5rrrDrrrrrrrrrrrrris .  2 ric Cs|dd}|s dSt|dd}|dkr|rt||}n7|dkr+t|j|}n+tt|}| dd}| dd}t t j|}tj|t j||||d}|h} |rg| |j| |j| D]} | d|qidS) zSet up server logging facility If cmdserver.log is '-', log messages will be sent to the given fp. It should be the 'd' channel while a client is connected, and otherwise is the stderr of the server process. r^roNs track-logrps max-log-filess max-log-size)maxfilesmaxsize)rsetr`r fileobjectloggerferrr abspath expandpath configint configbytesvfsmodr rpathdirname fileloggerbasenameaddrwrd setlogger) rdrqrrlogpathtrackedloggerrrr targetuisurrrrs4       rc@s$eZdZddZddZddZdS) pipeservicecCrr)rdrq)rrdrqr=rrrrrzpipeservice.__init__cCsdSrrrrrrinitrTzpipeservice.initc Csd|j}|!\}}t||j||}z|W|WdS|w1s+wYdSr)rdprotectedfinoutrirqrr)rrdrrsvrrrruns zpipeservice.runN)r2r3r4rrrrrrrrs rcCstddtdSrB)rsetpgidrandomseedrrrr_initworkerprocesss rc Cs|d}|d}d}zzZ||||||}zJz|Wn=tjy:}z|td|jWYd}~n%d}~wtyR}z |jtjkrHWYd}~n d}~wt yZYnwW| n| wWn|rq|j } nt |d} | ttW|z|WdSty}z|jtjkrWYd}~dSd}~ww|z|Wwty}z |jtjkrWYd}~wd}~ww)Nrbwbs abort: %s rk)makefilerrrcrmessageIOErrorerrnoEPIPErrr{rr!r strtolocal traceback format_excclose) rdrqconncreatecmdserverrrrrinstr{rrr _serverequestsZ             rc@sDeZdZdZdZddZddZddZd d Zd d Z d dZ dS)unixservicehandlerzSet of pluggable operations for unix-mode services Almost all methods except for createcmdserver() are called in the main process. You can't pass mutable resource back from createcmdserver(). NcCs ||_dSr)rd)rrdrrrrs zunixservicehandler.__init__cCs:t|||tj|jtd||jdS)Nslistening at %s ) r bindunixsocketlistensocket SOMAXCONNrdstatusrr%)rsockaddressrrr bindsockets  zunixservicehandler.bindsocketcCst|dSr)runlink)rrrrr unlinksocket#szunixservicehandler.unlinksocketcCdS)z9True if server should shut down; checked per pollintervalFrrrrr shouldexit&szunixservicehandler.shouldexitcCr)z/Called when main process notices new connectionNrrrrr newconnection*sz unixservicehandler.newconnectioncCst|j||||S)zhCreate new command server instance; called in the process that serves for the current connection)rird)rrqrrrrrrrr-sz"unixservicehandler.createcmdserver) r2r3r4r5 pollintervalrrrrrrrrrrrs rc@sreZdZdZdddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZddZdS)unixforkingservicezG Listens on unix domain socket and forks server per connection NcCs||_||_|d|_ttdsttd|js"ttd|p't ||_ d|_ d|_ d|_ d|_t|_d|_|dd}|dkrMttdt|||_tjr]tdSdS) NsaddresssAF_UNIXsunsupported platforms'no socket path specified with --addressr^smax-repo-cachers(negative max-repo-cache size not allowed)rdrqrr rrrrcrr_servicehandler_sock_mainipc _workeripc_oldsigchldhandlerr _workerpids_socketunlinkedrr repoloader _repoloaderr isdarwinrgui)rrdrqr=rmaxlenrrrr8s*    zunixforkingservice.__init__cCs~ttj|_ttjtj}|\|_|_|j|j|j t t dr*t tjttj|j}||_d|_|jdS)Ns unblocksignalF)rAF_UNIXr socketpair SOCK_DGRAMrrrrrr rr unblocksignalrSIGCHLD_sigchldhandlerrrrstart)rorrrrQs   zunixforkingservice.initcCs"|js|j|jd|_dSdS)NT)rrrrrrrr _unlinksocket`s z unixforkingservice._unlinksocketcCsNttj|j|j|j|j||j | ddSrB) rrrrrrrr rstop _reapworkersrrrr_cleanupes    zunixforkingservice._cleanupcCs"z |W|dS|wr) _mainloopr rrrrros zunixforkingservice.runc Csd}|j}t}||jtj|j||jtj|j |s*| r*| d}z |j |j d}Wnt yN}z|jtjkrBg}WYd}~nd}~ww|sU|rTnq|D] \}}||j|qWq|dS)NFT)timeout)r selectorsDefaultSelectorregisterr EVENT_READ_acceptnewconnectionr_handlemainipcrr selectrOSErrorr ETIMEDOUTr&fileobjr)rexitinghselectoreventsrkey_maskrrrrus:      zunixforkingservice._mainloopc Cs|j}z|\}}Wntjy(}z|jdtjkr#WYd}~dSd}~wwt}|rNz|j dd||j || W|dS|wz#|||j||||jtdWdSz|j jddWtdYdStdw)Nrr^sforked worker process (pid=%d) T)forcer)racceptrrrrEINTRrforkrdlogrrrrr _runworkerr_exitr)rrrrr_addrrpidrrrrs<      z'unixforkingservice._acceptnewconnectionc CsZz|d}Wntjy$}z|jdtjkrWYd}~dSd}~ww|j|dS)z#Process messages sent from a workerirN)recvrrrrr"rload)rrrrrrrrrsz!unixforkingservice._handlemainipccCs|tjdSr)r rWNOHANG)rrframerrrrsz"unixforkingservice._sigchldhandlerc Cs|jrRz td|\}}Wn+ty8}z|jtjkr!WYd}~q|jtjkr(|jWYd}~dSd}~ww|dkr?dS|j dd||j ||jsdSdS)NrArr^sworker process exited (pid=%d) ) rrwaitpidrrr"ECHILDclearrdr$discard)roptionsr(_statusrrrrr s"     zunixforkingservice._reapworkerscCsRttj|jt|j}zt|j|j||j|j gdWt dSt w)Nr) rrrrrrrdrqr _reposetupgccollect)rrrrrrr%szunixforkingservice._runworkercsl|sdSGfddd|j|_|j|_|j|j}|dur%dS|jdd|jt ||dS)NcseZdZfddZZS)z8unixforkingservice._reposetup..unixcmdserverrepocsHt|z |j|jWdStjy#|jddYdSw)Nr^s#failed to send repo root to master ) superr _cmdserveripcsendrootrrrdr$r)runixcmdserverreporrrs z>unixforkingservice._reposetup..unixcmdserverrepo.close)r2r3r4r __classcell__rr:)rrr:sr:s repocachesrepo from cache: %s ) localrrr7rrbr9rdr$r copycache)rrdrq cachedreporr<rr3s zunixforkingservice._reposetupr)r2r3r4r5rrr r rrrrrr r%r3rrrrr3s  %&  r)NN)+ __future__rrr4rrrrr"rr BaseSelector ImportError thirdpartyri18nrr rrrrr r r r rutilsrrobjectrr7r?rarhrirrrrrrrrrrsB     $ X  R)%"