o ?Og @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_dsd`daZ`dbdcZadddeZbdfdgZceaebecdhZdeUZeefdiZgGdjdkdkeUZhGdldmdmeUZidndoZjeijZdpdqZkeldrkrekdSdS)tz,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__rr8r8rr9rrrTcCsi}|j}tj|}|dtjfr |jdtj}|d<tj|r*t|tjtj ||}tj ||g|krDt |||j }|dur~|d@}|ru| sY|rf|d@sa|dM}|dO}n|sn|rqd}nt|||j kr~||d<|r|jdurd|d<|jdurd|d <|jdurd|d <|jdurd|d <|s|rtj|jrt||rtj |tj||j}ntj ||j}tj|}tj ||g|krt|||S) N/ri@irr)r*r+r,)rr~r%realpathrseplstripisabsrr commonpathrrisregislnkisdirissymrr)r*r+r,linknamerdirnamer)member dest_pathZfor_data new_attrsrZ target_pathrr8r8r9_get_filtered_attrss^            rcCs|Srr8)rrr8r8r9fully_trusted_filter0rrcCs,t||d}|r|jdi|ddiS|S)NFdeepr8rrrrrr8r8r9 tar_filter3 rcCs,t||d}|r|jdi|ddiS|S)NTrFr8rrr8r8r9 data_filter9rr)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.rzcName 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!rr+r,r"r#rrrr$rrr8r8r9rrs" 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".rrr8r8r9r&rzTarInfo.linkpathcCr*rr,)rrr8r8r9r&r+cCsd|jj|jt|fS)Nz<%s %r at %#x>)rroridrr8r8r9__repr__szTarInfo.__repr__T) rr(rrr)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(rrr)r*r+r,) rrr(rrr)r*r+r,rr/resultr8r8r9rs(  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!rr+r,r"r#r!rr)rrr)r*r'r(r r!rr+r,r"r#DIRTYPEr)rrinfor8r8r9get_infos(  zTarInfo.get_infosurrogateescapecCsv|}|D]\}}|durtd|q|tkr"||||S|tkr-||||S|tkr7|||Std)zr8r8r9rBCs zTarInfo._posix_split_namec Cs|dttfv}|r t|ddd|}t|ddd|}ntdd||}tdd||}|dt}|durrz%dsriz%06orDi)getCHRTYPEBLKTYPErTr:r'r0r?rVrrrrXrO) r4rSr6r7Zhas_device_fieldsr"r#ZfiletypepartsrWr r8r8r9rCSs:  &zTarInfo._create_headercCs.tt|t\}}|dkr|t|t7}|S)zdReturn the string payload filled with zero bytes up to the next 512 byte border. r)r\r2rr3)Zpayloadrbrcr8r8r9_create_payload}szTarInfo._create_payloadcCsR|||t}i}d|d<||d<t||d<t|d<||t||||S)zTReturn a GNUTYPE_LONGNAME or GNUTYPE_LONGLINK sequence for name. z ././@LongLinkrr!r'r=)r1r3r2rDrCr r[)rSrr!r6r7r4r8r8r9rEs zTarInfo._create_gnu_long_headerc Cs,d}|D]\}}z|ddWqtyd}Ynwd}|r'|d7}|D]I\}}|d}|r=||d}n|d}t|t|d}d } } |tt| } | | kr^n| } qQ|tt| d d |d |d 7}q+i} d| d<|| d<t|| d<t| d<|| td d| |S)zReturn a POSIX.1-2008 extended or global header sequence that contains a list of keyword, value pairs. The values must be strings. Fr/rETrs21 hdrcharset=BINARY r6rrrD = z././@PaxHeaderrr!r'r=r) r7r1rJr2rNrOr?rCr r[) rSr$r!r6Zbinarykeywordr;ZrecordslrLr>r4r8r8r9rOsB   ( 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)iIiQiYirirrIiii)$r2rtrrucountr3rvrNrXrKr?rrr)r*r'r(r r!rr+r,r"r#AREGTYPErr3GNUTYPE_SPARSErHr0rboolr%rrstrip GNU_TYPES)rSrWr6r7r objr>rstructsrMrnumbytes isextendedorigsizer8r8r9frombufs\          zTarInfo.frombufcCs8|jt}|||j|j}|jt|_||S)zOReturn the next TarInfo object from TarFile object tarfile. ) rr]rrtr6r7rr _proc_member)rSrrWror8r8r9 fromtarfile s  zTarInfo.fromtarfilecCsP|jttfvr ||S|jtkr||S|jtttfvr#| |S| |S)zYChoose the right processing method depending on the type and call it. ) r!rGrF _proc_gnulongrk _proc_sparserPrRSOLARIS_XHDTYPE _proc_pax _proc_builtin)rrr8r8r9rus     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$r6r7rrrm)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]r}r'rvr rwrNrr!rGr?r6r7rrFrr removesuffix)rrrWnextrr8r8r9rwBs  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. rrIrhi)r%rr]rrHrNr0rrlrrrr}r'r) rrrprrrsrWrrMrrqr8r8r9rx]s,       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#r]rs hdrcharsetsBINARYr/GNU.sparse.mapGNU.sparse.sizezGNU.sparse.major1zGNU.sparse.minorrFr')&rr]r}r'r!rRr$r0r2_header_length_prefix_rematchrKrIgroupr0rrg partitionrr6_decode_pax_fieldr7PAX_NAME_FIELDSrvr rwrN_proc_gnusparse_01_proc_gnusparse_00rW_proc_gnusparse_10rPryr~rrrr|)rrrWr$rr6 raw_headersrr5Zheader_value_end_offsetZkeyword_and_valueZ raw_keywordZequalsZ raw_valuer_r;rrrr8r8r9rzys    )        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)rrrZoffsetsrq_r_r;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#)rUrrr)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. Nr^r#r$) rr]rrUrIr2rrrrrr)rrr$rZfieldsrrWZnumberr8r8r9r s  (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.realsizerrN) r7setattrrI PAX_FIELDSPAX_NUMBER_FIELDSr0rmr0r$)rr$r6r7r_r;r8r8r9r~s&   zTarInfo._apply_pax_infocCs.z||dWSty|||YSw)z1Decode a single field from a pax record. rE)r=UnicodeDecodeError)rr;r6Zfallback_encodingZfallback_errorsr8r8r9r3s  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)rrirbrcr8r8r9r};szTarInfo._blockcCs |jtvSz4Return True if the Tarinfo object is a regular file.)r! REGULAR_TYPESrr8r8r9rDr+z TarInfo.isregcCs|Sr)rrr8r8r9isfileHszTarInfo.isfilecC |jtkS)z!Return True if it is a directory.)r!r3rr8r8r9rLr+z TarInfo.isdircCr)z%Return True if it is a symbolic link.)r!SYMTYPErr8r8r9rPr+z TarInfo.issymcCr)z!Return True if it is a hard link.)r!LNKTYPErr8r8r9rTr+z TarInfo.islnkcCr)z(Return True if it is a character device.)r!rXrr8r8r9ischrXr+z TarInfo.ischrcCr)z$Return True if it is a block device.)r!rYrr8r8r9isblk\r+z TarInfo.isblkcCr)zReturn True if it is a FIFO.)r!FIFOTYPErr8r8r9isfifo`r+zTarInfo.isfifocCs |jduSr)rrr8r8r9issparsedrzTarInfo.issparsecCs|jtttfvS)zCReturn True if it is one of character device, block device or FIFO.)r!rXrYrrr8r8r9isdevgsz TarInfo.isdevr8)r)4rorprqrrdict __slots__rpropertyr%setterr&r.r/rr5rr r<r8r9r: classmethodrTrB staticmethodrCr[rErOrtrvrur{rwrxrzrrrr~rr}rrrrrrrrrrr8r8r8r9rOs         :  )   2 >  y  rc@seZdZdZdZdZdZdZeZ e Z dZ e ZeZdZ    dfdd Zedddefd d Zedgd d ZedhddZedhddZediddZdddddZddZddZddZd d!Zdjd"d#Zdkdd%d&d'Z dldd(d)d*Z!dmd+d,Z"d-d.Z#dnddd0d1d2Z$doddd0d4d5Z%d6d7Z&d8d9Z'd:d;Z(dd?Z* $ dpd@dAZ+dBdCZ,dDdEZ-dFdGZ.dHdIZ/dJdKZ0dLdMZ1dNdOZ2dPdQZ3dRdSZ4dTdUZ5dqdVdWZ6dXdYZ7dmdZd[Z8d\d]Z9d^d_Z:d`daZ;dbdcZ.not_compressed)keyNr{z - method z:  z'file could not be opened successfully: :r#rr|rzzmode must be 'r' or 'w'Frzundiscernible mode)r0sortedrrhrrrrrrrUrrrr)rSrrrrakwargsrZ error_msgsrfuncZ saved_posrZerror_msgs_summaryfilemodestreamrr8rr9rsZ%      z TarFile.opencKs$|dvrtd||||fi|S)zCOpen uncompressed tar archive name for reading or writing. rr)r0)rSrrrrr8r8r9rUszTarFile.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) rSrrr compresslevelrrrrr8r8r9gzopen]s8    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) rSrrrrrrrrr8r8r9bz2open~s*   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) rSrrrrrrrrrr8r8r9xzopens*   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. rNzfilename %r not found) _getmemberrmKeyError)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. awxNrrr#rmajorminor):rrr~r% splitdriverr r rrrlstatstatfstatfilenost_modeS_ISREGst_inost_devst_nlinkrrr'S_ISDIRr3S_ISFIFOrS_ISLNKrreadlinkS_ISCHRrXS_ISBLKrYrst_uidr)st_gidr*st_sizer'st_mtimer(r!rpwdgetpwuidr+rgrpZgetgrgidr,rrst_rdevr"rr#) rrarcnamerZdrvrZstatresrZstmdinoder!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:%02drrrz-> zlink to )rrrlrrr+r)r,r*rrr"r#r'r(r localtimerrrrrrk)rverboserrr8r8r9rTs:     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 recursiverrfr8r8r9rzs<    " 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#)rr0r<rSr6r7rr^rr2rr[r'r\rr3rr)rrrrWrarbrcr8r8r9rs   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_filterrrKrN TypeErrorcallable_NAMED_FILTERSrr0)rrr8r8r9_get_filter_functions    zTarFile._get_filter_function.) numeric_ownerrc Csg}||}|dur |}|D]#}||||}|durq|r&|||j||| |dq|jdddd|D]7}tj||j } z|j || |d| || | || Wq>t yu} z || WYd} ~ q>d} ~ 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_attrsrcSrrr))rr8r8r9sz$TarFile.extractall..T)rreverse)r)r_get_extract_tarinforr _extract_onesortr~r%rrchownutimechmodr _handle_nonfatal_error) rr%rrrZ directoriesfilter_functionrrdirpathrr8r8r9 extractalls2   zTarFile.extractallrcCs8||}||||}|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%rrrrrr8r8r9extract s zTarFile.extractc Cst|tr ||}n|}|}z|||}Wn/ttfy/}z ||WYd}~nd}~wtyE}z ||WYd}~nd}~ww|durU|dd|j dS| rgt |}t j ||j|_|S)z@Get filtered TarInfo (or None) from member, which might be a strNr$r)rKrNrrr_handle_fatal_errorr rrrrr0r~r%rrr&)rrrr%rZ unfilteredrr8r8r9r s(   zTarFile._get_extract_tarinfoc Cs|dz|j|tj||j||dWdSty/}z ||WYd}~dSd}~wtyF}z | |WYd}~dSd}~ww)z%Extract from filtered tarinfo to diskr{rN) r_extract_memberr~r%rrrr r r)rrr%rrrr8r8r9r/ s  zTarFile._extract_onecCs |jdkr|dd|dS)z=Handle non-fatal error (ExtractError) according to errorlevelr# tarfile: %sN)rrrrr8r8r9r< 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)rrKrfilenamerstrerrorr!ror#r8r8r9r C 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)rrKrNrrr!r| fileobjectrrrrr extractfile_find_link_target)rrrr8r8r9r'O s     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)rmrr~r r%rrmakedirsrrrrrrmakefilermakedirrmakefiforrmakedevmakelinkr!r| makeunknownrrr)rr targetpathrrZ upperdirsr8r8r9r!o s8      zTarFile._extract_membercCsBz|jdurt|WdSt|dWdSty YdSw)z,Make a directory called targetpath. Ni)rr~mkdirFileExistsErrorrrr0r8r8r9r+ 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)rrr0sourceratargetrr'r8r8r9r* 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)r*rr!r3r8r8r9r/ s  zTarFile.makeunknowncCs ttdr t|dStd)z'Make a fifo called targetpath. mkfifozfifo not supported by systemN)rr~r7r r3r8r8r9r, 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~rrr )rrr0rr8r8r9r s  z TarFile.chmodc CsZ|j}|dur dSttdsdSz t|||fWdSty,}ztd|d}~ww)zBSet modification time of targetpath according to tarinfo. Nrz"could not change modification time)r(rr~rrr )rrr0r(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]rrrvrvrrrrKrNrtrurw ExceptionrrKrrrrr)rmrrrr8r8r9r2 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%normpathreversedrr) rrr normalizerZskippingrGr member_namer8r8r9rq 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. rNT)rrJzlinkname %r not found) rrrr~r%rrrrr)rrrlimitrr8r8r9r( 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)rrGrr8r8r9__iter__ s,   zTarFile.__iter__cCs ||jkrt|tjddSdS)z.Write debugging output to sys.stderr. fileN)rrkristderr)rlevelmsgr8r8r9r s z TarFile._dbgcCs ||Sr)rrr8r8r9 __enter__ szTarFile.__enter__cCs.|dur |dS|js|jd|_dSr)rrrr)rr!r; tracebackr8r8r9__exit__ s    zTarFile.__exit__) Nr{NNNNNNr6NNNN)r{N)r{Nr)r{NN)NNNTrr)rN)rT)TFr)>rorprqrrrrrrrrSr r6r7rrrr&r rrrrrrrrrrrrrrrrrrrrrrrr r'r!r+r*r/r,r-r.rrrrrrrr(rMrrSrUr8r8r8r9rls 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)rrr8r8r9r 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)metavarchoicesr[T)Zrequiredz-lz--list zShow listing of a tarfile)r\r[z-ez --extract+)r^z zExtract tarfile into target dir)nargsr\r[z-cz--create)zzzCreate tarfile from sourcesz-tz--testzTest if a tarfile is validr#z&--filter is only valid for extraction r{rNz{!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_argumentr Zadd_mutually_exclusive_group parse_argsrrexittestrrrrkrirPrrSrrr2r~curdirZ format_helprZcreatepopr%splitextr)rarWparserrargsr_rZtfrgrRZtar_namerZextZ compressionsZtar_modeZ tar_files file_namer8r8r9main s           "          rm__main__rV)mrrversion __author__ __credits__builtinsrrrir~rrZrrrVr0rewarningsrrrAttributeErrorNotImplementedErrorr?r NameError__all__r3rrrDr?rAr@rVr'rjrrrXrYr3rZCONTTYPErGrFrkrPrRryr rrrr|rrnrrrLrIrrr getfilesystemencodingr:r?rNrTrXr[rlrErr rrr r rtrurvrKrwryrobjectrrBufferedReaderrrrrrrrrrrrr r/compilerrrrrmror8r8r8r9s$         sh  ? # _