o Ƌ|cv @sHdZdZdZdZddlmZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZzddlZWn eyCdZYnwzddlZWn eyUdZYnweefZzeef7ZWn eyjYnwgdZdZd Zed Zd Zd Zd Zd ZdZ dZ!dZ"dZ#dZ$dZ%dZ&dZ'dZ(dZ)dZ*dZ+dZ,dZ-dZ.dZ/dZ0dZ1dZ2e2Z3e!e"e#e$e'e(e)e%e&e*e+e,f Z4e!e"e)e,fZ5e*e+e,fZ6dZ7hd Z8e9e9e9e:e:e:d!Z;ejZ=d$d%Z?d&d'Z@d(d)ZAd*e3fd+d,ZBd-d.ZCdedfd/d0ZDd1d2ZEGd3d4d4eFZGGd5d6d6eGZHGd7d8d8eGZIGd9d:d:eGZJGd;d<dd>eGZLGd?d@d@eLZMGdAdBdBeLZNGdCdDdDeLZOGdEdFdFeLZPGdGdHdHeLZQGdIdJdJZRGdKdLdLZSGdMdNdNeTZUGdOdPdPeTZVGdQdRdRe jWZXGdSdTdTeTZYGdUdVdVeTZZdWdXZ[eZjZdYdZZ\e]d[kre\dSdS)\z,Read from and write to tar format archives. z0.9.0u"Lars Gustäbel (lars@gustaebel.de)u4Gustavo Niemeyer, Niels Gustäbel, Richard Townsend.)openN)TarFileTarInfo is_tarfileTarError ReadErrorCompressionError StreamError ExtractError HeaderErrorENCODING USTAR_FORMAT GNU_FORMAT PAX_FORMATDEFAULT_FORMATrisustar sustar00d01234567LKSxgX)pathlinkpathsizemtimeuidgidunamegname>r%r&r,r+)Zatimectimer(r)r*r'ntutf-8cCs(|||}|d||t|tS)z8Convert a string to a null-terminated bytes object. N)encodelenNUL)slengthencodingerrorsr7$/opt/conda/lib/python3.10/tarfile.pystns r9cCs*|d}|dkr|d|}|||S)z8Convert a null-terminated bytes object to a string. rN)finddecode)r3r5r6pr7r7r8ntss   r>cCs|ddvr2d}tt|dD]}|dK}|||d7}q|ddkr0dt|d| }|Szt|dd}t|p?d d}W|StyOtd w) z/Convert a number field to a python number. r)r#r@asciistrict0invalid header)ranger1r>intstrip ValueErrorInvalidHeaderError)r3nir7r7r8ntis    rNrAcCs|}t|}d|krd|dkr%nntd|d|fdt}|S|tkrmd|d |kr|j d||df|j d||||f||7}||}q,||jkre|j d||jdfdSdS)NrrFT) roffsetr'positionrjrr map_indexmapr)rrrr'Z blockinfoZlastposZrealposr7r7r8r^s*    z_FileInFile.__init__cCsdSrr7rr7r7r8rwz_FileInFile.flushcCdSNTr7rr7r7r8readablezrz_FileInFile.readablecCrNFr7rr7r7r8writable}rz_FileInFile.writablecCs |jSr)rseekablerr7r7r8r z_FileInFile.seekablecCr)z*Return the current file position. )rrr7r7r8rrz_FileInFile.tellcCs|tjkrtt|d|j|_|jS|tjkr4|dkr't|j|d|_|jSt|j||j|_|jS|tjkrItt|j||jd|_|jStd)z(Seek to a position in the file. rzInvalid argument) ioSEEK_SETminmaxr'rSEEK_CURSEEK_ENDrJ)rrwhencer7r7r8rs   z_FileInFile.seekc Cs|dur |j|j}n t||j|j}d}|dkr |j|j\}}}}||jkr0|kr3nnn|jd7_|jt|jkrEd|_qt|||j}|rp|j||j||j|}t||krkt d||7}n|t |7}||8}|j|7_|dks|S)z!Read data from the file. NrrTr#rZ) r'rrrrr1rrr^rr2) rr'rXdatastartstoprr4rfr7r7r8r^s2    z_FileInFile.readcCs&|t|}||dt|<t|Sr)r^r1)rrfrXr7r7r8readintosz_FileInFile.readintocCs d|_dSr)rrr7r7r8rrz_FileInFile.closer)rqrrrsrtrrrrrrrrrr^rrr7r7r7r8rXs   rcseZdZfddZZS) ExFileObjectcs&t|j|j|j|j}t|dSr)rr offset_datar'sparsesuperr)rtarfiletarinfor __class__r7r8rs zExFileObject.__init__)rqrrrsr __classcell__r7r7rr8rsrc@s eZdZdZedxidddddddd d d d d ddddddddddddddddddd d!d"d#d$d%d&d%d'd%Zdyd)d*Zed+d,Zej d-d,Zed.d/Z e j d0d/Z d1d2Z d3d4Z e ed5fd6d7Zd8d9Zd:d;Zdd?Zd@dAZedBdCZedDdEZedFdGZedHdIZedJdKZedLdMZdNdOZdPdQZdRdSZdTdUZ dVdWZ!dXdYZ"dZd[Z#d\d]Z$d^d_Z%d`daZ&dbdcZ'dddeZ(dfdgZ)dhdiZ*djdkZ+dldmZ,dndoZ-dpdqZ.drdsZ/dtduZ0dvdwZ1d%S)zraInformational class which holds the details about an archive member given by a tar header block. TarInfo objects are returned by TarFile.getmember(), TarFile.getmembers() and TarFile.gettarinfo() and are usually created internally. rzName of the archive member.rzPermission bits.r)z6User ID of the user who originally stored this member.r*z7Group ID of the user who originally stored this member.r'zSize in bytes.r(zTime of last modification.chksumzHeader checksum.typezFile type. type is usually one of these constants: REGTYPE, AREGTYPE, LNKTYPE, SYMTYPE, DIRTYPE, FIFOTYPE, CONTTYPE, CHRTYPE, BLKTYPE, GNUTYPE_SPARSE.linknamezcName of the target file name, which is only present in TarInfo objects of type LNKTYPE and SYMTYPE.r+z User name.r,z Group name.devmajorzDevice major number.devminorzDevice minor number.rzThe tar header starts here.rzThe file's data starts here. pax_headerszMA dictionary containing key-value pairs of an associated pax extended header.rzSparse member information.rN_sparse_structs _link_targetrcCsj||_d|_d|_d|_d|_d|_d|_t|_d|_ d|_ d|_ d|_ d|_ d|_d|_d|_i|_dS)zXConstruct a TarInfo object. name is the optional name of the member. irrN)rrr)r*r'r(rREGTYPErrr+r,rrrrrrrrr7r7r8rs" zTarInfo.__init__cCr)z(In pax headers, "name" is called "path".rrr7r7r8r%rz TarInfo.pathcC ||_dSrr rr7r7r8r%  cCr)z0In pax headers, "linkname" is called "linkpath".rrr7r7r8r&rzTarInfo.linkpathcCr rr )rrr7r7r8r&r cCsd|jj|jt|fS)Nz<%s %r at %#x>)rrqridrr7r7r8__repr__szTarInfo.__repr__cCsl|j|jd@|j|j|j|j|j|j|j|j |j |j |j d }|dt kr4|dds4|dd7<|S)z9Return the TarInfo's attributes as a dictionary. ) rrr)r*r'r(rrrr+r,rrrr/)rrr)r*r'r(rrrr+r,rrDIRTYPEr)rinfor7r7r8get_infos"zTarInfo.get_infosurrogateescapecCsP|}|tkr||||S|tkr||||S|tkr$|||Std)zrrr)r*r'r(rrrr+r,rrAREGTYPErrGNUTYPE_SPARSErGrJrboolrisdirrstrip GNU_TYPES)r0rXr5r6robjrrstructsrMrnumbytes isextendedorigsizer7r7r8frombufs\          zTarInfo.frombufcCs8|jt}|||j|j}|jt|_||S)zOReturn the next TarInfo object from TarFile object tarfile. ) rr^rrXr5r6rr _proc_member)r0rrXrSr7r7r8 fromtarfile]s  zTarInfo.fromtarfilecCsP|jttfvr ||S|jtkr||S|jtttfvr#| |S| |S)zYChoose the right processing method depending on the type and call it. ) rr#r" _proc_gnulongrN _proc_sparser-r/SOLARIS_XHDTYPE _proc_pax _proc_builtin)rrr7r7r8rYrs     zTarInfo._proc_membercCsh|j|_|j}|s|jtvr|||j7}||_| |j |j |j | r2|jd|_|S)zfProcess a builtin type or an unknown type which will be treated as a regular file. r)rrrisregrSUPPORTED_TYPES_blockr'r_apply_pax_inforr5r6rPrrQ)rrrr7r7r8r_s zTarInfo._proc_builtinc Cs|j||j}z||}Wnty$}ztt|dd}~ww|j|_|j t kr8t ||j |j |_n|j tkrFt ||j |j |_|rQ|jd|_|S)zSProcess the blocks that hold a GNU longname or longlink member. Nr)rr^rbr'rZr ryr+rrr#r>r5r6rr"rrP removesuffix)rrrXnextrr7r7r8r[s  zTarInfo._proc_gnulongc Cs|j\}}}|`|rS|jt}d}tdD]4}zt|||d}t||d|d} Wn ty:Ynw|rF| rF||| f|d7}qt|d}|s ||_ |j |_ |j | |j |_||_ |S)z8Process a GNU sparse header plus extra headers. rr%rKi)rrr^rrGrNrJrrOrrrrbr'r) rrrTrVrWrXrrMrrUr7r7r8r\s,       zTarInfo._proc_sparsec Cs,|j||j}|jtkr|j}n|j}t d|}|dur,| d d|d<| d}|dkr9|j }nd}td}d} |||}|sLnN|\} } t| } | dkr^td ||d d|d| d} || dd|j} | tvr|| ||j |j} n || dd|j} | || <|| 7}qCz||} Wnty} ztt| dd} ~ wwd |vr|| |n!d |vr|| ||n| ddkr| ddkr|| |||jtt fvr| !||j |j|j"| _"d|vr| j#}| $s | jt%vr|| | j7}||_"| S)zVProcess an extended or global header as described in POSIX.1-2008. s\d+ hdrcharset=([^\n]+)\nNr#r/ hdrcharsetZBINARYs(\d+) ([^=]+)=rTrFr$GNU.sparse.mapGNU.sparse.sizezGNU.sparse.major1zGNU.sparse.minorrEr')&rr^rbr'rr/rr&researchgroupr<r5r5compilematchgroupsrHrKrir_decode_pax_fieldr6PAX_NAME_FIELDSrZr ryr+_proc_gnusparse_01_proc_gnusparse_00_proc_gnusparse_10r-r]rcrrr`ra)rrrXrrorgr5regexrr4r@rArerrr7r7r8r^sp       $    zTarInfo._proc_paxcCshg}td|D] }|t|dqg}td|D] }|t|dqtt|||_dS)z?Process a GNU tar extended sparse header, version 0.0. s\d+ GNU.sparse.offset=(\d+)\nr#s\d+ GNU.sparse.numbytes=(\d+)\nN)rkfinditerrrHrmlistzipr)rrerrXoffsetsrorUr7r7r8rt4szTarInfo._proc_gnusparse_00cCs@dd|ddD}tt|ddd|ddd|_dS)z?Process a GNU tar extended sparse header, version 0.1. cSsg|]}t|qSr7)rH).0xr7r7r8 Bsz.TarInfo._proc_gnusparse_01..rh,Nr$r#)r2rxryr)rrerrr7r7r8rs?s(zTarInfo._proc_gnusparse_01cCsd}g}|jt}|dd\}}t|}t||dkrAd|vr*||jt7}|dd\}}|t|t||dks|j|_t t |ddd|ddd|_ dS)z?Process a GNU tar extended sparse header, version 1.0. Nr=r#r$) rr^rr2rHr1rrrrxryr)rrerrfieldsrrXnumberr7r7r8ruEs  (zTarInfo._proc_gnusparse_10c Cs|D]T\}}|dkrt|d|q|dkr t|dt|q|dkr-t|dt|q|tvrX|tvrIzt||}Wn tyHd}Ynw|dkrR|d}t|||q||_dS) zoReplace fields with supplemental information from a previous pax extended or global header. zGNU.sparse.namer%rir'zGNU.sparse.realsizerrN) r>setattrrH PAX_FIELDSPAX_NUMBER_FIELDSrJrQr&r)rrr5r6r@rAr7r7r8rcUs&   zTarInfo._apply_pax_infocCs.z||dWSty|||YSw)z1Decode a single field from a pax record. rD)r<UnicodeDecodeError)rrAr5Zfallback_encodingZfallback_errorsr7r7r8rqls  zTarInfo._decode_pax_fieldcCs"t|t\}}|r |d7}|tS)z_Round up a byte count by BLOCKSIZE and return it, e.g. _block(834) => 1024. r#)r]r)rrLrdrer7r7r8rbtszTarInfo._blockcCs |jtvSz4Return True if the Tarinfo object is a regular file.)r REGULAR_TYPESrr7r7r8r`}r z TarInfo.isregcCs|Sr)r`rr7r7r8isfileszTarInfo.isfilecC |jtkS)z!Return True if it is a directory.)rrrr7r7r8rPr z TarInfo.isdircCr)z%Return True if it is a symbolic link.)rSYMTYPErr7r7r8issymr z TarInfo.issymcCr)z!Return True if it is a hard link.)rLNKTYPErr7r7r8islnkr z TarInfo.islnkcCr)z(Return True if it is a character device.)rr6rr7r7r8ischrr z TarInfo.ischrcCr)z$Return True if it is a block device.)rr7rr7r7r8isblkr z TarInfo.isblkcCr)zReturn True if it is a FIFO.)rFIFOTYPErr7r7r8isfifor zTarInfo.isfifocCs |jduSr)rrr7r7r8issparserzTarInfo.issparsecCs|jtttfvS)zCReturn True if it is one of character device, block device or FIFO.)rr6r7rrr7r7r8isdevsz TarInfo.isdevr7)r)2rqrrrsrtdict __slots__rpropertyr%setterr&rrrr rrrr classmethodr1r staticmethodrr:r!r,rXrZrYr_r[r\r^rtrsrurcrqrbr`rrPrrrrrrrr7r7r7r8rs        :  #   2 >  h   rc@seZdZdZdZdZdZdZeZ e Z dZ e ZeZ    d\dd Zedddefd d Zed]d d Zed^ddZed^ddZed_ddZdddddZddZddZddZd d!Zd`d"d#Zdadd%d&d'Zdbdd(d)d*Z dcd+d,Z!dddd.d/d0Z"dedd.d2d3Z#d4d5Z$ $ dfd6d7Z%d8d9Z&d:d;Z'dd?Z)d@dAZ*dBdCZ+dDdEZ,dFdGZ-dHdIZ.dJdKZ/dgdLdMZ0dNdOZ1dcdPdQZ2dRdSZ3dTdUZ4dVdWZ5dXdYZ6dZd[Z7dS)hrz=The TarFile Class provides an interface to tar archives. rFr#Nr}rc Csddddd}||vrtd||_|||_|s4|jdkr*tj|s*d|_d|_t||j}d |_n |d urHt|d rHt |j t t frH|j }t|d rQ|j|_d |_|r\tj |nd |_ ||_|d uri||_|d urp||_|d urw||_|d ur~||_|d ur||_| |_| d ur|jtkr| |_ni|_| d ur| |_| d ur| |_| |_d |_g|_d |_|j|_i|_z||jdkrd |_ |!|_ |jdkr |j"|jz|j#|}|j$|Wn#t%y|j"|jYnt&y}zt't |d d }~wwq|jdvr2d |_|jr5|j(|j)}|j*||jt+|7_Wd SWd SWd S|jsD|j,d |_)aOpen an (uncompressed) tar archive `name'. `mode' is either 'r' to read from an existing archive, 'a' to append data to an existing file or 'w' to create a new file overwriting an existing one. `mode' defaults to 'r'. If `fileobj' is given, it is used for reading or writing data. If it can be determined, `mode' is overridden by `fileobj's mode. `fileobj' is not closed, when TarFile is closed. rbzr+bwbxbr}ar~r|!mode must be 'r', 'a', 'w' or 'x'rr~FNrrTr}rr~r|)-rJr_moderr%exists bltn_openrrr(rr+rOabspathrrSr dereference ignore_zerosr5r6rrdebug errorlevel copybufsizermembers_loadedrrinodes firstmemberrerrZrrxr rr1r&r_r1r)rrrrrSrrrr5r6rrrrmodesrrXr7r7r8rs             zTarFile.__init__c s|s|std|dvrofdd}g}tj|dD]G}tj|} |dur-|} z | |d|fi|WSttfyb} z|d|d | |durX|| WYd} ~ qd} ~ wwd |} td | d |vr| d d \} }| p~d} |pd}|jvrtj|} ntd|| || |fi|Sd|vr| dd \} }| pd} |pd}| dvrtdt || |||}z || |fi|}Wn| d|_ |S|dvrj|||fi|Std)aOpen a tar archive for reading, writing or appending. Return an appropriate TarFile class. mode: 'r' or 'r:*' open for reading with transparent compression 'r:' open for reading exclusively uncompressed 'r:gz' open for reading with gzip compression 'r:bz2' open for reading with bzip2 compression 'r:xz' open for reading with lzma compression 'a' or 'a:' open for appending, creating the file if necessary 'w' or 'w:' open for writing without compression 'w:gz' open for writing with gzip compression 'w:bz2' open for writing with bzip2 compression 'w:xz' open for writing with lzma compression 'x' or 'x:' create a tarfile exclusively without compression, raise an exception if the file is already created 'x:gz' create a gzip compressed tarfile, raise an exception if the file is already created 'x:bz2' create a bzip2 compressed tarfile, raise an exception if the file is already created 'x:xz' create an lzma compressed tarfile, raise an exception if the file is already created 'r|*' open a stream of tar blocks with transparent compression 'r|' open an uncompressed stream of tar blocks for reading 'r|gz' open a gzip compressed stream of tar blocks 'r|bz2' open a bzip2 compressed stream of tar blocks 'r|xz' open an lzma compressed stream of tar blocks 'w|' open an uncompressed stream for writing 'w|gz' open a gzip compressed stream for writing 'w|bz2' open a bzip2 compressed stream for writing 'w|xz' open an lzma compressed stream for writing znothing to open)r}r:*csj|dkS)Ntaropen) OPEN_METH)rr0r7r8not_compressedXrz$TarFile.open..not_compressedkeyNr}z - method z:  z'file could not be opened successfully: :r#rr|r|zmode must be 'r' or 'w'Frzundiscernible mode)rJsortedrrjrrrrrrr2rrrr)r0rrrrckwargsrZ error_msgsrfuncZ saved_posrZerror_msgs_summaryfilemodestreamrr7rr8r.sZ%      z TarFile.opencKs$|dvrtd||||fi|S)zCOpen uncompressed tar archive name for reading or writing. rr)rJ)r0rrrrr7r7r8rszTarFile.taropenrc Ks|dvrtdzddlm}Wn tytddwz |||d||}WntyB}z|dur=|dkr=td |d}~wwz |j|||fi|}Wn!tyj}z||dkretd |d}~w|d |_ |S) zkOpen gzip compressed tar archive name for reading or writing. Appending is not allowed. r}r~r|mode must be 'r', 'w' or 'x'r)GzipFilezgzip module is not availableNrfr}rF) rJgziprrrrrrrr) r0rrr compresslevelrrrrr7r7r8gzopens8    zTarFile.gzopenc Ks|dvrtdzddlm}Wn tytddw||p!|||d}z |j|||fi|}Wn#ttfyO}z||dkrJt d |d}~w|d |_ |S) zlOpen bzip2 compressed tar archive name for reading or writing. Appending is not allowed. rrr)BZ2FilerN)rr}znot a bzip2 fileF) rJrrrrrrEOFErrorrrr) r0rrrrrrrrr7r7r8bz2opens*   zTarFile.bz2openc Ks|dvrtdz ddlm}m}Wn tytddw||p#|||d}z |j|||fi|}Wn#|tfyQ} z||dkrLt d | d} ~ w|d |_ |S) zkOpen lzma compressed tar archive name for reading or writing. Appending is not allowed. rrr)LZMAFilerrN)presetr}znot an lzma fileF) rJrrrrrrrrrr) r0rrrrrrrrrr7r7r8xzopens*   zTarFile.xzopenrrrr)rrrrcCs|jrdSd|_z;|jdvr7|jttd|jtd7_t|jt\}}|dkr7|jtt|W|j sB|j dSdS|j sM|j ww)zlClose the TarFile. In write-mode, two finishing zero blocks are appended to the archive. NTrr$r) rrrr_r2rrr] RECORDSIZErr)rrdrer7r7r8rs   z TarFile.closecCs(||d}|durtd||S)aReturn a TarInfo object for member `name'. If `name' can not be found in the archive, KeyError is raised. If a member occurs more than once in the archive, its last occurrence is assumed to be the most up-to-date version. rNzfilename %r not found) _getmemberrQKeyError)rrrr7r7r8 getmember s zTarFile.getmembercCs||js ||jS)zReturn the members of the archive as a list of TarInfo objects. The list has the same order as the members in the archive. )_checkr_loadrrr7r7r8 getmembersszTarFile.getmemberscCsdd|DS)zReturn the members of the archive as a list of their names. It has the same order as the list returned by getmembers(). cSsg|]}|jqSr7r )r{rr7r7r8r}&sz$TarFile.getnames..)rrr7r7r8getnames"szTarFile.getnamesc CsJ|d|dur |j}|dur|}tj|\}}|tjd}|d}|}||_ |dur@|j s:t |}n t |}nt |}d}|j}t |r|j|jf} |j ss|jdkrs| |jvrs||j| krst} |j| }n;t} | dr~||j| <n/t |rt} n't |rt} nt |rt} t|}nt |rt} n t |rt!} ndS||_||_"|j#|_$|j%|_&| tkr|j'|_(nd|_(|j)|_*| |_+||_,t-rz t-.|j$d|_/Wn t0yYnwt1rz t12|j&d|_3Wn t0yYnw| tt!fvr#t4tdr#t4tdr#t5|j6|_7t8|j6|_9|S) aCreate a TarInfo object from the result of os.stat or equivalent on an existing file. The file is either named by `name', or specified as a file object `fileobj' with a file descriptor. If given, `arcname' specifies an alternative name for the file in the archive, otherwise, the name is taken from the 'name' attribute of 'fileobj', or the 'name' argument. The name should be a text string. awxNrrr#rmajorminor):rrrr% splitdriverseplstriprrrlstatstatfstatfilenost_modeS_ISREGst_inost_devst_nlinkrrrS_ISDIRrS_ISFIFOrS_ISLNKrreadlinkS_ISCHRr6S_ISBLKr7rst_uidr)st_gidr*st_sizer'st_mtimer(rrpwdgetpwuidr+rgrpgetgrgidr,rrst_rdevrrr) rrarcnamerdrvrZstatresrZstmdinoderr7r7r8 gettarinfo(s                zTarFile.gettarinfoT)rcCs||dur |}|D]n}|rRtt|jtd|jp|j|jp$|jf| s0| r=tdd|j |j fntd|j tdt|jddt|j|r[dnd |rw|rltd |j|rwtd |jtq dS) aPrint a table of contents to sys.stdout. If `verbose' is False, only the names of the members are printed. If it is True, an `ls -l'-like output is produced. `members' is optional and must be a subset of the list returned by getmembers(). Nz%s/%sz%10sz%d,%dz%10dz%d-%02d-%02d %02d:%02d:%02drrrz-> zlink to )rrnrrrr+r)r,r*rrrrr'r localtimer(rrPrrrrm)rverboserrr7r7r8rxs2 z TarFile.listfiltercCsP|d|dur |}|jdur#tj||jkr#|dd|dS|d||||}|dur=|dd|dS|durS||}|durS|dd|dS|rut|d}| ||WddS1snwYdS| r| ||rt t |D]}|j tj||tj||||d qdSdS| |dS) a!Add the file `name' to the archive. `name' may be any type of file (directory, fifo, symbolic link, etc.). If given, `arcname' specifies an alternative name for the file in the archive. Directories are added recursively by default. This can be avoided by setting `recursive' to False. `filter' is a function that expects a TarInfo object argument and returns the changed TarInfo object, if it returns None the TarInfo object will be excluded from the archive. rNr$ztarfile: Skipped %rr#ztarfile: Unsupported type %rztarfile: Excluded %rrr)rrrr%r_dbgrr`raddfilerPrlistdiraddr)rrr recursiverrfr7r7r8rs<    " z TarFile.addcCs|dt|}||j|j|j}|j||jt |7_|j }|durWt ||j|j |dt |j t\}}|dkrN|jtt||d7}|j|t7_|j|dS)aAdd the TarInfo object `tarinfo' to the archive. If `fileobj' is given, it should be a binary file, and tarinfo.size bytes are read from it and added to the archive. You can create TarInfo objects directly, or by using gettarinfo(). rN)rcrr#)rr&rrSr5r6rr_rr1rr\r'r]rr2rr)rrrrXrcrdrer7r7r8rs   zTarFile.addfile. numeric_ownerc Csg}|dur|}|D]}|r||t|}d|_|j||| |dq |jddd||D]@}tj ||j }z|j |||d| ||| ||Wq8tyx}z|jdkrf|dd |WYd}~q8d}~wwdS) aExtract all members from the archive to the current working directory and set owner, modification time and permissions on directories afterwards. `path' specifies a different directory to extract to. `members' is optional and must be a subset of the list returned by getmembers(). If `numeric_owner` is True, only the numbers for user/group names are used and not the names. N set_attrsrcSrrr )rr7r7r8sz$TarFile.extractall..rrr# tarfile: %s)rPrr&rextractsortreverserr%rrchownutimechmodr rr)rr%rr directoriesrdirpathrr7r7r8 extractalls4    zTarFile.extractallrc Cs|dt|tr||}n|}|rtj||j|_ z|j |tj||j ||dWdSt yl}z/|j dkr?|jdurN|dd|jn|dd|j|jfWYd}~dSWYd}~dSd}~wty}z|j dkry|dd|WYd}~dSd}~ww)aExtract a member from the archive to the current working directory, using its full name. Its file information is extracted as accurately as possible. `member' may be a filename or a TarInfo object. You can specify a different directory using `path'. File attributes (owner, mtime, mode) are set unless `set_attrs' is False. If `numeric_owner` is True, only the numbers for user/group names are used and not the names. r}rrNr#rztarfile: %s %r)rr(r+rrrr%rrr_extract_memberrrrfilenamerstrerrorr )rmemberr%rrrrr7r7r8rs2     & zTarFile.extractcCsz|dt|tr||}n|}|s|jtvr!|||S|s)| r;t|j t r3t d| ||SdS)a\Extract a member from the archive as a file object. `member' may be a filename or a TarInfo object. If `member' is a regular file or a link, an io.BufferedReader object is returned. For all other existing members, None is returned. If `member' does not appear in the archive, KeyError is raised. r}z'cannot extract (sym)link as file objectN)rr(r+rr`rra fileobjectrrrrr extractfile_find_link_target)rrrr7r7r8rEs     zTarFile.extractfilecCsV|d}|dtj}tj|}|rtj|st||s'| r4| dd|j |j fn| d|j | rF|||nF|rQ|||n;|r\|||n0|sd|rk|||n!|ss| rz|||n|jtvr|||n||||r||||| s||||||dSdSdS)z\Extract the TarInfo object tarinfo to a physical file called targetpath. rr#z%s -> %sN)rQrrrr%dirnamermakedirsrrrrrr`makefilerPmakedirrmakefiforrmakedevmakelinkrra makeunknownrr r )rr targetpathrr upperdirsr7r7r8res8      zTarFile._extract_membercCs(z t|dWdStyYdSw)z,Make a directory called targetpath. rN)rmkdirFileExistsErrorrrrr7r7r8rs  zTarFile.makedircCs|j}||j|j}t|d?}|jdur7|jD]\}}||t|||t|q||j| nt|||jt|WddSWddS1sSwYdS)z'Make a file called targetpath. rN) rrrrrrr\rr'truncate)rrrsourcerctargetrr'r7r7r8rs      "zTarFile.makefilecCs"||||dd|jdS)zYMake a file from a TarInfo object with an unknown type at targetpath. r#z9tarfile: Unknown file type %r, extracted as regular file.N)rrrr!r7r7r8rs  zTarFile.makeunknowncCs ttdr t|dStd)z'Make a fifo called targetpath. mkfifozfifo not supported by systemN)rrr%r r!r7r7r8rs zTarFile.makefifocCs^ttdr ttdstd|j}|r|tjO}n|tjO}t||t |j |j dS)zYnwz| rSttdrSt |||WdSt|||WdStym}ztd|d}~wwdSdS)zSet owner of targetpath according to tarinfo. If numeric_owner is True, use .gid/.uid instead of .gname/.uname. If numeric_owner is False, fall back to .gid/.uid when the search based on name fails. geteuidrr$lchownzcould not change ownerN)rrr.r*r)rgetgrnamr,rrgetpwnamr+rr/rrr )rrrrgurr7r7r8rs6   z TarFile.chownc Cs8z t||jWdSty}ztd|d}~ww)zASet file permissions of targetpath according to tarinfo. zcould not change modeN)rr rrr rrrrr7r7r8r  s  z TarFile.chmodc CsLttdsdSz t||j|jfWdSty%}ztd|d}~ww)zBSet modification time of targetpath according to tarinfo. r Nz"could not change modification time)rrr r(rr r4r7r7r8r  s  z TarFile.utimec Cs:|d|jdur|j}d|_|S|j|jkr-|j|jd|jds-tdd} z|j |}Wnt ye}z!|j r[| dd|j|f|jt 7_WYd}~q/WYd}~nd}~wty}z-|j r| dd|j|f|jt 7_WYd}~q/|jdkrtt|dWYd}~nqd}~wty|jdkrtd dYn[ty}z|jdkrtt|dWYd}~nBd}~wty}ztt|dd}~wty }zzddl}t||jrtd |d|ty|wd}~ww |dur|j||Sd|_|S) zReturn the next member of the archive as a TarInfo object, when TarFile is opened for reading. Return None if there is no more available. ZraNr#rZTr$z0x%X: %srz empty filez zlib error: )rrrrrrr^rrrZrxrrrrKr+rvrwry Exceptionrr(rrrrr)rmrrrr7r7r8re st                z TarFile.nextcCsn|}|dur|d||}|rtj|}t|D]}|r)tj|j}n|j}||kr4|SqdS)z}Find an archive member by name from bottom to top. If tarinfo is given, it is used as the starting point. N)rindexrr%normpathreversedr)rrr normalizerr member_namer7r7r8rX s  zTarFile._getmembercCs |}|dur nqd|_dS)zWRead through the entire archive file and look for readable members. TN)rer)rrr7r7r8ro s  z TarFile._loadcCs>|jr td|jj|dur|j|vrtd|jdSdS)znCheck if TarFile is still open, and if the operation's mode corresponds to TarFile's mode. z %s is closedNzbad operation for mode %r)rrrrqr)rrr7r7r8ry s zTarFile._checkcCs`|rdtdtj|j|jf}d}n|j}|}|j||dd}|dur.t d||S)zZFind the target member of a symlink or hardlink member in the archive. rNT)rr:zlinkname %r not found) rrrrr%rrrrr)rrrlimitrr7r7r8r s  zTarFile._find_link_targetccs|jr |jEdHdSd}|jdur|}|d7}|V |t|jkr,|j|}n|js;|}|s:d|_dSndS|d7}|Vq)z$Provide an iterator object. Nrr#T)rrrrer1)rr7rr7r7r8__iter__ s,   zTarFile.__iter__cCs ||jkrt|tjddSdS)z.Write debugging output to sys.stderr. fileN)rrmrkstderr)rlevelmsgr7r7r8r s z TarFile._dbgcCs ||Sr)rrr7r7r8 __enter__ szTarFile.__enter__cCs.|dur |dS|js|jd|_dSr)rrrr)rrrA tracebackr7r7r8__exit__ s    zTarFile.__exit__) Nr}NNNNNNrNNNN)r}N)r}Nr)r}NN)NNN)Trr)rN)rT)TFr)8rqrrrsrtrrrrrrSr r5r6rrrrrrrrrrrrrrrrrrrxrrr rrrrrrrrrrr r rerrrrr=rrCrEr7r7r7r8rs~ o]       c 3(&  1  ?   rcCsBzt|dr t|d}nt|}|WdSty YdSw)zReturn True if name points to a tar archive that we are able to handle, else return False. 'name' should be a string, file, or file-like object. r^)rTF)rrrr)rrr7r7r8r s   rc Cs6ddl}d}|j|d}|jdddddd |jd d }|jd d ddd|jdddddd|jdddddd|jddddd|}|jdur|j}t|rt|d}|t |t j dWdn1sowY|j rt d |dSdS|d!d" |dS|jdur|j}t|rt|d#}|j|j d$WddS1swYdS|d!d" |dS|jdur9t|jd!kr|jd}tj}nt|jd%kr|j\}}n|d!|t|r.t|d#}|j|d&Wdn 1s wY|j r,|d'kr d( |} nd) ||} t | dSdS|d!d" |dS|jdur|jd} tj| \} } d*d*d+d+d,d,d,d,d-} | | vrcd.| | nd/}|j}t| |}|D]}||qqWdn 1swY|j rt d0 | dSdSdS)1Nrz3A simple command-line interface for tarfile module.) descriptionz-vz --verbose store_trueFzVerbose output)actiondefaulthelpT)requiredz-lz--list zShow listing of a tarfile)metavarrJz-ez --extract+)rLz zExtract tarfile into target dir)nargsrMrJz-cz--create)zzzCreate tarfile from sourcesz-tz--testzTest if a tarfile is validr}r>z{!r} is a tar archive.r#z{!r} is not a tar archive. r)rr$)r%rz{!r} file is extracted.z+{!r} file is extracted into {!r} directory.rrr)rz.tgzz.xzz.txzz.bz2z.tbzz.tbz2z.tb2zw:r~z{!r} file created.)argparseArgumentParser add_argumentadd_mutually_exclusive_group parse_argstestrrrrmrkr@rrSexitrxrrr1rcurdir format_helpr createpopr%splitextr)rPrFparserrmargsr`rZtfrWrBZtar_name_extZ compressionsZtar_modeZ tar_files file_namer7r7r8main s          "          ra__main__)^rtversion __author__ __credits__builtinsrrrkrrr[rrrVr&rkrrrAttributeErrorNotImplementedErrorr-r NameError__all__r2rrr rrrr3rrMrrr6r7rrZCONTTYPEr#r"rNr-r/r]r rrrrarrRrrrr)rHrrr getfilesystemencodingr9r>rNrTrYr\rnr5rr rrr r rvrwrxrKryr{robjectrrBufferedReaderrrrrrarqr7r7r7r8s        sh _. W