o ]Lbh@sVdZddlmZddlZddlZddlZddlZddlZddlZddl Z ddl m Z ddl m Z mZddlmZddlmZmZmZmZm Z mZdd lmZmZmZd d Zgd Zd gZedejZ ddZ!ddZ"ddZ#Gddde$Z%ddZ&ddZ'Gddde$Z(gdZ)Gdddej*Z+d d!Z,d"d#Z-Gd$d%d%e$Z.d&d'Z/dS)(aRcommand server extension for cHg 'S' channel (read/write) propagate ui.system() request to client 'attachio' command attach client's stdio passed by sendmsg() 'chdir' command change current directory 'setenv' command replace os.environ completely 'setumask' command (DEPRECATED) 'setumask2' command set umask 'validate' command reload the config and check if the server is up to date Config ------ :: [chgserver] # how long (in seconds) should an idle chg server exit idletimeout = 3600 # whether to skip config or env change checks skiphash = False )absolute_importN)_)getattrsetattr)hex) commandserverencodingerror extensionspycompatutil)hashutilprocutil stringutilcCsttt|S)z return sha1 hexdigest for a list)rrsha1rpprintdigest)itemsr5/usr/lib/python3/dist-packages/mercurial/chgserver.py _hashlistKsr)saliass diff-toolsseolsextdiffs extensionss fastannotates merge-toolssschemes)scommandssshow.aliasprefixs~\A(?: CHGHG |HG(?:DEMANDIMPORT|EMITWARNINGS|MODULEPOLICY|PROF|RCPATH)? |HG(?:ENCODING|PLAIN).* |LANG(?:UAGE)? |LC_.* |LD_.* |PATH |PYTHON.* |TERM(?:INFO)? |TZ )\Zcsg}tD] }|||qtD] \}}||||qt|}dtjvr,dhntfddt tjD}tt |}|dd|ddS)areturn a quick hash for detecting config/env changes confighash is the hash of sensitive config items and environment variables. for chgserver, it is designed that once confighash changes, the server is not qualified to serve its client and should redirect the client to a new server. different from mtimehash, confighash change will not mark the server outdated and exit since the user can have different configs at the same time. sCHGHGsHGcs*g|]\}}t|r|vr||fqSr)_envrematch.0kvignoredrr sz_confighash..N) _configsectionsappend configitems_configsectionitemsconfigrr environsetr iteritemssorted)ui sectionitemssectionitem sectionhashenvitemsenvhashrrr _confighashrs      r2c Csddt|D}z ddlm}||Wn ty Ynwg}tjr,|tj|D]}z |tt |Wq.t yFYq.wt t |S)zget a list of paths that should be checked to detect change The list will include: - extensions (will not cover all files for complex extensions) - mercurial/__version__.py - python binary cSsg|]\}}|qSrr)rnmrrrr sz"_getmtimepaths..r) __version__) r r5r# ImportErrorr sysexecutablefsencodeinspect getabsfile TypeErrorr*r()r+modulesr5filesr4rrr_getmtimepathss"     r?cCs dd}tt||ddS)aWreturn a quick hash for detecting file changes mtimehash calls stat on given paths and calculate a hash based on size and mtime of each file. mtimehash does not read file content because reading is expensive. therefore it's not 100% reliable for detecting content changes. it's possible to return different hashes for same file contents. it's also possible to return a same hash for different file contents for some carefully crafted situation. for chgserver, it is designed that once mtimehash changes, the server is considered outdated immediately and should no longer provide service. mtimehash is not included in confighash because we only know the paths of extensions after importing them (there is imp.find_module but that faces race conditions). We need to calculate confighash without importing. cSs2zt|}|tj|jfWStyYdSwN)osstatST_MTIMEst_sizeOSError)pathstrrrtrystats   z_mtimehash..trystatN )rr maplist)pathsrHrrr _mtimehashsrLc@s&eZdZdZddZedddZdS) hashstatezCa structure storing confighash, mtimehash, paths used for mtimehashcC||_||_||_dSr@) confighash mtimehash mtimepaths)selfrOrPrQrrr__init__ zhashstate.__init__NcCs<|durt|}t|}t|}|dd||t|||S)Ns cmdserversconfighash = %s mtimehash = %s )r?r2rLlogrM)r+rQrOrPrrrfromuis zhashstate.fromuir@)__name__ __module__ __qualname____doc__rS staticmethodrVrrrrrMs rMcs"Gfddd|j|S)Ncs8eZdZdfdd ZddZdfdd ZZS) z_newchgui..chguiNcs0t|||rt|d|_dS|_dS)N_csystem)superrSrr\)rRsrc) __class__chguicsystemrrrSs z!_newchgui..chgui.__init__cSs`||just|jdr|jtjks|jr"tj||||dS|| |t ||S)Nsfileno)r'cwdout) foutr safehasattrfilenorstdout_finoutredirectedsystemflushr\ shellenviron)rRcmdr'rbrcrrr _runsystems  z#_newchgui..chgui._runsystemcs |j|t|ddiddS)Npagerattachio)typecmdtableT)r\rrk)rRrlenv)attachiorr _runpagersz"_newchgui..chgui._runpagerr@)rWrXrYrSrmrt __classcell__rrsr`rar_rr`sr`rw)srcuirarsrrvr _newchguis!rycCs&ddlm}|j}dD] }t||t||q t|dr#|j|_| ||}| ||d| D]!\}}} | ||} d| vsM| dksM| drNq5|||| | q5|d } | rctj| pdd} |d } |j|| | d \} }t|tj||d ||urt|tj||d ||fS) Nr)dispatch)finfoutferrsenvirons_csystemsconfig:s--config$scwds repository)wd)fp)r6rzr_loadrrr rer\_earlyparseopts _parseconfig walkconfig configsource startswith setconfigrArFrealpath _getlocalr populateuir setuplogging)rxargscdebugrznewuiaoptionsr-namevaluesourcerbrpathrFnewluirrr _loadnewuis.       rc@s"eZdZdZddZdddZdS) channeledsystemaPropagate ui.system() request in the following format: payload length (unsigned int), type, '', cmd, '', cwd, '', envkey, '=', val, '', ... envkey, '=', val if type == 'system', waits for: exitcode length (unsigned int), exitcode (int) if type == 'pager', repetitively waits for a command name ending with ' ' and executes it defined by cmdtable, or exits the loop if the command name is empty. cCrNr@)in_rcchannel)rRrrcrrrrrS=rTzchanneledsystem.__init__Nsystemc Cs||t|pdg}|ddt|Dd|}|jt d|j t ||j||j |dkr^|j d}td|\}|dkrQttd td |j d\} | S|d kr |j dd }|spdS|r|||vr|||n ttd|qctd|)N.css |] \}}d||fVqdS)s%s=%sNrrrrr Dsz+channeledsystem.__call__..>cIr>Isinvalid response>irnTsunexpected command: %ssinvalid S channel type: %s)r abspathextendr r)joinrcwritestructpackrlenrjrreadunpackr AbortrreadlineProgrammingError) rRrlr'rbrprqrdatalengthrcrrr__call__Bs.       zchanneledsystem.__call__)NrN)rWrXrYrZrSrrrrrr(sr))scinr{rb)scoutr|wb)scerrr}rc seZdZfddZfddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ fddZ ddZejjZeee e ee e deedr^ddZeed<ZSZS) chgcmdserverc sptt|t|t||d|j||||||_d|_g|_||_ ||_ |dur6|j |_ tj |j d<dSdS)NSFsvalidate)r]rrSryrrs clientsock _ioattached_oldiosrM baseaddress capabilitiescopyvalidate) rRr+repofinrdsock prereposetupsrMrrwrrrSgs  zchgcmdserver.__init__cs*tt||j|d|_dSNF)r]rcleanupr+rj _restoreiorrRrwrrrzs  zchgcmdserver.cleanupc Cs8|jtdddt|j}|jdd||j}| | t |t D]_\}\}}}|dks8Jt ||}t||t||jrNq+|dkrU|}n%tjrh|rbt|}nt|}n|rod} nd} t||| }||urt|||t|||q+d |_|jtd t|d S) z{Attach to client's stdio passed via unix domain socket; all channels except cresult will no longer be used rIr chgserversreceived fds: %r rr}rTrN)rsendallrrr recvfdsrfr+rUrj_saveiozip _iochannelsrrAdup2closerr ispy3isattyrmake_line_bufferedunwrap_line_bufferedfdopenrcresultrr) rR clientfdsr+fdcnfnmodernewfpbufsizerrrrss8      zchgcmdserver.attachiocCsX|jrdS|j}tD]\}}}t||}t||}t|}|j|||fq dSr@)rr+rrrAduprfr#)rRr+rr_modechrrrrrrs  zchgcmdserver._saveioc Cs|jsdSttjtj}|j}t|jtD]k\\}}}\}}}t||} t j s0| |us0| z!t j s8| |urHd|vrHt || |t || Wntyo} z|jddt| |WYd} ~ nd} ~ wwt |t|||t|||qt ||jdd=dS)Nwrsgot %s while duplicating %s )rrAopendevnullO_WRONLYr+rrrr rrrrfrjrErUr forcebytestrr) rRnullfdr+rrrrrrrerrrrrrs6    zchgcmdserver._restoreioc Cs|}d}d}z t|j||j\|_}WnTtjyE}z#|jtdt||j r9|jtd|j d}WYd}~n)d}~wtj yi}z|j durU|j }|j| d}WYd}~nd}~ww|r|j d}|jddr||}|jd |dSt||jj}g}|j|jjkrt|j|jj} |d | |jjr|d |j|jjkrt|j|j} |d | |jd dt||jd|pddS)aReload the config and check if the server is up to date Read a list of '' separated arguments. Write a non-empty list of '' separated instruction strings or '' if the list is empty. An instruction string could be either: - "unlink $path", the client should unlink the path to stop the outdated server. - "redirect $path", the client should attempt to connect to $path first. If it does not work, start a new server. It implies "reconnect". - "exit $n", the client should exit directly with code n. This may happen if we cannot parse the config. - "reconnect", the client should close the connection and reconnect. If neither "reconnect" nor "redirect" is included in the instruction list, the client can continue with this server after completing all the instructions. Fs abort: %s s(%s) TNsuisdetailed-exit-codesexit %ds unlink %ss reconnects redirect %srs validate: %s r) _readlistrr+rr RepoErrorrrrhintErrordetailed_exit_codeformatrj configboolrrrMrVrQrP _hashaddressrrOr#rUrr) rRr errorraisedrluiinst exit_codenewhashinstsaddrrrrrsJ   zchgcmdserver.validatecCs.|}|sdS|jdd|t|dS)zChange current directory Note that the behavior of --cwd option is bit different from this. It does not affect --config parameter. Nrschdir to '%s' )_readstrr+rUrAchdir)rRrFrrrr"s zchgcmdserver.chdircCs||ddS)zChange umask (DEPRECATED)rN) _setumask_readrrrrsetumask.szchgcmdserver.setumaskcCs*|}t|dkrtd||dS)z Change umaskrs(invalid mask length in setumask2 requestN)rr ValueErrorr)rRrrrr setumask24s zchgcmdserver.setumask2cCs.td|d}|jdd|t|dS)Nrrrs setumask %r )rrr+rUr r)rRrmaskrrrr;szchgcmdserver._setumaskcs<|j}g|_ztt|W|||_S|||_wr@)rr]r runcommandr)rR globaloldiosrwrrr@szchgcmdserver.runcommandcCsh|}z tdd|D}Wn tytdw|jddt|tj tj |dS)ztClear and update os.environ Note that not all variables can make an effect on the running process. css|] }|ddVqdS)=rN)split)rsrrrrSsz&chgcmdserver.setenv..s"unexpected value in setenv requestrs setenv: %r N) rdictrr+rUr*keysr r'clearupdate)rRlnewenvrrrsetenvLs  zchgcmdserver.setenv)roschdirs runcommandssetenvssetumasks setumask2s setprocnamecCs&|}|jdd|t|dS)zChange process titlerssetprocname: %r N)rr+rUr setprocname)rRrrrrr iszchgcmdserver.setprocname)rWrXrYrSrrsrrrrrrrrr rserverrrrr rerr rurrrwrrfs6  1 )<    rcCsd|tfS)Ns %s.%d.tmp)rAgetpid)addressrrr _tempaddressrsrcCs6tj|\}}|ddd}dtj|||fS)Nrrrs%s-%s)rArFrr)rhashstrdirnamebasenamerrrrvsrc@sleZdZdZdZddZddZddZd d Zd d Z d dZ ddZ ddZ ddZ ddZddZdS)chgunixservicehandlerz"Set of operations for chg servicesrcCs4||_d|_d|_d|_|dd|_t|_dS)Nrs idletimeout)r+ _hashstate _baseaddress _realaddress configint _idletimeouttime _lastactive)rRr+rrrrSs zchgunixservicehandler.__init__cCs(||||||dSr@)_inithashstate_checkextensions_bind_createsymlink)rRrrrrr bindsockets   z chgunixservicehandler.bindsocketcCsF||_|jddrd|_||_dSt|j|_t||jj|_dS)Nrsskiphash) rr+rrrrMrVrrOrRrrrrrsz$chgunixservicehandler._inithashstatecCs"|jsdStrd|j_dSdS)N)rr notloadedrPrrrrrs  z&chgunixservicehandler._checkextensionscCs@t|j}t||t||_|tj t ||jdSr@) rrr bindunixsocketrArB _socketstatlistensocket SOMAXCONNrename)rRr tempaddressrrrrs    zchgunixservicehandler._bindcCsB|j|jkrdSt|j}ttj|j|t||jdSr@) rrrrAsymlinkrFrr r()rRr)rrrrs  z$chgunixservicehandler._createsymlinkcCsHzt|j}|j|jjko|tj|jtjkWSty#YdSwr)rArBrst_inor$rCrE)rRrGrrr_issocketowners  z$chgunixservicehandler._issocketownercCs|sdSt|jdSr@)r,r tryunlinkrr rrr unlinksocketsz"chgunixservicehandler.unlinksocketcCsH|s|jdd|jdSt|j|jkr"|jdddSdS)Nrs%s is not owned, exiting. Tsbeing idle too long. exiting. F)r,r+rUrrrrrrrr shouldexitsz chgunixservicehandler.shouldexitcCst|_dSr@)rrrrrr newconnectionsz#chgunixservicehandler.newconnectionc Cst|j||||||j|jSr@)rr+rr)rRrconnrrdrrrrcreatecmdserversz%chgunixservicehandler.createcmdserverN)rWrXrYrZ pollintervalrSrrrrrr,r.r/r0r2rrrrrs       rcCsdtjvr tjd=dtjvrtjdtjd<tjd=ndtjvr-dtjvr)tjd=tjd=|r7|ddddt|}tj|d||d S) NsCHGINTERNALMARKsCHGORIG_LC_CTYPEsLC_CTYPEsCHG_CLEAR_LC_CTYPEsbundles mainreporootr!srepo)roptshandler)r r'rrrunixforkingservice)r+rr4hrrrchgunixservices     r8)0rZ __future__rr:rArer&rBrri18nrr rrnoderr6rr r r r utilsrrrrr"r%compileXrr2r?rLobjectrMryrrrr rrrrr8rrrrsJ "      %$6 j