o =?h @sdZdZdZdZddlmZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZddlZzddlZWn eyGdZYnwzddlZWn eyYdZYnweefZzeef7ZWn eynYnwgdZdZd Zed Zd Zd Zd Zd Z dZ!dZ"dZ#dZ$dZ%dZ&dZ'dZ(dZ)dZ*dZ+dZ,dZ-dZ.dZ/dZ0dZ1dZ2dZ3e3Z4e"e#e$e%e(e)e*e&e'e+e,e-f Z5e"e#e*e-fZ6e+e,e-fZ7dZ8hd Z9e:e:e:e;e;e;d!Zne?Z>d$d%Z@d&d'ZAd(d)ZBd*e4fd+d,ZCd-d.ZDdedfd/d0ZEd1d2ZFGd3d4d4eGZHGd5d6d6eHZIGd7d8d8eHZJGd9d:d:eHZKGd;d<dd>eHZMGd?d@d@eMZNGdAdBdBeMZOGdCdDdDeMZPGdEdFdFeMZQGdGdHdHeMZRGdIdJdJZSGdKdLdLZTGdMdNdNeUZVGdOdPdPeUZWGdQdRdRe jXZYGdSdTdTeHZZGdUdVdVeZZ[GdWdXdXeZZ\GdYdZdZeZZ]Gd[d\d\eZZ^Gd]d^d^eZZ_Gd_d`d`eZZ`eZeeIfZadudbdcZbdddeZcdfdgZddhdiZeecedeedjZfeUZgehdkZiGdldmdmeUZjGdndodoeUZkdpdqZlekjZdrdsZmendtkremdSdS)vz,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-8cCs8|durtd|||}|d||t|tS)z8Convert a string to a null-terminated bytes object. Nzmetadata cannot contain None) ValueErrorencodelenNUL)slengthencodingerrorsr8./opt/alt/python310/lib64/python3.10/tarfile.pystns r:cCs*|d}|dkr|d|}|||S)z8Convert a null-terminated bytes object to a string. rN)finddecode)r4r6r7pr8r8r9ntss   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#rAasciistrict0invalid header)ranger2r?intstripr0InvalidHeaderError)r4nir8r8r9ntis    rNrBcCs|}t|}d|krd|dkr%nntd|d|fdt}|S|tkrmd|d |krs  rc@sjeZdZdZdddZddZddZd d Zd d Zd dZ e j fddZ dddZ ddZddZdS) _FileInFilezA thin wrapper around an existing file object that provides a part of its data as an individual file object. NcCs||_||_||_d|_t|dd|_d|_|durd|fg}d|_g|_d}|j}|D]&\}}||kr>|j d||df|j d||||f||7}||}q,||jkre|j d||jdfdSdS)NrrFT) roffsetr'positionrhrr map_indexmapr)rrrr'Z blockinfoZlastposZrealposr8r8r9rbs*    z_FileInFile.__init__cCsdSrr8rr8r8r9r{z_FileInFile.flushcCdSNTr8rr8r8r9readable~rz_FileInFile.readablecCrNFr8rr8r8r9writablerz_FileInFile.writablecCs |jSr)rseekablerr8r8r9r z_FileInFile.seekablecCr)z*Return the current file position. )rrr8r8r9rrz_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_ENDr0)rrwhencer8r8r9rs   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#rY) r'rrrrr2rrr]rr3) rr'rWdatastartstoprr5rdr8r8r9r]s2    z_FileInFile.readcCs&|t|}||dt|<t|Sr)r]r2)rrdrWr8r8r9readintosz_FileInFile.readintocCs d|_dSr)rrr8r8r9rrz_FileInFile.closer)rorprqrrrrrrrrrrrr]rrr8r8r8r9r\s   rceZdZfddZZS) ExFileObjectcs&t|j|j|j|j}t|dSr)rr offset_datar'sparsesuperr)rtarfiletarinfor __class__r8r9rs zExFileObject.__init__rorprqr __classcell__r8r8rr9rsrc@s eZdZdS) FilterErrorN)rorprqr8r8r8r9rsrcr)AbsolutePathErrorcs ||_td|jddS)Nzmember z has an absolute pathrrrrrrrr8r9rszAbsolutePathError.__init__rr8r8rr9rrcr)OutsideDestinationErrorc.||_||_t|jd|dddS)Nz would be extracted to , which is outside the destinationr_pathrrrrrr%rr8r9r  z OutsideDestinationError.__init__rr8r8rr9rrrcr)SpecialFileErrorc||_t|jddS)Nz is a special filerrrr8r9rzSpecialFileError.__init__rr8r8rr9rrrcr)AbsoluteLinkErrorcr)Nz is a link to an absolute pathrrrr8r9rrzAbsoluteLinkError.__init__rr8r8rr9rrrcr)LinkOutsideDestinationErrorcr)Nz would link to rrrrrr8r9rrz$LinkOutsideDestinationError.__init__rr8r8rr9rrrcr)LinkFallbackErrorcs2||_||_td|jdd|ddS)Nzlink z would be extracted as a zcopy of z, which was rejectedrrrr8r9rs   zLinkFallbackError.__init__rr8r8rr9rrrTcCsi}|j}tjj|tjjd}|dtjfr$|jdtj}|d<tj|r.t |tjjtj ||tjjd}tj ||g|krLt |||j }|dur|d@}|r}|sa|rn|d@si|dM}|dO}n|sv|ryd}nt|||j kr||d<|r|jdurd|d <|jdurd|d <|jdurd|d <|jdurd|d <|s|rtj|jrt|tj|j}||jkr||d <|rtj |tj||j}ntj ||j}tjj|tjjd}tj ||g|krt|||S)N)rE/ri@irr)r*r+r,linkname)rr~r%realpath ALLOW_MISSINGrseplstripisabsrr commonpathrrisregislnkisdirissymrr)r*r+r,r rnormpathdirnamer)member dest_pathZfor_data new_attrsrZ target_pathrZ normalizedr8r8r9_get_filtered_attrssl           rcCs|Srr8)rrr8r8r9fully_trusted_filterArrcCs,t||d}|r|jdi|ddiS|S)NFdeepr8rrrrrr8r8r9 tar_filterD r!cCs,t||d}|r|jdi|ddiS|S)NTrFr8rr r8r8r9 data_filterJr"r#)Z fully_trustedrrs([0-9]{1,20}) c @s,eZdZdZed|idddddddd d d d d ddddddddddddddddddd d!d"d#d$d%d&d%d'd%Zd}d)d*Zed+d,Zej d-d,Zed.d/Z e j d0d/Z d1d2Z e e e e e e e e d3e d4 d5d6Z d7d8Zeed9fd:d;Zdd?Zd@dAZedBdCZdDdEZedFdGZedHdIZedJdKZedLdMZedNdOZedPdQZdRdSZdTdUZ dVdWZ!dXdYZ"dZd[Z#d\d]Z$d^d_Z%d`daZ&dbdcZ'dddeZ(dfdgZ)dhdiZ*djdkZ+dldmZ,dndoZ-dpdqZ.drdsZ/dtduZ0dvdwZ1dxdyZ2dzd{Z3d%S)~raInformational 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.r zcName 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(r$REGTYPEr%r r+r,r&r'rrrr(rrr8r8r9rs" zTarInfo.__init__cCr)z(In pax headers, "name" is called "path".rrr8r8r9r%rz TarInfo.pathcC ||_dSrr-r,r8r8r9r% cCr)z0In pax headers, "linkname" is called "linkpath".r rr8r8r9r&rzTarInfo.linkpathcCr.rr0)rr r8r8r9r&r/cCsd|jj|jt|fS)Nz<%s %r at %#x>)rroridrr8r8r9__repr__szTarInfo.__repr__T) rr(rr r)r*r+r,r_KEEPc Cs| rt|} nt|} || ur|| _|| ur|| _|| ur"|| _|| ur)|| _|| ur0|| _|| ur7|| _|| ur>|| _|| urE|| _ | S)zGReturn a deep copy of self with the given attributes replaced. ) copyZdeepcopyrr(rr r)r*r+r,) rrr(rr r)r*r+r,rr3resultr8r8r9rs(  zTarInfo.replacecCs|jdurd}n|jd@}|j||j|j|j|j|j|j|j|j |j |j |j d }|dt kr>|dds>|dd7<|S)z9Return the TarInfo's attributes as a dictionary. N) rrr)r*r'r(r$r%r r+r,r&r'r%rr )rrr)r*r'r(r$r%r r+r,r&r'DIRTYPEr)rrinfor8r8r9get_infos(  zTarInfo.get_infosurrogateescapecCsv|}|D]\}}|durtd|q|tkr"||||S|tkr-||||S|tkr7|||Std)zsD        zTarInfo.create_pax_headercCs||tdS)zAReturn the object as a pax global header block sequence. r/)rSXGLTYPE)clsr(r8r8r9create_pax_global_headerNsz TarInfo.create_pax_global_headercCs||d}tdt|D]-}d|d|}d||d}t|||tkr9t|||tkr9||fSq td)zUSplit a name longer than 100 chars into a prefix and a name part. r r#Nzname is too long)splitrHr2rr1 LENGTH_PREFIXrEr0)rrr6r7Z componentsrMrBr8r8r9rFTs zTarInfo._posix_split_namec Cs|dttfv}|r t|ddd|}t|ddd|}ntdd||}tdd||}|dt}|durr8r8r8r9rSsB   ( z"TarInfo._create_pax_generic_headerc Csft|dkr tdt|tkrtd|ttkrtdt|dd}|t|vr1t d|}t |dd|||_ t|dd |_ t|d d |_ t|d d |_t|d d |_t|d d|_||_|dd |_t |d d|||_t |dd|||_t |dd|||_t|dd|_t|dd|_t |dd||}|jtkr|j drt|_|jtkrd}g}tdD]0} zt|||d} t||d|d} Wn tyYn w|| | f|d7}qt|d} t|dd} || | f|_ |!r |j "d|_ |r1|jt#vr1|d|j |_ |S)zAConstruct a TarInfo object from a 512 byte bytes object. rz empty headerztruncated headerzend of file headerz bad checksumrlt|ii i)iIiQiYir irrMiii)$r2rtrrucountr3rvrNrXrKr?rrr)r*r'r(r$r%r r+r,r&r'AREGTYPErr7GNUTYPE_SPARSErHr0rboolr)rrstrip GNU_TYPES)rWrWr6r7r$objrBrstructsrMrnumbytes isextendedorigsizer8r8r9frombufs\          zTarInfo.frombufcCs8|jt}|||j|j}|jt|_||S)zOReturn the next TarInfo object from TarFile object tarfile. ) rr]rrxr6r7rr _proc_member)rWrrWrsr8r8r9 fromtarfiles  zTarInfo.fromtarfilecCsP|jttfvr ||S|jtkr||S|jtttfvr#| |S| |S)zYChoose the right processing method depending on the type and call it. ) r%rKrJ _proc_gnulongro _proc_sparserTrVSOLARIS_XHDTYPE _proc_pax _proc_builtin)rrr8r8r9ry0s     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 )rrrrr%SUPPORTED_TYPES_blockr'r_apply_pax_infor(r6r7rrrq)rrrr8r8r9r=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]rr'rzr rwrRrr%rKr?r6r7rrJr r removesuffix)rrrWnextrr8r8r9r{Ss  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. rrMrli)r)rr]rrHrNr0rrprrrrr'r) rrrtrvrwrWrrMrrur8r8r9r|ns,       zTarInfo._proc_sparsec Cs|j||j}|jtkr|j}n|j}d}d}g}t||kr||dkrt ||}s6t dz t | d}Wn tyJt dw|dkrSt d||t|kr_t d|d|d} ||dd| } | d\} } } | r| dks|| dkrt d||| | f| dkr|dur| d kr|j}nd }||7}t||kr||dks*|durd }|D]*\}} } || d d |j}|tvr|| ||j|j}n || d d |j}|||<qz||}Wnty}ztt|dd}~wwd |vr|||n#d |vr|||n|d dkr3|ddkr3|||||jtt fvrf|!||j|j|j"|_"d|vrf|j#}|$s[|jt%vrc|||j7}||_"|S)zVProcess an extended or global header as described in POSIX.1-2008. rNrGr#rars hdrcharsetsBINARYr/GNU.sparse.mapGNU.sparse.sizezGNU.sparse.major1zGNU.sparse.minorrFr')&rr]rr'r%rVr(r4r2_header_length_prefix_rematchrKrIgroupr0rrg partitionrr6_decode_pax_fieldr7PAX_NAME_FIELDSrzr rwrR_proc_gnusparse_01_proc_gnusparse_00r[_proc_gnusparse_10rTr}rrrrr)rrrWr(rr6 raw_headersrr5Zheader_value_end_offsetZkeyword_and_valueZ raw_keywordZequalsZ raw_valuercr?rrrr8r8r9r~s    )        zTarInfo._proc_paxc Csg}g}|D];\}}}|dkr&z |t|Wqty%tdw|dkrAz |t|Wqty@tdwqtt|||_dS)z?Process a GNU tar extended sparse header, version 0.0. sGNU.sparse.offsetrGsGNU.sparse.numbytesN)rrIr=r0rKlistzipr)rrrZoffsetsru_rcr?r8r8r9rs"  zTarInfo._proc_gnusparse_00cCs@dd|ddD}tt|ddd|ddd|_dS)z?Process a GNU tar extended sparse header, version 0.1. cSsg|]}t|qSr8)rI).0xr8r8r9 sz.TarInfo._proc_gnusparse_01..r,Nr$r#)rYrrr)rrr(rr8r8r9rs(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. Nrbr#r$) rr]rrYrIr2rrrrrr)rrr(rZfieldsrrWZnumberr8r8r9rs  (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%rr'zGNU.sparse.realsizerr N) r;setattrrI PAX_FIELDSPAX_NUMBER_FIELDSr0rqr4r()rr(r6r7rcr?r8r8r9r-s&   zTarInfo._apply_pax_infocCs.z||dWSty|||YSw)z1Decode a single field from a pax record. rE)r=UnicodeDecodeError)rr?r6Zfallback_encodingZfallback_errorsr8r8r9rDs  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)rrmrbrcr8r8r9rLszTarInfo._blockcCs |jtvSz4Return True if the Tarinfo object is a regular file.)r% REGULAR_TYPESrr8r8r9rUr/z TarInfo.isregcCs|Sr)rrr8r8r9isfileYszTarInfo.isfilecC |jtkS)z!Return True if it is a directory.)r%r7rr8r8r9r]r/z TarInfo.isdircCr)z%Return True if it is a symbolic link.)r%SYMTYPErr8r8r9rar/z TarInfo.issymcCr)z!Return True if it is a hard link.)r%LNKTYPErr8r8r9rer/z TarInfo.islnkcCr)z(Return True if it is a character device.)r%r\rr8r8r9ischrir/z TarInfo.ischrcCr)z$Return True if it is a block device.)r%r]rr8r8r9isblkmr/z TarInfo.isblkcCr)zReturn True if it is a FIFO.)r%FIFOTYPErr8r8r9isfifoqr/zTarInfo.isfifocCs |jduSr)rrr8r8r9issparseurzTarInfo.issparsecCs|jtttfvS)zCReturn True if it is one of character device, block device or FIFO.)r%r\r]rrr8r8r9isdevxsz TarInfo.isdevr8)r)4rorprqrrdict __slots__rpropertyr%setterr&r2r3rr9rr r@r<r=r> classmethodrXrF staticmethodrGr_rIrSrxrzryrr{r|r~rrrrrrrrrrrrrrrrr8r8r8r9r`s         :  )   2 >  y  rc@s eZdZdZdZdZdZdZeZ e Z dZ e ZeZdZ    dkdd Zedddefd d Zedld d ZedmddZedmddZednddZdddddZddZddZddZd d!Zdod"d#Zdpdd%d&d'Z dqdd(d)d*Z!drd+d,Z"d-d.Z#dsddd0d1d2Z$d3d4Z%dtddd0d6d7Z&d8d9Z' drd:d;Z(dd?Z*d@dAZ+ $ dudddBdCdDZ,dEdFZ-dGdHZ.dIdJZ/dKdLZ0dMdNZ1dOdPZ2dQdRZ3dSdTZ4dUdVZ5dWdXZ6dYdZZ7dvd[d\Z8d]d^Z9drd_d`Z:dadbZ;dcddZdidjZ?dS)wrz=The TarFile Class provides an interface to tar archives. rFr#Nr{r:c 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+bwbZxbr{ar|r!mode must be 'r', 'a', 'w' or 'x'rr|FNrrTr{rr|r)-r0r_moder~r%exists bltn_openrrrOrrRrOabspathrrSr dereference ignore_zerosr6r7rr(debug errorlevel copybufsizermembers_loadedrrinodes firstmemberrrrzrrvr rrXr4r^r2r)rrrrrSrrrr6r7r(rrrZmodesrrWr8r8r9rs             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)rrWr8r9not_compressed2rz$TarFile.open..not_compressed)keyNr{z - method z:  z'file could not be opened successfully: :r#rr|rzzmode must be 'r' or 'w'Frzundiscernible mode)r0sortedrrhrrrrrrrYrrrr)rWrrrrakwargsrZ error_msgsrfuncZ saved_posrZerror_msgs_summaryfilemodestreamrr8rr9rsZ%      z TarFile.opencKs$|dvrtd||||fi|S)zCOpen uncompressed tar archive name for reading or writing. rr)r0)rWrrrrr8r8r9rfszTarFile.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|rmode must be 'r', 'w' or 'x'r)GzipFilezgzip module is not availableNrdr{rF) r0Zgziprrrrrrrr) rWrrr compresslevelrrrrr8r8r9gzopenns8    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) r0rrrrrrEOFErrorrrr) rWrrrrrrrrr8r8r9bz2opens*   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) r0rrrrrrrrrr) rWrrrrrrrrrr8r8r9xzopens*   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^r3rrr\ RECORDSIZErr)rrbrcr8r8r9rs   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. r Nzfilename %r not found) _getmemberrqKeyError)rrrr8r8r9 getmembers 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_loadrrr8r8r9 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|]}|jqSr8r-)rrr8r8r9rsz$TarFile.getnames..)rrr8r8r9getnamesszTarFile.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. awxNr rr#rmajorminor):rrr~r% splitdriverrrrrrlstatstatfstatfilenost_modeS_ISREGst_inost_devst_nlinkrrr+S_ISDIRr7S_ISFIFOrS_ISLNKrreadlinkS_ISCHRr\S_ISBLKr]rst_uidr)st_gidr*st_sizer'st_mtimer(r%r pwdgetpwuidr+rgrpZgetgrgidr,rrst_rdevr&rr') rrarcnamerZdrvrZstatresr Zstmdinoder%r8r8r9 gettarinfos                zTarFile.gettarinfoT)rcCs"||dur |}|D]}|rf|jdurtdntt|jtd|jp)|j|jp.|jf| s:| rGtdd|j |j fntd|j |jdurXtdntdt|jdd t|j|rod nd |r|rtd |j|rtd |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 ??????????z%s/%sz%10sz%d,%dz%10dz????-??-?? ??:??:??z%d-%02d-%02d %02d:%02d:%02drr rz-> zlink to )rrrlrrr+r)r,r*rrr&r'r'r(r localtimerrrr rrk)rverboserrr8r8r9res:     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 %rtarfile: Excluded %rrr)rrr~r%r_dbgrrraddfilerrlistdiraddr)rrr recursiverrfr8r8r9r s<    " 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)rarr#)rr4r@rSr6r7rr^rr2rr[r'r\rr3rr)rrrrWrarbrcr8r8r9r s   zTarFile.addfilecCsh|dur|j}|dur tSt|trtd|St|r|Szt|WSty3td|ddw)NzrString names are not supported for TarFile.extraction_filter. Use a function such as tarfile.data_filter directly.zfilter z not found) extraction_filterrrOrR TypeErrorcallable_NAMED_FILTERSrr0)rrr8r8r9_get_filter_functions    zTarFile._get_filter_function.) numeric_ownerrc Csg}||}|dur |}|D]&}||||\}} |durq|r(|| |j||| ||dq|jdddd|D]} zsz|| |}Wntyf} z|| t| WYd} ~ WqAd} ~ ww|durs|| dWqAt j ||j } zt | } Wnty||dYWqAwt| js||d WqA|j|| |d ||| ||| WqAty} z || WYd} ~ qAd} ~ 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. The `filter` function will be called on each member just before extraction. It can return a changed TarInfo or None to skip the member. String names of common filters are accepted. N) set_attrsrfilter_functioncSrrr-)rr8r8r9 sz$TarFile.extractall..T)rreversezexcluded by filterZmissingznot a directory)r)r_get_extract_tarinforr _extract_onesort_FILTER_ERRORS_log_no_directory_fixupreprr~r%rrrFileNotFoundErrorrrrchownutimechmodr _handle_nonfatal_error)rr%rrrZ directoriesrrr unfilteredexcdirpathrrr8r8r9 extractallsb       zTarFile.extractallcCs|dd|j|fdS)Nr$z(tarfile: Not fixing up directory %r (%s))rr)rrreasonr8r8r9r. s zTarFile._log_no_directory_fixuprc Cs<||}||||\}}|dur|||||dSdS)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. The `filter` function will be called before extraction. It can return a changed TarInfo or None to skip the member. String names of common filters are accepted. N)rrr) rrr%rrrrrr$r8r8r9extract2 s zTarFile.extractc Cst|tr ||}n|}d}z|||}Wn/ttfy/}z ||WYd}~nd}~wtyE}z ||WYd}~nd}~ww|durU|dd|j dS| rgt |}t j ||j|_||fS)zGet (filtered, unfiltered) TarInfos from *member* *member* might be a string. Return (None, None) if not found. Nr$r)NN)rOrRrrr_handle_fatal_errorr r#rrrr4r~r%rr r*)rrrr%r$filteredrr8r8r9rF s(   zTarFile._get_extract_tarinfoc Cs|dz|j|tj||j||||dWdSty1}z ||WYd}~dSd}~wtyH}z | |WYd}~dSd}~ww)zExtract from filtered tarinfo to disk. filter_function is only used when extracting a *different* member (e.g. as fallback to creating a symlink) r{)rrrextraction_rootN) r_extract_memberr~r%rrrr*r r#)rrr%rrrrr8r8r9rd s  zTarFile._extract_onecCs |jdkr|dd|dS)z=Handle non-fatal error (ExtractError) according to errorlevelr# tarfile: %sN)rrrrr8r8r9r#x s zTarFile._handle_nonfatal_errorcCsp|jdkrt|tr)|jdur|dd|jdS|dd|j|jfdS|ddt|j|fdS)z1Handle "fatal" error according to self.errorlevelrNr#r.ztarfile: %s %rztarfile: %s %s)rrOrfilenamerstrerrorr%ror/r8r8r9r* s   zTarFile._handle_fatal_errorcCsz|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)rrOrRrrr%r fileobjectrrrrr extractfile_find_link_target)rrrr8r8r9r3 s     zTarFile.extractfilerr,cCs\|d}|dtj}tj|}|rtj|st||s'| r4| dd|j |j fn| d|j | rF|||nI|rQ|||n>|r\|||n3|sd|rk|||n$|ss| r}|j||||dn|jtvr|||n||||r||||| s||||||dSdSdS)zExtract the filtered TarInfo object tarinfo to a physical file called targetpath. filter_function is only used when extracting a *different* member (e.g. as fallback to creating a symlink) r r#z%s -> %sr5N)rqrr~rr%rrmakedirsrrrrr rmakefilermakedirrmakefiforrmakedevmakelink_with_filterr%r makeunknownr r"r!)rr targetpathrrrr,Z upperdirsr8r8r9r- s@      zTarFile._extract_membercCsBz|jdurt|WdSt|dWdSty YdSw)z,Make a directory called targetpath. Ni)rr~mkdirFileExistsErrorrrr=r8r8r9r8 s  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)rrr=sourceratargetrr'r8r8r9r7 s      "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)r7rr%r@r8r8r9r< s  zTarFile.makeunknowncCs ttdr t|dStd)z'Make a fifo called targetpath. mkfifozfifo not supported by systemN)rr~rDr r@r8r8r9r9 s zTarFile.makefifocCsjttdr ttdstd|j}|durd}|r!|tjO}n|tjO}t||t |j |j dS)z     z TarFile.chownc CsF|jdurdSz t||jWdSty"}ztd|d}~ww)zASet file permissions of targetpath according to tarinfo. Nzcould not change mode)rr~r"rr )rrr=rr8r8r9r"y s  z TarFile.chmodc CsZ|j}|dur dSttdsdSz t|||fWdSty,}ztd|d}~ww)zBSet modification time of targetpath according to tarinfo. Nr!z"could not change modification time)r(rr~r!rr )rrr=r(rr8r8r9r! 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#rYTr$z0x%X: %srz empty filez zlib error: )rrrrrrr]rrrzrvrrrrKrRrtrurw ExceptionrrOrrrrr)rmrrrr8r8r9r st                z TarFile.nextc Cs|}d}|dur#z||}Wn tyd}Ynw|d|}|r+tj|}t|D]"}|r<|j|jkr;d}q/|rFtj|j}n|j}||krQ|Sq/|rXt|dS)z}Find an archive member by name from bottom to top. If tarinfo is given, it is used as the starting point. FNT) rindexr0r~r%rreversedrr) rrr normalizerZskippingrVr member_namer8r8r9r s2     zTarFile._getmembercCs |}|dur nqd|_dS)zWRead through the entire archive file and look for readable members. TN)rrrr8r8r9r 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)rrrror)rrr8r8r9r 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. r NT)rrXzlinkname %r not found) rrrr~r%rrr rr)rrr limitrr8r8r9r4 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)rrrrr2)rrVrr8r8r9__iter__ s,   zTarFile.__iter__cCs ||jkrt|tjddSdS)z.Write debugging output to sys.stderr. fileN)rrkristderr)rlevelmsgr8r8r9r= s z TarFile._dbgcCs ||Sr)rrr8r8r9 __enter__C szTarFile.__enter__cCs.|dur |dS|js|jd|_dSr)rrrr)rr%r? tracebackr8r8r9__exit__G s    zTarFile.__exit__) Nr{NNNNNNr:NNNN)r{N)r{Nr)r{NN)NNNTrr)rN)rT)TFr)@rorprqrrrrrrrrSr r6r7rrrr2rrrrrrrrrrrrrrrrr r rr'rr)rrr#r*r3r-r8r7r<r9r:rHr;r r"r!rrrrr4r[rrarcr8r8r8r9r}s o]       c& 3 E     82!  ?(   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)rrr8r8r9rU s   rc Cshddl}d}|j|d}|jdddddd |jd d td d |jdd}|jddddd|jdddddd|jdddddd|jdd dd!d|}|jr]|jdur]|d"d#|j dur|j }t |rt |d$}| t | tjd%Wdn1swY|jrt d&|dSdS|d"d'|dS|jdur|j}t |rt |d(}|j|jd)WddS1swYdS|d"d'|dS|jdurRt|jd"kr|jd}tj}nt|jd*kr|j\}}n|d"|t |rGt |d(}|j||jd+Wdn 1s%wY|jrE|d,kr9d-|} nd.||} t | dSdS|d"d'|dS|jdur|jd} tj| \} } d/d/d0d0d1d1d1d1d2} | | vr|d3| | nd4}|j}t | |}|D]}||qWdn 1swY|jrt d5| dSdSdS)6Nrz3A simple command-line interface for tarfile module.) descriptionz-vz --verbose store_trueFzVerbose output)actiondefaulthelpz--filterz zFilter for extraction)metavarchoicesriT)Zrequiredz-lz--list zShow listing of a tarfile)rjriz-ez --extract+)rlz zExtract tarfile into target dir)nargsrjriz-cz--create)zzzCreate tarfile from sourcesz-tz--testzTest if a tarfile is validr#z&--filter is only valid for extraction r{r\z{!r} is a tar archive.z{!r} is not a tar archive. r)rr$)r%rrz{!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_argumentrZadd_mutually_exclusive_group parse_argsrr)exittestrrrrkrir^rrSrrr2r~curdirZ format_helpr'Zcreatepopr%splitextr )roreparserrargsr_rZtfrur`Ztar_namerZextZ compressionsZtar_modeZ tar_files file_namer8r8r9mainh s           "          r{__main__rd)orrversion __author__ __credits__builtinsrrrir~rrZrrrVr4rewarningsrrrAttributeErrorNotImplementedErrorrMr NameError__all__r3rrrHrCrErDrZr+rnrrr\r]r7rZCONTTYPErKrJrorTrVr}r rrrrrrrrrrPrIrrr getfilesystemencodingr:r?rNrTrXr[rlrTrr rrr r rtrurvrKrwryrobjectrrBufferedReaderrrrrrrrrrrrr!r#rr3compilerrrrr{ror8r8r8r9s(         sh  D #a _