o ]LbW@sddlmZddlZddlZddlZddlZddlZddlZddlZddl Z ddl m Z ddl m Z ddlmZmZmZm Z mZz ddlZejWn eyVddlZYnwedZejZejZejZejZejZejZej Z ej!Z!ej"Z"ej#Z#ej$Z$ej%j&Z&ej'Z'ej(Z(dZ)Gd d d e*Z+Gd d d e*Z,dcddZ-ej.Z.ddZ/ddZ0Gddde*Z1ddZ2ddZ3ddZ4ddZ5ddd d!Z6d"d#Z7d$d%Z8d&d'Z9d(d)Z:d*d+Z;d,d-Zd0Z?e>d1Z@d2d3ZAejBjCZDejEZFd4d5ZGd6d7ZHdaIdaJd8d9ZKd:d;ZLdd?ZNd@dAZOejPejQhZRdBdCZSdedDdEZTdedFdGZUdHdIZVdJdKZWdLdMZXdNdOZYdPdQZZdRdSZ[GdTdUdUe*Z\dfdVdWZ]dXZ^dYdZZ_d[d\Z`d]d^Zad_d`ZbdadbZcdS)g)absolute_importN)_)getattr)encodingerrorpolicypycompatwin32osutilc@s|eZdZdZdZdZdZddZddZd d Z d d Z d dZ ddZ ddZ ddZddZddZddZddZdS)mixedfilemodewrappera)Wraps a file handle when it is opened in read/write mode. fopen() and fdopen() on Windows have a specific-to-Windows requirement that files opened with mode r+, w+, or a+ make a call to a file positioning function when switching between reads and writes. Without this extra call, Python will raise a not very intuitive "IOError: [Errno 0] Error." This class wraps posixfile instances when the file is opened in read/write mode and automatically adds checks or inserts appropriate file positioning calls when necessary. rrcCs t|d|t|dddS)N_fp_lastopr)object __setattr__selffpr3/usr/lib/python3/dist-packages/mercurial/windows.py__init__Jszmixedfilemodewrapper.__init__cC|j|SNr __enter__rrrrrNs zmixedfilemodewrapper.__enter__cC|j|||dSrr__exit__)rexc_typeexc_valexc_tbrrrr Rzmixedfilemodewrapper.__exit__cC t|j|Srrrrnamerrr __getattr__U z mixedfilemodewrapper.__getattr__cCs|j||Sr)rr)rr(valuerrrrXz mixedfilemodewrapper.__setattr__cCs|jdtjdS)Nr)rseekosSEEK_CURrrrr _noopseek[r$zmixedfilemodewrapper._noopseekcOs"t|d|j|jj|i|SNr)rrOPNONErr-rargskwargsrrrr-^szmixedfilemodewrapper.seekcCs0|j|jkr |t|d|j|j|Sr1)rOPREADr0rrOPWRITErwrite)rdrrrr8bs  zmixedfilemodewrapper.writecO6|j|jkr |t|d|j|jj|i|Sr1)rr6 _noopeseekrrr7r writelinesr3rrrr<i zmixedfilemodewrapper.writelinescOr:r1)rr7r0rrr6rreadr3rrrr>pr=zmixedfilemodewrapper.readcOr:r1)rr7r0rrr6rreadliner3rrrr?wr=zmixedfilemodewrapper.readlinecOr:r1)rr7r0rrr6r readlinesr3rrrr@~r=zmixedfilemodewrapper.readlinesN)__name__ __module__ __qualname____doc__r2r6r7rrr r)rr0r-r8r<r>r?r@rrrrr 9s"  r c@8eZdZdZddZddZddZdd Zd d Zd S) fdproxyzeWraps osutil.posixfile() to override the name attribute to reflect the underlying file name. cCs||_||_dSr)r(r)rr(rrrrrs zfdproxy.__init__cCrrrrrrrrs zfdproxy.__enter__cCrrr)rr! exc_value tracebackrrrr r$zfdproxy.__exit__cCs t|jSr)iterrrrrr__iter__ zfdproxy.__iter__cCr%rr&r'rrrr)r*zfdproxy.__getattr__N) rArBrCrDrrr rJr)rrrrrFs rFrc Csz&t|||}tjrt||}d|vr|dtjd|vr$t|WS|WSt y@}zt |j dt ||jfd}~ww)z/Open a file with even more POSIX-like semanticsar+z%s: %sN)r posixfiler ispy3rFr-r.SEEK_ENDr WindowsErrorIOErrorerrnor strfromlocalstrerror)r(mode bufferingrerrrrrrPs  rPcCs z|WStyYdSwNF)isattyAttributeError)rrrr_isattys   r^cCsjd} t}|dks|dkrn|dkrt|dkr!|dd}n||}qtdtdt|S) zPrompt for password with echo off, using Windows getch(). This shouldn't be called directly- use ``ui.getpass()`` instead, which checks if the session is interactive first. T  NrM)msvcrtgetwchKeyboardInterruptputwchr unitolocal)pwcrrr get_passwords  rkc@rE) winstdoutzSome files on Windows misbehave. When writing to a broken pipe, EINVAL instead of EPIPE may be raised. When writing too many bytes to a console at the same, a "Not enough space" error may happen. Python 3 already works around that. cCs||_tj o t||_dSr)rr rQr^throttlerrrrrszwinstdout.__init__cCr%r)rr)rkeyrrrr)r*zwinstdout.__getattr__cCs&z|jWdStyYdSwr)rcloserTrrrrros  zwinstdout.closec Csz0|js |j|WSd}t|}d}||kr.||}|j||||}||ksWdSWdStyQ}z|jdkrCt|sC|ttj dd}~ww)Ni>r Broken pipe) rmrr8lenrTrUr lasterrorwaspipeerrorroEPIPE)rslimitlstartendinstrrrr8s$ zwinstdout.writec Cs>z|jWSty}z t|sttjdd}~ww)Nrp)rflushrTr rrrUrs)rryrrrrzs   zwinstdout.flushN) rArBrCrDrr)ror8rzrrrrrls rlcCdSNTrrrrr openhardlinks r}cCs(|dd}|ddkr|dd}|S)z>> e = {b'var1': b'v1', b'var2': b'v2', b'var3': b'v3'} >>> # Only valid values are expanded >>> shelltocmdexe(b'cmd $var1 ${var2} %var3% $missing ${missing} %missing%', ... e) 'cmd %var1% %var2% %var3% $missing ${missing} %missing%' >>> # Single quote prevents expansion, as does \$ escaping >>> shelltocmdexe(b"cmd '$var1 ${var2} %var3%' \$var1 \${var2} \\", e) 'cmd "$var1 ${var2} %var3%" $var1 ${var2} \\' >>> # $$ is not special. %% is not special either, but can be the end and >>> # start of consecutive variables >>> shelltocmdexe(b"cmd $$ %% %var1%%var2%", e) 'cmd $$ %% %var1%%var2%' >>> # No double substitution >>> shelltocmdexe(b"$var1 %var1%", {b'var1': b'%var2%', b'var2': b'boom'}) '%var1% %var1%' >>> # Tilde expansion >>> shelltocmdexe(b"~/dir ~\dir2 ~tmpfile \~/", {}) '%USERPROFILE%/dir %USERPROFILE%\\dir2 ~tmpfile ~/' c3s|]}|vVqdSrr).0rjrrr sz shelltocmdexe..s$'~s_-rr'N"%$r{}s${~)rrs %USERPROFILE%r)rr) anyr sysbytesstring ascii_lettersdigitsrqindex ValueErrorget)renvvarcharsresrpathlenrjvarrrr shelltocmdexels                    MrcCsPtdur tdatdurtdja|r t|s t|s |Sdtd|S)a >>> shellquote(br'C:\Users\xyz') '"C:\\Users\\xyz"' >>> shellquote(br'C:\Users\xyz/mixed') '"C:\\Users\\xyz/mixed"' >>> # Would be safe not to quote too, since it is all double backslashes >>> shellquote(br'C:\\Users\\xyz') '"C:\\\\Users\\\\xyz"' >>> # But this must be quoted >>> shellquote(br'C:\\Users\\xyz/abc') '"C:\\\\Users\\\\xyz/abc"' Ns (\\*)("|\\$)s[^a-zA-Z0-9._:/-]s"%s"s\1\1\\\2)_quotererecompile_needsshellquotesearchsubrtrrrrs  rcCs$|dr|dr|ddS|S)NrrrM)rendswithrrrr_unquote s rcCstttj|ddS)z3Parse a command string in cmd.exe way (best-effort)F)posix)r maplistr shlexsplitrrrr shellsplitsrcCr{r|rstrrrisownerr~rcstjdd}dd|tjDtj |dvr#dgfdd}tj |vr2||Stjd dtjD]}|tj ||}|d urP|Sq=|tj tj |S) a*Find executable for command searching like cmd.exe does. If command is a basename then PATH is searched for command. PATH isn't searched if command is an absolute or relative path. An extension from PATHEXT is found and added if not present. If command isn't found None is returned.sPATHEXTs.COM;.EXE;.BAT;.CMDcSsg|]}|qSrr)rextrrr #szfindexe..rrcs*D]}||}tj|r|SqdS)z:Will append extension (if needed) and return existing fileN)r.rexists) pathcommandr executablepathextsrr findexisting's  zfindexe..findexistingsPATHN)renvironrrsplitr ospathsepr.rsplitextrjoin expanduser expandvars)commandpathextrrrrrrfindexes  rc #si}tj|D]X}t|}tj|\}}|sd}||d}|durYzfddt|dD}Wn tyR}z|j t j t j t j fvrFi}WYd}~nd}~ww| ||}||dVqdS)zStat each file in files. Yield each stat, or None if a file does not exist or has a type we don't care about. Cluster and cache stat per directory to minimize number of OS stat calls..Ncs*i|]\}}}|jtvrt||qSr)st_mode _wantedkindsr)rnkrtgetkindrr Ks zstatfiles..T)statS_IFMTrr.rrrlistdirOSErrorrUENOENTEINVALENOTDIR setdefault)filesdircachenfdirbasecachedmaprZrrr statfiles<s.    rcCs|s ttSdS)ziReturn the name of the user with the given uid. If uid is None, return the name of the current user.N)r fsencodegetpassgetuser)uidrrrusernameZsr cCr{)zkReturn the name of the group with the given gid. If gid is None, return the name of the current group.Nr)gidrrr groupnamecsr c CsDt|}zt|}Wnty}zt|d}~wwt|Sr)r fsdecoder.readlinkrr]r)pathnamerlinkerrrrjs  rc Cst|rdSt|tj|\}}|stj|\}}|rJ|rLzt|r)WdSt|Wn ttfy;YdSwtj|\}}|rN|s!dSdSdSdS)zspecial version of os.removedirs that does not remove symlinked directories or junction points if they actually contain filesN)rr.rmdirrrrr)r(headtailrrr removedirsws  rc Cs\z t||WdSty-}z|jtjkrt|t||WYd}~dSd}~ww)z=atomically rename file src to dst, replacing dst if it existsN)r.renamerrUEEXISTunlink)rrrrrrrs rcCs ddtjgtjddDS)NcSsg|]}t|qSr)r strtolocal)rargrrrrszgethgcmd..r)sysrargvrrrrgethgcmds rcCstr)KeyError)r(rrr groupmembersrcCr{r[r)rrrrisexecr~r!c@seZdZddZddZdS) cachestatcCrrr)rrrrrrr~zcachestat.__init__cCr{r[rrrrr cacheabler~zcachestat.cacheableN)rArBrCrr#rrrrr"s r"c Cs|dur tjtjf}n t|ttfs|f}|D]?}z4t|t|!}|r+t|p,|}t ||d}t |WdWS1sGwYWqt yVYqwdS)a)Look up a key/value name in the Windows registry. valname: value name. If unspecified, the default value for the key is used. scope: optionally specify scope for registry lookup, this can be a sequence of scopes to look up in order. Default (CURRENT_USER, LOCAL_MACHINE). Nr) winregHKEY_CURRENT_USERHKEY_LOCAL_MACHINErlisttupleOpenKeyrrV QueryValueExrhEnvironmentError)rnvalnamescoperthkeyr(valrrr lookupregs * r0TcCr{)z(check whether a stat result is a symlinkFrrrrr statislinkr r1cCr{)z1check whether a stat result is an executable fileFrrrrr statisexecr r2cCstrNotImplementedError)fdsrrrpollsr6cCs<g} t|}|s n||}|sn||qd|S)z$Read all available data from a pipe.Tr)r peekpiper>appendr)pipechunkssizertrrrreadpipes    r<cCstd)Nzunsupported platformr3)sockrrrrbindunixsocketsr>)rLrMr[r)NN)d __future__rrUrrdr.rrrri18nrr rr_rrrr _winregr$CloseKey ImportError importmodr getvolumenamegetfsmountpoint getfstyper hidewindowmakedirnlinksoslink samedevicesamefilesetsignalhandler spawndetachedrrtestpidrumaskrr rFrPrr^rkrlr}rrrrrrrrrrrrrrr normcasespecsr normcasespec upperfallbacknormcasefallbackrrrrrrrrrS_IFREGS_IFLNKrrr r rrrrrr!r"r0 expandglobsr1r2r6r<r>rrrrs       M 2   }