o ]Lb|@s dZddlmZmZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZddlZddlZddlZddlZddlZddlZddlmZddlmZddlmZmZmZm Z ddlmZddl!m"Z"dd l#m$Z$m%Z%m&Z&m'Z'mZm(Z(dd l)m*Z*m+Z+m,Z,m-Z-ej.rdd l/m0Z0m1Z1m2Z2m3Z3e'4d Z5e'4d Z6e5j7Z7e5j8Z8ej9Z9ej:Z:ej;Z;ejZ>e>Z?ej@Z@e(jAZAe(jBZBe(jCZCejDZEejFrddl#mGZnddl#mHZe&jIZIejJZJejKZKejLZLejMZMejNZNejOZOejPZPejQZQejRZRejSZSejTZTejUZUejVZVejWZWe6jXZXejYZYejZZZej[Z[ej\Z\ej]Z]ej^Z^ej_Z_ej`Z`ejaZaejbZbejcZcejdZdejeZeejfZfejgZgejhZhejiZiejjZjejkZkejlZlejmZmejnZnee6dejoZoejpZpejqZqejrarejsZsejtZtddZue*jvZve*jwZwe*jxZxze6jyZyWn ezyYnwe{Z|ddZ}e~e$jdZereddededdededdederejreddededdededdedddd Ze je+je jd!Zgd"ZeD] ZeevsJqGd#d$d$e{ZGd%d&d&e{ZzeZWneydd'd(ZYnwd)ZGd*d+d+e{Zdd,d-ZGd.d/d/e{ZGd0d1d1eZhd2ZGd3d4d4e{ZGd5d6d6e{ZGd7d8d8eZ 9dd;d<ZGd=d>d>eZ 9 9 9 : 9dd?d@ZdAdBZddDdEZdFdGZGdHdIdIe{ZGdJdKdKejZGdLdMdMeeZGdNdOdOeeZGdPdQdQe{ZejddRdSZejddTdUZGdVdWdWe{ZGdXdYdYe{ZdZd[ZGd\d]d]e{Zd^d_ZddbdcZdddeZdfdgZdhdiZejrdjdkZdldmZddndoZhdpZ : : :   9ddqdrZddsdtZhduZdvZdwdxZeedydZejFr)eZes(ejZn ejZes2ejZdzd{Zd|d}Zd~dZddZddkazddlZdaWn eŐyZd:aYnwGddde{ZeƃZiZddZddZddZddZːdddZGddde{ZGddde{ZΐdddZddZАdddZddZddZddZGddde{ZՐd ddZGddde{ZddZddZeddeIdfddeIdfddeIdfddeIdfddeIdfddeIdfdd`eIdfdd`eIdfdd`eIdfddeIdf ZGddde{ZedZddZddZddZejdkr-eZeZeZn ejZejZejZedkrHejdkrHddńZnddńZddȄZddʄZddd̄ZeddeId΃fddeIdσfddeIdЃfddeIdуfddeIdӃfddeIdԃfddeIdՃfddeId׃fddeId؃fddeIdكfddeIdۃfddeId܃fddeId݃f ZejGdd߄de{ZejddZde_ddZdZddZGddde{Zd ddZdde,je,jddfddZeZdddZddZddZddZejddZddZdS( zMercurial utility functions and platform specific implementations. This contains helper routines that are independent of the SCM core and hide platform-specific details from the core. )absolute_importprint_functionN)hex)attr)delattrgetattropensetattr)tracing)encodingerrori18npolicypycompat urllibcompat) compressionhashutilprocutil stringutil)IteratorListOptionalTuplebase85osutil)windows)posix statfilescCs&tjrdSt||d@t_adS)z%updates the umask. used by chg serverNi)r iswindowsosumaskplatform)valr$0/usr/lib/python3/dist-packages/mercurial/util.pysetumasks r&cCsd}|D]}||O}q|SNrr$) containerbitsbitr$r$r%bitsfroms r+sHGEMITWARNINGSdefault mercurialhgexthgext3rdignorez bad escapezinvalid escape sequencezthe imp module is deprecatedcCs:tr|d|7}tt|t|dtjdSdS)zxIssue an python native deprecation warning This is a noop outside of tests, use 'ui.deprecwarn' when possible. sF (compatibility will be dropped after Mercurial-%s, update your code.)rN) _dowarnwarningswarnrsysstrDeprecationWarningsysstderrflush)msgversion stacklevelr$r$r%nouideprecwarnsr=)md5sha1sha512)r@r?r>c@s>eZdZdZdddZddZddZd d Zed d Z d S)digesterahelper to compute digests. This helper can be used to compute one or more digests given their name. >>> d = digester([b'md5', b'sha1']) >>> d.update(b'foo') >>> [k for k in sorted(d)] ['md5', 'sha1'] >>> d[b'md5'] 'acbd18db4cc2f85cedef654fccc4a4d8' >>> d[b'sha1'] '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33' >>> digester.preferred([b'md5', b'sha1']) 'sha1' cCsPi|_|D]}|tvrttd|t||j|<q|r&||dSdSNsunknown digest type: %s)_hashesDIGESTSr Abort_update)selfdigestsskr$r$r%__init__szdigester.__init__cCs|jD]}||qdSN)rDvaluesrH)rIdatahr$r$r%rHs zdigester.updatecCs,|tvr ttdtt|j|SrC)rEr rFrGrLrrDdigest)rIkeyr$r$r% __getitem__ szdigester.__getitem__cC t|jSrN)iterrDrIr$r$r%__iter__ zdigester.__iter__cCstD] }||vr |SqdS)z@returns the strongest digest type in both supported and DIGESTS.N)DIGESTS_BY_STRENGTH) supportedrLr$r$r% preferreds zdigester.preferredN)rB) __name__ __module__ __qualname____doc__rMrHrTrX staticmethodr\r$r$r$r%rAs  rAc@*eZdZdZddZd ddZddZd S) digestcheckerzfile handle wrapper that additionally checks content against a given size and digests. d = digestchecker(fh, size, {'md5': '...'}) When multiple digests are given, all of them are validated. cCs0||_||_d|_t||_t|j|_dSr')_fh_size_gotdict_digestsrAkeys _digester)rIfhsizerJr$r$r%rM%s  zdigestchecker.__init__cCs.|j|}|j||jt|7_|SrN)rdreadrjrHrflen)rIlengthcontentr$r$r%rn,s  zdigestchecker.readcCsj|j|jkrttd|j|jf|jD]\}}||j|kr2ttd|||j|fqdS)Ns"size mismatch: expected %d, got %ds %s mismatch: expected %s, got %s)rerfr rFrGrhitemsrjrIrLvr$r$r%validate2s  zdigestchecker.validateNrm)r]r^r_r`rMrnrur$r$r$r%rcs   rccCs,|durt||||St||dSrN) memoryview) sliceableoffsetrpr$r$r%bufferEsrzics|eZdZdZfddZddZeddZedd Zd d Z d d Z ddZ ddZ ddZ ddZefddZZS)bufferedinputpipeaWa manually buffered input pipe Python will not let us use buffered IO and lazy reading with 'polling' at the same time. We cannot probe the buffer state and select will not detect that data are ready to read if they are already buffered. This class let us work around that by implementing its own buffering (allowing efficient readline) while offering a way to know if the buffer is empty from the output (allowing collaboration of the buffer with polling). This class lives in the 'util' module because it makes use of the 'os' module from the python stdlib. cst|trt}tt||SrN) isinstancefileobjectproxyobservedbufferedinputpipesuperr{__new__)clsrk __class__r$r%r]s zbufferedinputpipe.__new__cCs||_g|_d|_d|_dS)NFr)_input_buffer_eof_lenbuf)rIinputr$r$r%rMe zbufferedinputpipe.__init__cCrU)zTrue is any data is currently buffered This will be used externally a pre-step for polling IO. If there is already data then no polling should be set in place.)boolrrWr$r$r% hasbufferks zbufferedinputpipe.hasbuffercCs|jjSrN)rclosedrWr$r$r%rsszbufferedinputpipe.closedcC |jSrN)rfilenorWr$r$r%rwrYzbufferedinputpipe.filenocCrrN)rcloserWr$r$r%rzrYzbufferedinputpipe.closecCs2|js|j|kr||js|j|ks||SrN)rr _fillbuffer _frombufferrIrlr$r$r%rn}s zbufferedinputpipe.readcCs2|js|jdkr|t|t|t|j|Sr')rrrmax _chunksizerminrr$r$r%unbufferedreadsz bufferedinputpipe.unbufferedreadcOst|jdkrd|jg|_t|jd|_d}|jr$|jdd}|jsA|dkrA||jr:|jdd}|jsA|dks+|d}|dkrM|j}nt|jdkr`||jt|jd7}||S)NrrBrrm )rorjoinrfindrrr)rIargskwargslfirlr$r$r%readlines" zbufferedinputpipe.readlinecCs||dks|js dS|jd}t|jdkrd|j}|d|}|t|d}|r6|g|_t||_|Sg|_d|_|S)zYreturn at most 'size' data from the buffer The data are removed from the buffer.rrBrN)rrorr)rIrlbufrPr$r$r%rs    zbufferedinputpipe._frombuffercCsBt|j|}|sd|_|S|jt|7_|j||S)zread data to the bufferT) r rnrrrrrorappend)rIrlrPr$r$r%rs zbufferedinputpipe._fillbuffer)r]r^r_r`rrMpropertyrrrrrnrrrrr __classcell__r$r$rr%r{Ns   r{csj|dkrdS|dur d}tdfdd}z tj||tjdWSty4t|jdkr3YdSw)NrrBrcsSrNr$r$fpr$r%zmmapread..)access)rmmap ACCESS_READ ValueErrorr fstatst_size)rrlfdr$rr%mmapreads rc@seZdZdZdZddZddZddZeZd d Z d d Z d dZ ddZ ddZ ddZddZddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Zd1d2Zd3d4Zd5d6Zd7S)8r}zA proxy around file objects that tells a watcher when events occur. This type is intended to only be used for testing purposes. Think hard before using it in important code. _orig _observercC t|d|t|d|dSNrrobject __setattr__)rIrkobserverr$r$r%rMzfileobjectproxy.__init__cCs.hd}||vrt||Stt|d|S)N>rnseektellrr9read1writedetachrisattyreadallreadablereadintorseekabletruncatewritabler readlines writelinesrr__getattribute__r)rInameoursr$r$r%rs z fileobjectproxy.__getattribute__cCtt|dSNrrrrrWr$r$r% __nonzero__zfileobjectproxy.__nonzero__cCtt|d|SrrrrrIrr$r$r% __delattr__ zfileobjectproxy.__delattr__cCtt|d||Srr rrrIrvaluer$r$r%r zfileobjectproxy.__setattr__cCst|dSr)rrrXrWr$r$r%rXrzfileobjectproxy.__iter__cOVt|d}t|||i|}t|d}t||d}|r)||g|Ri||SrrrIrrrorigresrfnr$r$r% _observedcall   zfileobjectproxy._observedcallcOt|ddg|Ri|S)NrrrrrIrrr$r$r%r  zfileobjectproxy.closecOr)Nrrrrr$r$r%r%rzfileobjectproxy.filenocOr)Nrr9rrr$r$r%r9*rzfileobjectproxy.flushcOr)Nrrrrr$r$r%r/rzfileobjectproxy.isattycOr)Nrrrrr$r$r%r4rzfileobjectproxy.readablecOr)Nrrrrr$r$r%r9rzfileobjectproxy.readlinecOr)Nrrrrr$r$r%r>rzfileobjectproxy.readlinescOr)Nrrrrr$r$r%rCrzfileobjectproxy.seekcOr)Nrrrrr$r$r%rHrzfileobjectproxy.seekablecOr)Nrrrrr$r$r%rMrzfileobjectproxy.tellcOr)Nrrrrr$r$r%rRrzfileobjectproxy.truncatecOr)Nrrrrr$r$r%rWrzfileobjectproxy.writablecOr)Nrrrrr$r$r%r\rzfileobjectproxy.writelinescOr)Nrrnrrr$r$r%rnarzfileobjectproxy.readcOr)Nrrrrr$r$r%rfrzfileobjectproxy.readallcOr)Nrrrrr$r$r%rkrzfileobjectproxy.readintocOr)Nrrrrr$r$r%rprzfileobjectproxy.writecOr)Nrrrrr$r$r%rurzfileobjectproxy.detachcOr)Nrrrrr$r$r%rzrzfileobjectproxy.read1N) r]r^r_r` __slots__rMrr__bool__rrrXrrrr9rrrrrrrrrrrnrrrrrr$r$r$r%r}s<& r}cs8eZdZdZfddZfddZfddZZS)r~aA variation of bufferedinputpipe that is aware of fileobjectproxy. ``bufferedinputpipe`` makes low-level calls to ``os.read()`` that bypass ``fileobjectproxy``. Because of this, we need to make ``bufferedinputpipe`` aware of these operations. This variation of ``bufferedinputpipe`` can notify observers about ``os.read()`` events. It also re-publishes other events, such as ``read()`` and ``readline()``. cs0tt|}t|jjdd}|r||t|S)Nosread)rr~rrrrr)rIrrrr$r%rs  z%observedbufferedinputpipe._fillbuffercs2tt||}t|jjdd}|r||||S)N bufferedread)rr~rnrrr)rIrlrrrr$r%rns  zobservedbufferedinputpipe.readcs6tt|j|i|}t|jjdd}|r|||S)Nbufferedreadline)rr~rrrr)rIrrrrrr$r%rs z"observedbufferedinputpipe.readline)r]r^r_r`rrnrrr$r$rr%r~s   r~> recvsendsendtosendallmakefilerecvfrom recv_into gettimeout setsockopt settimeout setblocking recvfrom_intoc@seZdZdZdZddZddZddZd d Zd d Z e Z d dZ ddZ ddZ ddZddZddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'S)( socketproxyzA proxy around a socket that tells a watcher when events occur. This is like ``fileobjectproxy`` except for sockets. This type is intended to only be used for testing purposes. Think hard before using it in important code. rcCrrr)rIsockrr$r$r%rMrzsocketproxy.__init__cCs&|tvr t||Stt|d|Sr)PROXIED_SOCKET_METHODSrrrrr$r$r%rs zsocketproxy.__getattribute__cCrrrrr$r$r%rrzsocketproxy.__delattr__cCrrrrr$r$r%rrzsocketproxy.__setattr__cCrrrrWr$r$r%rrzsocketproxy.__nonzero__cOrrrrr$r$r%rrzsocketproxy._observedcallc OsLt|ddg|Ri|}t|d}t|j||j|j|j|j|jdS)Nrrrreadswriteslogdata logdataapis) rrmakeloggingfileobjectrkrrrrr)rIrrrrr$r$r%rs"  zsocketproxy.makefilecOr)Nrrrrr$r$r%rrzsocketproxy.recvcOr)Nrrrrr$r$r%rrzsocketproxy.recvfromcOr)Nrrrrr$r$r%rrzsocketproxy.recvfrom_intocOr)Nr recv_inforrr$r$r%r rzsocketproxy.recv_intocOr)Nrrrrr$r$r%rrzsocketproxy.sendcOr)Nrrrrr$r$r%rrzsocketproxy.sendallcOr)Nrrrrr$r$r%rrzsocketproxy.sendtocOr)Nrrrrr$r$r%r rzsocketproxy.setblockingcOr)Nrrrrr$r$r%r%rzsocketproxy.settimeoutcOr)Nrrrrr$r$r%r*rzsocketproxy.gettimeoutcOr)Nrrrrr$r$r%r/rzsocketproxy.setsockoptN)r]r^r_r`rrMrrrrrrrrrrrrrrrrrrr$r$r$r%rs, rc@seZdZddZddZdS)baseproxyobservercCs||_||_||_||_dSrN)rkrrr)rIrkrrrr$r$r%rM6rzbaseproxyobserver.__init__cCs|js|jr|jd|jdSd|vr;|jr&|jdt|n|jd|jt|f|jdS|jrD|jd|d}|D]}|jd|jt|fqK|jdS)Nrs: %s s %s> %s s: T) rrrkrr9r escapestrr splitlines)rIrPlinesliner$r$r% _writedata<s*     zbaseproxyobserver._writedataN)r]r^r_rMrr$r$r$r%r5s rcs`eZdZdZ dfdd ZdddZdd d Zd d Zd dZddZ ddZ ddZ Z S)fileobjectobserverzLogs file object activity.TFcs&tt|||||||_||_dSrN)rrrMrr)rIrkrrrrrrr$r%rM]s zfileobjectobserver.__init__rmcCsZ|jsdS|dur d}|dkr|dkrdS|jr&|jd|j|t|f||dS)NrBrms%s> read(%d) -> %drrrkrrrorrIrrlr$r$r%rndszfileobjectobserver.readcC8|jsdS|jr|jd|jt|f||dS)Ns%s> readline() -> %dr)rIrlimitr$r$r%rws zfileobjectobserver.readlinecCsd|jsdS|jr|jd|jt||f|dur |d|nd}t|tr+|}| |dS)Ns%s> readinto(%d) -> %rrrB) rrrkrrror|rwtobytesr)rIrdestrPr$r$r%rs zfileobjectobserver.readintocCsN|jsdS|dur|rt|}|jr |jd|jt||f||dS)Ns%s> write(%d) -> %r)rrorrkrrr)rIrrPr$r$r%rs zfileobjectobserver.writecC$|jsdS|jd|j|fdS)Ns%s> flush() -> %r )rrkrrrIrr$r$r%r9zfileobjectobserver.flushcCs:|jsdS|jr|jd|j|t|f||dS)Ns%s> bufferedread(%d) -> %drrr$r$r%rszfileobjectobserver.bufferedreadcCr)Ns%s> bufferedreadline() -> %drr r$r$r%rsz#fileobjectobserver.bufferedreadlineTTFTrv) r]r^r_r`rMrnrrrr9rrrr$r$rr%rZs    rTFcCst||||||d}t||S)z.Turn a file object into a logging file object.r)rr})loghrkrrrrrrr$r$r%rs rcseZdZdZ     d fdd Zd!ddZd"d d Zd"d d Zd"ddZd#ddZ d"ddZ d"ddZ d$ddZ ddZ ddZddZddZZS)%socketobserverzLogs socket activity.TFcs,tt|||||||_||_||_dSrN)rrrMrrstates)rIrkrrrrrrrr$r%rMs  zsocketobserver.__init__NcCs&|jsdS|jd|j||fdS)Ns%s> makefile(%r, %r) rrkrr)rIrmodebufsizer$r$r%rszsocketobserver.makefilercCs<|jsdS|jr|jd|j||t|f||dS)Ns%s> recv(%d, %d) -> %drrIrrlflagsr$r$r%rszsocketobserver.recvc CsD|jsdS|jr|jd|j||t|df||ddS)Ns%s> recvfrom(%d, %d) -> %drrrr$r$r%rszsocketobserver.recvfromcCsH|jsdS|jr|jd|j|||df||d|ddS)Ns%s> recvfrom_into(%d, %d) -> %drrrrkrrrrIrrrlrr$r$r%rszsocketobserver.recvfrom_intocCs@|jsdS|jr|jd|j|||f||d|dS)Ns%s> recv_into(%d, %d) -> %drrrr$r$r%rszsocketobserver.recv_intocCs:|jsdS|jd|jt||t|f||dS)Ns%s> send(%d, %d) -> %d)rrkrrrorrIrrPrr$r$r%rs zsocketobserver.sendcCs:|jsdS|jr|jd|jt||f||dS)Ns%s> sendall(%d, %d)rrrkrrrorrr$r$r%rszsocketobserver.sendallcCsL|jsdS|r |}nd}|jr|jd|jt||||f||dS)Nrs%s> sendto(%d, %d, %r) -> %dr)rIrrPflagsoraddressaddressrr$r$r%r(szsocketobserver.sendtocC$|jsdS|jd|j|fdS)Ns%s> setblocking(%r) r)rIrflagr$r$r%r9rzsocketobserver.setblockingcCr)Ns%s> settimeout(%r) r)rIrrr$r$r%r?rzsocketobserver.settimeoutcCr )Ns%s> gettimeout() -> %f rr r$r$r%rErzsocketobserver.gettimeoutcCs*|jsdS|jd|j||||fdS)Ns!%s> setsockopt(%r, %r, %r) -> %r r)rIrleveloptnamerr$r$r%rKszsocketobserver.setsockoptTTTFTNNr)rrrN)r]r^r_r`rMrrrrrrrrrrrrrr$r$rr%rs(   rc Cs t|||||||d}t||S)z$Turn a socket into a logging socket.)rrrrr)rr) rrkrrrrrrrr$r$r%makeloggingsocketUs  r%cCs*z ddlm}|jWStyYdSw)z(Return version information if available.r __version__sunknown)r-r'r; ImportErrorr&r$r$r%r;ms   r;c Cs|st}td|}|sd|}}n|dr|\}}n|dd}}|dus-Jg}|dD]}z |t|Wq4tyIYnwt |dkr[|dt |dksP|dkrg|d|dfS|dkrv|d|d|dfS|d kr|d|d|d|fSt d |) azParses a Mercurial version string into an N-tuple. The version string to be parsed is specified with the ``v`` argument. If it isn't defined, the current Mercurial version string will be parsed. ``n`` can be 2, 3, or 4. Here is how some version strings map to returned values: >>> v = b'3.6.1+190-df9b73d2d444' >>> versiontuple(v, 2) (3, 6) >>> versiontuple(v, 3) (3, 6, 1) >>> versiontuple(v, 4) (3, 6, 1, '190-df9b73d2d444') >>> versiontuple(b'3.6.1+190-df9b73d2d444+20151118') (3, 6, 1, '190-df9b73d2d444+20151118') >>> v = b'3.6' >>> versiontuple(v, 2) (3, 6) >>> versiontuple(v, 3) (3, 6, None) >>> versiontuple(v, 4) (3, 6, None, None) >>> v = b'3.9-rc' >>> versiontuple(v, 2) (3, 9) >>> versiontuple(v, 3) (3, 9, None) >>> versiontuple(v, 4) (3, 9, None, 'rc') >>> v = b'3.9-rc+2-02a8fea4289b' >>> versiontuple(v, 2) (3, 9) >>> versiontuple(v, 3) (3, 9, None) >>> versiontuple(v, 4) (3, 9, None, 'rc+2-02a8fea4289b') >>> versiontuple(b'4.6rc0') (4, 6, None, 'rc0') >>> versiontuple(b'4.6rc0+12-425d55e54f98') (4, 6, None, 'rc0+12-425d55e54f98') >>> versiontuple(b'.1.2.3') (None, None, None, '.1.2.3') >>> versiontuple(b'12.34..5') (12, 34, None, '..5') >>> versiontuple(b'1.2.3.4.5.6') (1, 2, 3, '.4.5.6') s(\d+(?:\.\d+){,2})[+-]?(.*)rBrN.rr)s invalid version part request: %d) r;remodmatchgroupgroupssplitrintrror ProgrammingError)rtnmvpartsextravintsir$r$r% versiontuplews47        r:csVjjdkrgfdd}|Sijjdkr"fdd}|Sfdd}|S)z"cache the result of function callsrcs tdkr dSr')rorr$)func listcacher$r%fs  zcachefunc..frcs|vr ||<|SrNr$argcacher;r$r%r= cs|vr ||<|SrNr$rr@r$r%r=rB)__code__ co_argcountr;r=r$)rAr;r<r% cachefuncs   rGc@s eZdZdZddZddZdS)cowz^helper class to make copy-on-write easier Call preparewrite before doing any writes. cCs(t|ddr|jd8_||S|S)z;call this before writes, return self or a copied new object_copiedrr)rrIrrWr$r$r% preparewrites  zcow.preparewritecCst|ddd|_|S)zalways do a cheap copyrIrr)rrIrWr$r$r%copyszcow.copyN)r]r^r_r`rJrKr$r$r$r%rHs rHcs6eZdZdZfddZejrddZddZZ S)sortdicta[a simple sorted dictionary >>> d1 = sortdict([(b'a', 0), (b'b', 1)]) >>> d2 = d1.copy() >>> d2 sortdict([('a', 0), ('b', 1)]) >>> d2.update([(b'a', 2)]) >>> list(d2.keys()) # should still be in last-set order ['b', 'a'] >>> d1.insert(1, b'a.5', 0.5) >>> d1 sortdict([('a', 0), ('a.5', 0.5), ('b', 1)]) cs$||vr||=tt|||dSrN)rrL __setitem__)rIrSrrr$r%rMszsortdict.__setitem__cKsDt|tr t|}|D]\}}|||<q |D]}||||<qdSrN)r|rgr iteritems)rIsrcr=rLrtr$r$r%rHs    zsortdict.updatecCsHtt|D]\}\}}||kr|||<||kr!||=|||<qdSrN) enumeratelistrr)rIpositionrSrr9rLrtr$r$r%insertszsortdict.insert) r]r^r_r`rMrispypyrHrSrr$r$rr%rLs  rLc@eZdZdZdS)cowdictacopy-on-write dict Be sure to call d = d.preparewrite() before writing to d. >>> a = cowdict() >>> a is a.preparewrite() True >>> b = a.copy() >>> b is a True >>> c = b.copy() >>> c is a True >>> a = a.preparewrite() >>> b is a False >>> a is a.preparewrite() True >>> c = c.preparewrite() >>> b is c False >>> b is b.preparewrite() True Nr]r^r_r`r$r$r$r%rV(rVc@rU) cowsortdictzZcopy-on-write sortdict Be sure to call d = d.preparewrite() before writing to d. NrWr$r$r$r%rYCrXrYc@sBeZdZdZejZejddZejddZ ddZ dd Z d S) transactionalzBBase class for making a transactional type into a context manager.cCdS)z$Successfully closes the transaction.Nr$rWr$r$r%rOrztransactional.closecCr[)zoMarks the end of the transaction. If the transaction has not been closed, it will be aborted. Nr$rWr$r$r%releaseSrztransactional.releasecC|SrNr$rWr$r$r% __enter__Zztransactional.__enter__cCs8z|dur|W|dSW|dS|wrN)rr\)rIexc_typeexc_valexc_tbr$r$r%__exit__]s   ztransactional.__exit__N) r]r^r_r`abcABCMeta __metaclass__abstractmethodrr\r^rcr$r$r$r%rZJs   rZccsX|sdVdSzz dV|Wn tjy|wW|dS|w)zA context manager that closes the transaction on InterventionRequired If no transaction was provided, this simply runs the body and returns N)rr InterventionRequiredr\)trr$r$r%acceptinterventiones rjccs |VdSrNr$) enter_resultr$r$r%nullcontextmanagerxs rlc@s$eZdZdZdZddZddZdS) _lrucachenodezA node in a doubly linked list. Holds a reference to nodes on either side as well as a key-value pair for the dictionary entry. )nextprevrSrcostcCs"||_||_t|_d|_d|_dSr')rnro_notsetrSrrprWr$r$r%rMs  z_lrucachenode.__init__cCst|_d|_d|_dS)zMark the node as emptied.Nr)rqrSrrprWr$r$r% markemptys z_lrucachenode.markemptyN)r]r^r_r`rrMrrr$r$r$r%rm}s  rmc@seZdZdZd&ddZddZddZd d Zd d Zd&d dZ ddZ ddZ e fddZ d'ddZe fddZddZd(ddZddZd d!Zd"d#Zd$d%ZdS)) lrucachedictaDict that caches most recent accesses and sets. The dict consists of an actual backing dict - indexed by original key - and a doubly linked circular list defining the order of entries in the cache. The head node is the newest entry in the cache. If the cache is full, we recycle head.prev and make it the new head. Cache accesses result in the node being moved to before the existing head and being marked as the new head node. Items in the cache can be inserted with an optional "cost" value. This is simply an integer that is specified by the caller. The cache can be queried for the total cost of all items presently in the cache. The cache can also define a maximum cost. If a cache insertion would cause the total cost of the cache to go beyond the maximum cost limit, nodes will be evicted to make room for the new code. This can be used to e.g. set a max memory limit and associate an estimated bytes size cost to each item in the cache. By default, no maximum cost is enforced. rcCs*i|_t|_d|_||_d|_||_dS)Nrr)_cacherm_headrecapacity totalcostmaxcost)rIrrxr$r$r%rMs  zlrucachedict.__init__cCrUrN)rortrWr$r$r%__len__rYzlrucachedict.__len__cCs ||jvSrN)rtrIrLr$r$r% __contains__rYzlrucachedict.__contains__ccs.|j}tt|jD] }|jV|j}q dSrN)rurangerortrSrn)rIr4r9r$r$r%rXs zlrucachedict.__iter__cCs|j|}|||jSrN)rt _movetoheadr)rIrLnoder$r$r%rTs  zlrucachedict.__getitem__cCs|j|}|dur-|j|j8_||_||_|j|7_|||jr+|dS|j|j kr8| }n|j j }|j turN|j|j8_|j|j =||_ ||_||_|j|7_||j|<||_ |jro|dSdS)z8Insert a new item in the cache with optional cost value.N)rtgetrwrprr}rx_enforcecostlimitrerv _addcapacityrurorSrq)rIrLrtrpr~r$r$r%rSs2        zlrucachedict.insertcCs|||dSrN)rSrsr$r$r%rMrzlrucachedict.__setitem__cCs||dSrN)poprzr$r$r% __delitem__zlrucachedict.__delitem__cCsrz|j|}Wnty|tur|YSw|dusJ|j}|j|j8_||||j |_ |SrN) rtrKeyErrorrqrrwrprrr}rnru)rIrLr,r~rr$r$r%rs   zlrucachedict.popNcCs$z||WSty|YSwrN)rTr)rIrLr,r$r$r%r s   zlrucachedict.getcCs@z|j|}|dus J|jWSty|tur|YSw)zGet the specified item without moving it to the head Unlike get(), this doesn't mutate the internal state. But be aware that it doesn't mean peek() is thread safe. N)rtrrrq)rIrLr,r~r$r$r%peeks   zlrucachedict.peekcCsF|j}|jtur|j|j8_||j}|jtus|jdSrN) rurSrqrwrprrrnrtclear)rIr4r$r$r%rs  zlrucachedict.clearcCs|p|j}|p |j}t||d}|jj}|jtur+||jur+|j}|jtur+||justt|j D]}|j |j|j |j d|j}q2|S)a8Create a new cache as a copy of the current one. By default, the new cache has the same capacity as the existing one. But, the cache capacity can be changed as part of performing the copy. Items in the copy have an insertion/access order matching this instance. )rx)rp) rvrxrsrurorSrqr|rortrSrrp)rIrvrxresultr4r9r$r$r%rK(s  zlrucachedict.copycCs||jsdS|jj}|dusJ|jtur|j}|jtus|dus"J|j|j}}|j|j=|j|j8_|||fS)zuRemove the oldest item from the cache. Returns the (key, value) describing the removed cache entry. N) rtrurorSrqrrwrprr)rIr4rSrr$r$r% popoldestGs    zlrucachedict.popoldestcCsF|j}|j|j_|j|j_|j|_|jj|_||j_||j_||_dS)aqMark a node as the newest, making it the new head. When a node is accessed, it becomes the freshest entry in the LRU list, which is denoted by self._head. Visually, let's make ``N`` the new head node (* denotes head): previous/oldest <-> head <-> next/next newest ----<->--- A* ---<->----- | | E <-> D <-> N <-> C <-> B To: ----<->--- N* ---<->----- | | E <-> D <-> C <-> B <-> A This requires the following moves: C.next = D (node.prev.next = node.next) D.prev = C (node.next.prev = node.prev) E.next = N (head.prev.next = node) N.prev = E (node.prev = head.prev) N.next = A (node.next = head) A.prev = N (head.prev = node) N)rurnro)rIr~headr$r$r%r}cs    zlrucachedict._movetoheadcCs:|j}t}||j_|j|_||_||_|jd7_|S)zhAdd a node to the circular linked list. The new node is inserted before the head node. r)rurmrornre)rIrr~r$r$r%rszlrucachedict._addcapacitycCst|dks |j|jkrdSt|jd}|jj}|jtur&|j}|jtust|dkrR|j|krV|j|j=|j|j 8_| |j}t|dkrT|j|ks1dSdSdSdS)Nrg?) rorwrxr2rurorSrqrtrprr)rI targetcostr4r$r$r%rs   &zlrucachedict._enforcecostlimitr$rNr')r]r^r_r`rMryr{rXrTrSrMrrqrrrrrKrr}rrr$r$r$r%rss&   '    / rscs@itjjdkrfdd}|Sfdd}|S)z+cache most recent results of function callsrcsH|vrtdkr=||<n|||SNropopleftremoverr>rAr;orderr$r%r=    zlrucachefunc..fcsH|vrtdkr=||<n|||SrrrCrr$r%r=r) collectionsdequerDrErFr$rr% lrucachefuncs  rc@s&eZdZddZdddZddZdS) propertycachecCs||_|j|_dSrN)r;r]r)rIr;r$r$r%rMs zpropertycache.__init__NcCs||}||||SrN)r; cachevalue)rIobjtyperr$r$r%__get__s  zpropertycache.__get__cCs||j|j<dSrN)__dict__r)rIrrr$r$r%rszpropertycache.cachevaluerN)r]r^r_rMrrr$r$r$r%rs  rcCs$t|}||jvr|j|=dSdS)z2clear a cached property value, if one has been setN)rr5r)rpropr$r$r%clearcachedpropertys   rccsdd}g}d}|D]5}|||t|7}||kr@||kr6|d>}d||>}||kr0|}||kr6|}d|Vd}g}q |rKd|VdSdS)zrreturn no less than min bytes per chunk while data remains, doubling min after each chunk until it reaches maxcSs,|sdSd}|r|dL}|d7}|s|dS)Nrrr$)xr9r$r$r%log2szincreasingchunks..log2rrrBN)rror)sourcerrrrblenchunknminr$r$r%increasingchunkss,     rcCr[)NTr$rr$r$r%alwaysr_rcCr[NFr$rr$r$r%neverr_rcfdd}|S)a disable garbage collector Python's garbage collector triggers a GC each time a certain number of container objects (the number being defined by gc.get_threshold()) are allocated even when marked not to be tracked by the collector. Tracking has no effect on when GCs are triggered, only on what objects the GC looks into. As a workaround, disable GC while building complex (huge) containers. This garbage collector issue have been fixed in 2.7. But it still affect CPython's performance. cs@t}tz|i|W|rtSS|rtwwrN)gc isenableddisableenable)rr gcenabledr;r$r%wrapper-s  znogc..wrapperr$r;rr$rr%nogcs  rcCr]rNr$rr$r$r%r;rrcCs|st|Stj|r.tj|dtj|dkr%tj|t|Sdt||f}t||d}}| | |r`|r`|d|dkr`| | |r`|r`|d|dksL| t j dgt ||prdS)aareturn the relative path from one place to another. root should use os.sep to separate directories n1 should use os.sep to separate directories n2 should use "/" to separate directories returns an os.sep-separated path. If n1 is a relative path, it's assumed it's relative to root. n2 should always be relative to root. r/rm..r+) localpathr pathisabs splitdriverpconvert splitpathr1reverserrossepro)rootn1n2abr$r$r%pathto>s   rcsfdd}|S)z5wrap a function with code to check for calling errorscsBz|i|WSty tttdkrtjw)Nr*) TypeErrorro traceback extract_tbr7exc_infor SignatureError)rrdepthr;r$r%check]s zchecksignature..checkr$)r;rrr$rr%checksignatureZsr> hfsjfsufsxfszfsNTFSapfsext2ext3ext4btrfstmpfsreiserfsc Cs|r|rJd}tj|r|r|ot|}t||rB|rBz ttj|} Wn ty4d} Ynw| t vrB|dur@|d}|ryzt |||durUd} t | WdSt tfyx} z| jtjkrn|durn|WYd} ~ nd} ~ wwtj|rtt|||durd} t | dSz]t|||rt||n't|||r|jrt|} | |r|jtjdd@} t|| | f|durt|dd}||WdWdS1swYWdSWdStjy}zt t |d}~ww) afcopy a file, preserving mode and optionally other stat info like atime/mtime checkambig argument is used with filestat, and is useful only if destination file is guarded by any lock (e.g. repo.lock or repo.wlock). copystat and checkambig should be exclusive. nb_bytes: if set only copy the first `nb_bytes` of the source file. NFz7the `nb_bytes` argument is incompatible with `hardlink`z"cannot use `nb_bytes` on a symlinkrzr+)r)!r rlexistsfilestatfrompathunlink getfstypedirnameOSError_hardlinkfswhitelistoslinkr r3IOErrorerrnoEEXISTislinksymlinkreadlinkshutilcopyfilecopystatcopymodestatisambigST_MTIMEutimer rErrorrFr forcebytestr)rOr hardlinkr checkambignb_bytesno_hardlink_cbcheck_fs_hardlinkoldstatfstyper5excnewstatadvancedr=instr$r$r%rzsn             &rc s\d}fdd}tj|rSdur#t|jttj|jk|t|t|D]\}}tj||}tj||} t || \} || 7}q/|fSdurkttj|jttj|jk|rzt ||Wn(t t fy} z| j t jkrdt||WYd} ~ n d} ~ wwt|||d7}r|fS)z2Copy a directory tree using hardlinks if possible.rcs"rrtdntd_dSdS)Nslinkingscopying)rGtopicr$rprogressr$r%settopicszcopyfiles..settopicNFr)r risdirrst_devrmkdirlistdirr copyfilesrrrrrrrK increment) rOdstrrnumrrkindsrcnamedstnamer4rr$rr%r sD     r >auxconnulprncom1com2com3com4com5com6com7com8com9lpt1lpt2lpt3lpt4lpt5lpt6lpt7lpt8lpt9s:*?"<>|cCs|dr tdSd|vrtdS|dddD]Z}|sqt|D]#}|tvr3td|St|dkrFtdt|Sq#|d d }|r^| t vr^td|S|d d }|d vrt|dvrttd|Sqd S)aUCheck that the base-relative path is a valid filename on Windows. Returns None if the path is ok, or a UI string describing the problem. >>> checkwinfilename(b"just/a/normal/path") >>> checkwinfilename(b"foo/bar/con.xml") "filename contains 'con', which is reserved on Windows" >>> checkwinfilename(b"foo/con.xml/bar") "filename contains 'con', which is reserved on Windows" >>> checkwinfilename(b"foo/bar/xml.con") >>> checkwinfilename(b"foo/bar/AUX/bla.txt") "filename contains 'AUX', which is reserved on Windows" >>> checkwinfilename(b"foo/bar/bla:.txt") "filename contains ':', which is reserved on Windows" >>> checkwinfilename(b"foo/bar/b\07la.txt") "filename contains '\\x07', which is invalid on Windows" >>> checkwinfilename(b"foo/bar/bla ") "filename ends with ' ', which is not allowed on Windows" >>> checkwinfilename(b"../bar") >>> checkwinfilename(b"foo\\") "filename ends with '\\', which is invalid on Windows" >>> checkwinfilename(b"foo\\/bar") "directory name ends with '\\', which is invalid on Windows" \s3filename ends with '\', which is invalid on Windowss\/s9directory name ends with '\', which is invalid on Windowsrs4filename contains '%s', which is reserved on Windowss3filename contains '%s', which is invalid on Windowsr+rrmNs. rs8filename ends with '%s', which is not allowed on Windows) endswithrGreplacer1_filenamebytestr_winreservedcharsordrrlower_winreservednames)rr4cbasetr$r$r%checkwinfilenamesL     r3 perf_counterc Cszt||WSty }z |jtjkrWYd}~n d}~wty(YnwtjtjBtjBt tddB}t ||}t ||t |dS)zCreate a lock file atomically if possible This may leave a stale lock file if symlink isn't supported and signal interrupt is enabled. NO_BINARYr) r rrrrAttributeErrorO_CREATO_WRONLYO_EXCLrr rr)infopathnamewhyrldr$r$r%makelock]s     r>c Cszt|WSty!}z|jtjtjfvrWYd}~n d}~wty)Ynwt|d }|WdS1s>wYdSNrb)rrrEINVALENOSYSr6 posixfilern)r;r<rr$r$r%readlockqs    $rDcCs0zt|WStyt|jYSw)z1stat file object that may not have fileno method.)r rrr6rrrr$r$r%r~s  rcCst|}tj|\}}|}||kr|}||krdStj||}zt|}||kr3WdSWdSty?YdSw)z Return true if the given path is on a case-sensitive filesystem Requires a path (like /foo/.hg) ending with a foldable final directory component. TF)r lstatrr1upperr.rr)rs1drb2p2s2r$r$r%fscasesensitives   rLcCr]rNr$rr$r$r%rrc@s*eZdZddZd ddZeddZdS) _recCstd}d}z tt||aWdStydaYdSty9t|}t|}tt||atjaYdSw)Ns \[([^\[]+)\]s[ui]F) rre2r._re2r(rrr5 _re2_input)rI check_pattern check_inputr$r$r% _checkre2s      z _re._checkre2rcCstdur|tr:|tjtjB@dkr:|tj@rd|}|tj@r'd|}ztt|WStjy9Ynwt||S)zCompile a regular expression, using re2 if possible For best performance, use only re2-compatible regexp features. The only flags from the re module that are re2-compatible are IGNORECASE and MULTILINE.Nrs(?i)s(?m)) rOrSr- IGNORECASE MULTILINErNcompilerPr )rIpatrr$r$r%rVs   z _re.compilecCs tdur|tr tjStjS)zReturn the version of escape corresponding to self.compile. This is imperfect because whether re2 or re is used for a particular function depends on the flags, etc, but it's the best we can do. N)rOrSrNescaper-rWr$r$r%rXs z _re.escapeNr$)r]r^r_rSrVrrXr$r$r$r%rMs  rMc Csdd}tj}tjr|tj}|ddtd||f}tj|}g}| |D]<\}}|r7| |q+|t vrA||t |<t |} | |} | sY||t |<} | |} | | p^|tj ||}q+d |S)a4Get name in the case stored in the filesystem The name should be relative to root, and be normcase-ed for efficiency. Note that this function is unnecessary, and should not be called, for case-sensitive filesystems (simply because it's expensive). The root should be normcase-ed, too. cSsddt|DS)NcSsi|]}t||qSr$)normcase).0r4r$r$r% sz9fspath.._makefspathcacheentry..)r r )dirr$r$r%_makefspathcacheentryrz%fspath.._makefspathcacheentryr's\\s([^%s]+)|([%s]+)rB)rrosaltsepr*r-rVr rnormpathfindallr _fspathcacherr) rrr]sepspatternr\rpartsepcontentsfoundr$r$r%fspaths,         rhcCs\d\}}}zzUtjdtj|dtj|d\}}t|d|dd}t||t|}t |dkWW|dur@|||fD]}z |durPt |WqDt yZYqDwSt yYW|durl|||fD]}z |dur|t |Wqpt yYqpwd Sw|dur|||fD]}z |durt |Wqt yYqww) z5check whether hardlink count reporting works properly)NNN.%s-s1~prefixsuffixr\s%s2~NrF) rmkstempr rbasenamerrrrCnlinksrr)testfilef1f2rrr=r$r$r% checknlink s`              rtcCs"t|tjptjo|tjS)z)Check path ends with os.sep or os.altsep.)rr)rrr^rr$r$r% endswithsep6 s   rvcCs |tjS)zSplit path by os.sep. Note that this function does not use os.altsep because this is an alternative of simple "xxx.split(os.sep)". It is recommended to use os.path.normpath() before using this function if need.)r1rrrur$r$r%r@ s rc Cstj|\}}tjd|d|d\}}t|t|||||r$|SzIzt|d}Wn%tyQ} z| j t j krC|WYd} ~ WSt | ddsL|| _ d} ~ wwt|d} t |D]} | | q[|| W|Szt|WtyYw)aCreate a temporary file with the same contents from name The permission bits are copied from the original file. If the temporary file is going to be truncated immediately, you can use emptyok=True as an optimization. Returns the name of the temporary file. ri~rjr@Nfilenamewb)r rr1rrnrrrCrrENOENTrrx filechunkiterrrr) remptyok createmodeenforcewritablerHrrtempifprofprr$r$r% mktempcopyJ s>          rc@sVeZdZdZddZeddZeddZej Z dd Z d d Z d d Z ddZ dS)rzhelp to exactly detect change of a file 'stat' attribute is result of 'os.stat()' if specified 'path' exists. Otherwise, it is None. This can avoid preparative 'exists()' examination on client side of this class. cCs ||_dSrN)r)rIrr$r$r%rM} rYzfilestat.__init__c CsRz t|}W||Sty(}z|jtjkrd}WYd}~||Sd}~wwrN)r rrrrz)rrrerrr$r$r%r s  zfilestat.frompathcCst|}||SrN)r rr)rrrr$r$r%fromfp szfilestat.fromfpcCsz!|jj|jjko |jtj|jtjko |jtj|jtjkWSty*Ynwz |jduo5|jduWSty@YdSwr)rrST_CTIMErr6rIoldr$r$r%__eq__ s  zfilestat.__eq__cCs0z |jtj|jtjkWStyYdSw)aExamine whether new (= self) stat is ambiguous against old one "S[N]" below means stat of a file at N-th change: - S[n-1].ctime < S[n].ctime: can detect change of a file - S[n-1].ctime == S[n].ctime - S[n-1].ctime < S[n].mtime: means natural advancing (*1) - S[n-1].ctime == S[n].mtime: is ambiguous (*2) - S[n-1].ctime > S[n].mtime: never occurs naturally (don't care) - S[n-1].ctime > S[n].ctime: never occurs naturally (don't care) Case (*2) above means that a file was changed twice or more at same time in sec (= S[n-1].ctime), and comparison of timestamp is ambiguous. Base idea to avoid such ambiguity is "advance mtime 1 sec, if timestamp is ambiguous". But advancing mtime only in case (*2) doesn't work as expected, because naturally advanced S[n].mtime in case (*1) might be equal to manually advanced S[n-1 or earlier].mtime. Therefore, all "S[n-1].ctime == S[n].ctime" cases should be treated as ambiguous regardless of mtime, to avoid overlooking by confliction between such mtime. Advancing mtime "if isambig(oldstat)" ensures "S[n-1].mtime != S[n].mtime", even if size of a file isn't changed. F)rrr6rr$r$r%r s  zfilestat.isambigc Cs`|jtjdd@}z t|||fWdSty/}z|jtjkr*WYd}~dSd}~ww)aLChange file stat of specified path to avoid ambiguity 'old' should be previous filestat of 'path'. This skips avoiding ambiguity, if a process doesn't have appropriate privileges for 'path'. This returns False in this case. Otherwise, this returns True, as "ambiguity is avoided". rrNFT)rrr rrrEPERM)rIrrrrr$r$r% avoidambig s  zfilestat.avoidambigcCs ||k SrNr$)rIotherr$r$r%__ne__ rYzfilestat.__ne__N)r]r^r_r`rM classmethodrrr__hash__rrrrr$r$r$r%ru s  # rc@sBeZdZdZdddZddZd d Zd d Zd dZddZ dS)atomictempfileawritable file object that atomically updates a file All writes will go to a temporary copy of the original file. Call close() when you are done writing, and atomictempfile will rename the temporary copy to the original name, making the changes visible. If the object is destroyed without being closed, all your writes are discarded. checkambig argument of constructor is used with filestat, and is useful only if target file is guarded by any lock (e.g. repo.lock or repo.wlock). w+bNFcCsj||_t|d|v|d|vd|_t|j||_||_|jj|_|jj|_|jj|_|jj |_ |jj |_ dS)Nw)r|r}r~) _atomictempfile__namer _tempnamerC_fp _checkambigrnrrrr)rIrrr}rr$r$r%rM s    zatomictempfile.__init__cCs|jjsI|jt|j}|jot|}|rA|jrAt |j |t|}| |r?|jtj dd@}t |||fdSdSt |j |dSdS)Nrr)rrrrrrrrrrenamerrrr r)rIrxrrrr$r$r%r s      zatomictempfile.closecCs>|jjszt|jWn tyYnw|jdSdSrN)rrr rrrrrWr$r$r%discard s zatomictempfile.discardcCst|dr |dSdS)Nr) safehasattrrrWr$r$r%__del__ s  zatomictempfile.__del__cCr]rNr$rWr$r$r%r^ r_zatomictempfile.__enter__cCs |dur |dS|dSrN)rr)rIexctypeexcvaluerr$r$r%rc s  zatomictempfile.__exit__)rNF) r]r^r_r`rMrrrr^rcr$r$r$r%r s  rcCsJ|rt|nt||r#z ttj|WdSty"YdSwdS)z.unlink and remove the directory if it is emptyN) tryunlinkr removedirsr rrr)r= ignoremissingrmdirr$r$r% unlinkpath% s  rc CsDzt|WdSty!}z|jtjkrWYd}~dSd}~ww)z1Attempt to remove a file, ignoring ENOENT errors.N)rrrrz)r=er$r$r%r4 s rc Cszt||Wnbtyi}zV|jtjkrWYd}~dS|jtjks$|s%tjt|}||kr2t |||zt||Wnty^}z|jtjkrYWYd}~WYd}~dSd}~wwWYd}~nd}~ww|durvt ||dSdS)zrecursive directory creation with parent mode inheritance Newly created directories are marked as "not to be indexed by the content indexing service", if ``notindexed`` is specified for "write" mode access. N) makedirrrrrzr rrabspathmakedirschmod)rr notindexedrparentr$r$r%r> s2    rcCs6t|d }|WdS1swYdSr?)r rn)rrr$r$r%readfile\ s $rcC:t|d}||WddS1swYdS)Nryr rrtextrr$r$r% writefileb   "rcCr)Nsabrrr$r$r% appendfileh rrc@s"eZdZdZddZdddZdS) chunkbufferzoAllow arbitrary sized chunks of data to be efficiently read from an iterator over chunks of arbitrary size.cCs&dd}|||_t|_d|_dS)z?in_iter is the iterator that's iterating over the input chunks.cssZ|D]'}t|dkr'd}|t|kr&|d}|||V|}|t|ksq|VqdS)Nrro)chunksrposendr$r$r%splitbigu s   z&chunkbuffer.__init__..splitbigrN)rVrr_queue _chunkoffset)rIin_iterrr$r$r%rMr s  zchunkbuffer.__init__Nc Cs |dur d|jS|}g}|j}|dkr|s3d}|jD]}|||t|8}|dkr/nq|s3nX|d}t|}|j}|dkrT||krT||8}|||q||} || krq|| 8}||||dd|_n||||||j|7_|| 8}|dksd|S)zRead L bytes of data from the iterator of chunks of data. Returns less than L bytes if the iterator runs dry. If size parameter is omitted, read everythingNrBrr)rrVrrrorr) rIlleftrqueuetargetrchunklrychunkremainingr$r$r%rn sF      /zchunkbuffer.readrN)r]r^r_r`rMrnr$r$r$r%rn srccsj|dksJ|dus|dksJ |dur|}nt||}|o$||}|s)dS|r1|t|8}|Vq)aYCreate a generator that produces the data in the file size (default 131072) bytes at a time, up to optional limit (default is to read all data). Chunks may be less than size bytes if the chunk is the last chunk in the file, or the file is a socket or some other type of file that sometimes reads less data than is requested.rN)rrnro)r=rlr nbytesrKr$r$r%r{ s   r{c@rb) cappedreaderaA file object proxy that allows reading up to N bytes. Given a source file object, instances of this type allow reading up to N bytes from that source file object. Attempts to read past the allowed limit are treated as EOF. It is assumed that I/O is not performed on the original file object in addition to I/O that is performed by this instance. If there is, state tracking will get out of sync and unexpected results will ensue. cCs||_||_dS)z,Allow reading up to bytes from .N)rd_left)rIrkr r$r$r%rM s zcappedreader.__init__rmcCsP|jsdS|dkr |j}|jt||j}|jt|8_|jdks&J|S)NrBr)rrdrnrro)rIr4rPr$r$r%rn szcappedreader.readcCs2|t|}|dur dS||dt|<t|Sr')rnro)rIrrr$r$r%r s zcappedreader.readintoNrv)r]r^r_r`rMrnrr$r$r$r%r s   rcr)z@return a function that renders a readable count of some quantitycsDD]\}}}t|||kr||t|Sqdd|S)Nrmr*)absfloat)count multiplierdivisorformat unittabler$r%go s zunitcountfn..gor$)rrr$rr% unitcountfn s rcCs<||dkr ttd|dkrttd|d|fS)aCheck that linerange : makes sense and return a 0-based range. >>> processlinerange(10, 20) (9, 20) >>> processlinerange(2, 1) Traceback (most recent call last): ... ParseError: line range must be positive >>> processlinerange(0, 5) Traceback (most recent call last): ... ParseError: fromline must be strictly positive rsline range must be positivers"fromline must be strictly positive)r ParseErrorrG)fromlinetoliner$r$r%processlinerange s  rd@s%.0f GB s%.1f GBs%.2f GBrs%.0f MBs%.1f MBs%.2f MBs%.0f KBs%.1f KBs%.2f KBs %.0f bytesc@s0eZdZdZddZddZddZdd Zd S) transformingwriterz3Writable file wrapper to transform data by functioncCs||_||_dSrN)r_encode)rIrencoder$r$r%rM2 s ztransformingwriter.__init__cC|jdSrN)rrrWr$r$r%r6 rztransformingwriter.closecCrrN)rr9rWr$r$r%r99 rztransformingwriter.flushcCs|j||SrN)rrr)rIrPr$r$r%r< rztransformingwriter.writeN)r]r^r_r`rMrr9rr$r$r$r%r/ s  rs\r*\ncC td|S)Nr_eolresubrKr$r$r%tolfF  rcCr)N rrr$r$r%tocrlfK rrcCs t|tSrN)rrrr$r$r% _crlfwriterP rYrrsCPython)r,rcCs:d}t|turtt|j}|r|St|j dS)NTrB) rfilerS_ISREGr rrst_moderVr)rfastpathr$r$r%iterfilez s   rcCr]rNr$rr$r$r%r r_ccs$|D] }|D]}|Vq qdSrN)r)iteratorrrr$r$r% iterlines s  rcCstjtj|SrN)r r expanduser expandvarsrur$r$r% expandpath srcsxpddd}|r(|d|7}t|dkr"|dd}n|}||<td||f}|fdd|S)aReturn the result of interpolating items in the mapping into string s. prefix is a single character string, or a two character string with a backslash as the first character if the prefix needs to be escaped in a regular expression. fn is an optional function that will be applied to the replacement text just before replacement. escape_prefix is an optional flag that allows using doubled prefix for its escaping. cSr]rNr$rr$r$r%r rzinterpolate..|rNs%s(%s)cs|ddS)Nr)r/rrmappingr$r%r s)rriror-rVr)rkrrKr escape_prefixpatterns prefix_charrr$rr% interpolate s   rg@@s%.0f ss%.1f ss%.2f ss%.3f sgMbP?s%.1f mss%.2f mss%.3f msgư>s%.1f uss%.2f uss%.3f usg& .>s%.1f nss%.2f nss%.3f nsc@sPeZdZdZejedddZejddZejddZ ddZ e e Z d S) timedcmstatszFStats information produced by the timedcm context manager on entering.cCstSrN)timerr$r$r$r%r sztimedcmstats.)r,rrcCs|jrt|jSdS)Ns )elapsed timecountrWr$r$r% __bytes__ rztimedcmstats.__bytes__N)r]r^r_r`ribFactorystartrr rr strmethod__str__r$r$r$r%r s  rc gstjd7_ttjd}z>tj|g|R |VWdn1s&wYWt|j|_tjd8_dSWt|j|_tjd8_dSt|j|_tjd8_w)zA context manager that produces timing information for a given context. On entering a timedcmstats instance is produced. This context manager is reentrant. r)r N)timedcm_nestedrr logrrr) whencefmt whenceargs timing_statsr$r$r%r s rcr)zReport the execution time of a function call to stderr. During development, use as a decorator when you need to measure the cost of a function, e.g. as follows: @util.timed def foo(a, b, c): pass csnttj}|i|}Wdn1swYtj}|dd|jdtj|f|S)Ns %s%s: %s  r*)rrbytestrr]rr8rr )rr time_statsrr8rr$r%r s  ztimed..wrapperr$rr$rr%timed s r ))mr)kr)gr)skbr)smbr)sgbr)brcCst|}z#tD]\}}||r$tt|dt| |WSq t|WSty9t t d|w)zConvert a space specifier to a byte count. >>> sizetoint(b'30') 30 >>> sizetoint(b'2.2kb') 2252 >>> sizetoint(b'6M') 6291456 Nscouldn't parse size: %s) stripr. _sizeunitsr)r2rrorr rrG)rKr2rLur$r$r% sizetoint s   $  rc@s(eZdZdZddZddZddZdS) hookszA collection of hook functions that can be used to extend a function's behavior. Hooks are called in lexicographic order, based on the names of their sources.cCs g|_dSrN)_hooksrWr$r$r%rM- rYzhooks.__init__cCs|j||fdSrN)rr)rIrhookr$r$r%add0 rz hooks.addcGs8|jjdddg}|jD] \}}|||q|S)NcSs|dSr'r$rr$r$r%r4 sz hooks.__call__..)rS)rsortr)rIrresultsrrr$r$r%__call__3 s zhooks.__call__N)r]r^r_r`rMrrr$r$r$r%r( s  r %-*s in %s %s:%dc#sfddtd| dD| d}|r=tdd|D}|D]\}}|dur4|||fVq%||||fVq%dSdS)aYields lines for a nicely formatted stacktrace. Skips the 'skip' last entries, then return the last 'depth' entries. Each file+linenumber is formatted according to fileline. Each line is formatted according to line. If line is None, it yields: length of longest filepath+line number, filepath+linenumber, function Not be used in production code but very convenient while developing. cs0g|]\}}}}t||ft|fqSr$)rsysbytes)rZrlnr;_textfileliner$r% G s z"getstackframes..Nrcss|] }t|dVqdSrNr)rZentryr$r$r% L sz!getstackframes..)r extract_stackr)skiprrrentriesfnmaxfnlnr;r$rr%getstackframes; s  r)s stacktracerBcCsR|r||d||ft|d|dD] }|||q|dS)aWWrites a message to f (stderr) with a nicely formatted stacktrace. Skips the 'skip' entries closest to the call, then show 'depth' entries. By default it will flush stdout first. It can be used everywhere and intentionally does not require an ui object. Not be used in production code but very convenient while developing. s %s%s at: r)rN)r9rrstripr))r:r%r=otherfrrkrr$r$r%debugstacktraceT s  r,cCsh|durt}d||f}||vr||vr|StdD]}d|||f}||vr1||vr1|SqdS)a Generate a name that it is safe to rename f to in the given context. f: filename to rename tag: a string tag that will be included in the new name ctx: a context, in which the new name must not exist others: a set of other filenames that the new name must not be in Returns a file name of the form oldname~tag[~number] which does not exist in the provided context and is not in the set of other names. Ns%s~%srs%s~%s~%s)set itertoolsr)r=tagctxothersrr4r$r$r%safenamen s  r2cCs4||}t||krttdt||f|S)z=read n bytes from stream.read and abort if less was availables5stream ended unexpectedly (got %d bytes, expected %d))rnror rFrG)streamr4rKr$r$r% readexactly s   r4cCsp|dkr td||d@}|dL}g}|r+|td|B|d@}|dL}|s|t|d|S)aiEncode an unsigned integer value to a varint. A varint is a variable length integer of 1 or more bytes. Each byte except the last has the most significant bit set. The lower 7 bits of each byte store the 2's complement representation, least significant group first. >>> uvarintencode(0) '\x00' >>> uvarintencode(1) '\x01' >>> uvarintencode(127) '\x7f' >>> uvarintencode(1337) '\xb9\n' >>> uvarintencode(65536) '\x80\x80\x04' >>> uvarintencode(-1) Traceback (most recent call last): ... ProgrammingError: negative value for uvarint: -1 rsnegative value for uvarint: %drB)r r3rrbytechrr)rr)bytesr$r$r% uvarintencode s r:cCs>d}d} tt|d}||d@|>O}|d@s|S|d7}q)aDecode an unsigned variable length integer from a stream. The passed argument is anything that has a ``.read(N)`` method. >>> try: ... from StringIO import StringIO as BytesIO ... except ImportError: ... from io import BytesIO >>> uvarintdecodestream(BytesIO(b'\x00')) 0 >>> uvarintdecodestream(BytesIO(b'\x01')) 1 >>> uvarintdecodestream(BytesIO(b'\x7f')) 127 >>> uvarintdecodestream(BytesIO(b'\xb9\n')) 1337 >>> uvarintdecodestream(BytesIO(b'\x80\x80\x04')) 65536 >>> uvarintdecodestream(BytesIO(b'\x80')) Traceback (most recent call last): ... Abort: stream ended unexpectedly (got 0 bytes, expected 1) rTrr5r7r6)r-r4)rkrshiftbyter$r$r%uvarintdecodestream sr=c csvttjd}|dkr6z!z ttjdWn tjy YnwdVWttj|dSttj|wdVdS)NCr-)locale setlocaleLC_CTYPEr)oldlocr$r$r% with_lc_ctype s" rCcstjdr8ddlmmddlm}m}m }m }Gfddd|}|}|||_ |j |||jSzttjd}ttjd}||WStyWYd Sty`Yd Sw) zProvide an estimate for the available system memory in Bytes. If no estimate can be provided on the platform, returns None. swinr)c_long c_ulonglong) Structurebyrefsizeofwindllc sFeZdZdfdfdfdfdfdfdfdfd fg Zd S) z'_estimatememory..MEMORYSTATUSEXdwLength dwMemoryLoad ullTotalPhys ullAvailPhysullTotalPageFileullAvailPageFileullTotalVirtualullAvailVirtualullExtendedVirtualN)r]r^r__fields_r$DWORD DWORDLONGr$r%MEMORYSTATUSEX srW SC_PAGE_SIZE SC_PHYS_PAGESN)r sysplatform startswithctypesrDrEctypes.wintypesrFrGrHrIrJkernel32GlobalMemoryStatusExrMr sysconf sysconf_namesrr)rFrGrHrIrWrpagesizepagesr$rTr%_estimatememory s"     rd)rr!rNrr")Nr))rr)FFFNNTr#)FNF)FTr)rN)rrrr(r` __future__rrrdr contextlibrrhashlibr.r?rr r" pyplatformrer-rrr7timerr3r~r thirdpartyrrrrr r hgdemandimportr r-r r rrrutilsrrrr TYPE_CHECKINGtypingrrrr importmodrr b85decode b85encode cookielibhttplibpickler socketserverbytesiostringio xmlrpclib httpserverurlerrurlreqrr+rrrrGrbindunixsocket cachestat checkexec checklinkr expandglobsgetfsmountpointr get_password groupmembers groupnameisexecisownerr r lookupregrrpr_rY normcasespecnormcasefallback openhardlinksrparsepatchoutputrpollrCrrr samedevicesamefilesamestatsetflagsr1r statisexec statislinkr!rusernamer& compengines SERVERROLE CLIENTROLErecvfdsr6rrqr+renvironrr2filterwarningsr6ispy3r=md5sha1sha512rErZrLrArcrz NameErrorrr{rr}r~rrrrrrr%r;r:rGrH OrderedDictrLrgrVrYrZcontextmanagerrjrlrmrsrrrrrrrrTrrrrr r/r,r3rcheckosfilenameclockr>rDrrLrPrNrOr(rMrarhrtrvrrrrrrrrrrrr{rrr bytecountrrVrrrr oslinesep tonativeeol fromnativeeolnativeeolwriteridentitypython_implementation version_inforrrrrrKrrrr rrrr)r8stdoutr,r r2r4r:r=rCrdr$r$r$r%sd             0% n3*{%e    W'  . !  P- >  7-" +j F  S'                                   %-