m %UIc@sdZdkZdkZdkZdkZy dkZWnej o eZnXdddddddd gZde fd YZ e Z d Z d Z d ZdZdZdZdZdZd ZdZdZdZdZdZdZdZd ZdZdZdZdZ dZ!dZ"d Z#d!Z$d"Z%d#Z&d Z'dZ(dZ)dZ*dZ+dZ,dZ-dZ.d Z/dZ0dZ1dZ2d$Z3d%Z4dfd&YZ5dfd'YZ6d e6fd(YZ7dS()sRead and write ZIP files.Nt BadZipfileterrort ZIP_STOREDt ZIP_DEFLATEDt is_zipfiletZipInfotZipFilet PyZipFilecBstZRS(N(t__name__t __module__(((t$/data/zmath/lib/python2.4/zipfile.pyRsiis<4s4H2lHsPKs<4s4B4HlLL5HLlsPKs <4s2B4HlLL2HsPKiiiiiiii i i i i iiiiicCsRy8t|d}t|}|i|otSnWntj onXtS(s?Quickly see if file is a ZIP file by checking the magic number.trbN( topentfilenametfpint _EndRecDatatendrectclosetTruetIOErrortFalse(R RR((R RBs   cCsd|idd|id}|i}|dd!tjoU|ddjoDtit |}t |}|i d|i |d|Snt |d d}|i||d|i}|it}|djo|tit |||d!}t |}||d}|d t|jo*|i ||i ||||Sq`nd S( sReturn data from the "End of Central Directory" record, or None. The data is a list of the nine items in the ZIP "End of central dir" record followed by a tenth item, the file seek offset of this record.iiiiiittiiN(RtseekttelltfilesizetreadtdatatstringEndArchivetstructtunpacktstructEndArchiveRtlisttappendtmint END_BLOCKtrfindtstarttcommenttlen(RR#R&R%RRR((R RNs. %       cBs&tZdZdddZdZRS(s>Class with attributes describing each file in the ZIP archive.tNoNameiiicCs||_|itd}|djo|d|!}ntidjo|itid}n||_||_ t |_ d|_ d|_ d|_d|_d|_d|_d|_d|_d|_d|_dS(Nit/Ri(R tselft orig_filenametfindtchrt null_bytetostseptreplacet date_timeRt compress_typeR&textrat create_systemtcreate_versiontextract_versiontreservedt flag_bitstvolumet internal_attrt external_attr(R*R R2R.((R t__init__ts&              cCs|i}|ddd>|dd>B|dB}|dd>|d d>B|ddB}|id @od}}}n|i}|i}|i}t i t t |i |i|i|i|||||t|it|i }||i|iS( s'Return the per-file header as a string.iii iiiii iiN(R*R2tdttdosdatetdostimeR9tCRCt compress_sizet file_sizeRtpacktstructFileHeadertstringFileHeaderR7R8R3R'R R4theader(R*RGRARBRCR?R>R@((R t FileHeaders &&    (iiiiii(RR t__doc__R=RH(((R Rqs !cBstZdZeZdedZdZdZdZ dZ dZ dZ d Z d Zd Zeed Zd ZdZdZRS(s Class with methods to open, read, write, close, list zip files. z = ZipFile(file, mode="r", compression=ZIP_STORED) file: Either the path to the file, or a file-like object. If it is a path, the file will be opened and closed by ZipFile. mode: The mode can be either read "r", write "w" or append "a". compression: ZIP_STORED (no compression) or ZIP_DEFLATED (requires zlib). trcCs|tjon/|tjotp tdq?n tdd|_h|_g|_||_|i ddd|_ }t |toMd|_||_hdd<dd <d d <}t||||_n(d |_||_t|d d|_|djo|in|djon|d joQy$|i|ii|idWqtj o|iiddqXn.|ip|iid|_ntddS(s>Open the ZIP file with mode read "r", write "w" or append "a".s.Compression requires the (missing) zlib modules(That compression method is not supporteditbRRJR twtwbtasr+bitnameisMode must be "r", "w" or "a"N(t compressionRRtzlibt RuntimeErrorR*tdebugt NameToInfotfilelisttmodeR1tkeyt isinstancetfilet basestringt _filePassedR tmodeDictR tfptgetattrtNonet _GetContentst_RealGetContentsRt start_dirRR(R*RYRVRPRWR\((R R=sD         !         cCsOy|iWn:tj o.|ip|iid|_nnXdS(sORead the directory, making sure we close the file if the format is bad.N(R*RaRR[R]RR_(R*((R R`s   c Csl|i}t|}|p tdn|idjo |GHn|d}|d} |d|_|d|}|| }|idjodG| G|G|GHn| ||_ |i |i d d }x||jo|id }|d }|d d !tjo td ntit|}|idjo |GHn|i|t}t|}|i|t|_|i|t|_||t|t|t}|t||_|dd !\ |_|_|_|_|_ |_!} } |_$|_%|_&|dd!\|_'|_(|_)| d?d| d?d@| d@| d?| d?d@| d@df|_*|i+i,|||i-|i<|idjo dG|GHqqWx|i+D]} |i | id |id} | d d !t0jo tdntit1| } | id| t2| t3| _4|i| t2}|| i6jot7d| i6|fqqWdS(s/Read in the table of contents for the ZIP file.sFile is not a zip fileiiiii isgiven, inferred, offsetii.is&Bad magic number for central directoryi iiiii i?ttotalis Bad magic number for file headers3File name in directory "%s" and header "%s" differ.N(8R*R]RRRRStsize_cdt offset_cdR&txtconcatRbRRcRtcentdirtstringCentralDirRRtstructCentralDirt_CD_FILENAME_LENGTHR Rt_CD_EXTRA_FIELD_LENGTHR4t_CD_COMMENT_LENGTHt_CD_LOCAL_HEADER_OFFSETt header_offsetR6R5R7R8R9R3tttdRARBRCR:R;R<R2RUR!RTRtfheaderRFREt_FH_FILENAME_LENGTHt_FH_EXTRA_FIELD_LENGTHt file_offsettfnameR+RR(R*RhRgRcR RRvRdR]RrReRRqRpRf((R Rasd              IC   cCs.g}x!|iD]}|i|iqW|S(s+Return a list of file names in the archive.N(tlR*RURR!R (R*RRw((R tnamelist2s  cCs|iS(sJReturn a list of class ZipInfo instances for files in the archive.N(R*RU(R*((R tinfolist9scCsFddGHx6|iD]+}d|i}d|i||ifGHqWdS( s+Print a table of contents for the zip file.s%-46s %19s %12ss File Names Modified tSizes%d-%02d-%02d %02d:%02d:%02ds %-46s %s %12dN(s File Names Modified Rz(R*RUtzinfoR2tdateR RC(R*R|R{((R tprintdir>s    cCsIxB|iD]7}y|i|iWq tj o|iSq Xq WdS(s%Read all the files and check the CRC.N(R*RUR{RR R(R*R{((R ttestzipEs cCs |i|S(s,Return the instance of ZipInfo given 'name'.N(R*RTRO(R*RO((R tgetinfoMscCsb|id jo tdn|ip tdn|i|}|ii}|ii |i d|ii |i }|ii |d|itjon|itjodtp tdntid}|i|}|id|i}|o||}q.ntd |i|fti|}||ijotd |n|S( s)Return file bytes (as a string) for name.RJRNsread() requires mode "r" or "a"s3Attempt to read ZIP archive that was already closedis1De-compression requires the (missing) zlib moduleitZs-Unsupported compression method %d for file %ssBad CRC-32 for file %sN(RJRN(R*RVRRR]RROR{RtfileposRRuRRBtbytesR3RRRQt decompressobjtdct decompresstflushtexRtbinasciitcrc32tcrcRA(R*RORRRR{RR((R RQs2    cCs|i|ijo|iodG|iGHq1n|id jo tdn|ip tdn|it jot o tdn|it t fjo tdndS( s6Check for errors before writing a file to the archive.sDuplicate name:RLRNs write() requires mode "w" or "a"s4Attempt to write ZIP archive that was already closeds.Compression requires the (missing) zlib modules(That compression method is not supportedN(RLRN( R{R R*RTRSRVRRR]R3RRQR(R*R{((R t _writecheckss     cCsti|} ti| i}|dd!} |d jot || }nt || }| dd@d>|_ |d jo|i|_n ||_|i|t|d} d|_|ii|_d|_}d|_} d|_}|ii|i|ii|_|itjotiti ti!d}nd }x| i#dd } | pPn|t%| }t&i'| |}|o#|i(| } | t%| } n|ii| qVW| i)|o9|i*} | t%| } |ii| | |_n ||_||_||_|ii}|ii,|id d|iit-i.d |i|i|i|ii,|d|i/i0|||i1|i|dd>B|d B}|d d >|d d>B|dd B}t itt|i|i|i|i|i|i|||i|i|it|it|it|id|i|i|i }|ii"||ii"|i|ii"|i|ii"|iqGW|ii}t it$t%dd|||| | d }|ii"||ii'n|i(p|ii)nd|_dS(sJClose the file, and for mode "w" and "a" write the ending records.NRLRNiiii iiii i(RLRN(*R*R]R_RVtcountRtpos1RUR{R2R>R?R@RRDRjRiR6R5R7R8R9R3RARBRCR'R R4R&R;R<RoRhRtpos2RRRRR[R( R*RRhR{RR?R>RR@R((R Rs@   &&  $   (RR RIR_R]RR=R`RaRxRyR}R~RRRRRRR(((R Rs , E      " <  cBs#tZdZddZdZRS(sDClass to create ZIP archives with Python library files and packages.Rc Cstii|\} }tii|oitii|d}tii |o|od||f}n|}|i odG|GdG|GHn|i |dd!|\} } |i o dG| GHn|i| | ti|}|idx|D]}tii||} tii|\}}tii| o:tii tii| do|i| |qq|djoJ|i | dd!|\} } |i o dG| GHn|i| | qqWq|i o d G|GHnxti|D]}tii||} tii|\}}|djoJ|i | dd!|\} } |i o dG| GHn|i| | qqWne|ddjo td n|i |dd!|\} } |i o d G| GHn|i| | d S( s,Add all files from "pathname" to the ZIP archive. If pathname is a package directory, search the directory and all package subdirectories recursively for all *.py and enter the modules into the archive. If pathname is a plain directory, listdir *.py and enter all modules. Else, pathname must be a Python *.py file and the module will be put into the archive. Added modules are always module.pyo or module.pyc. This method will compile the module.py into module.pyc if necessary. s __init__.pys%s/%ssAdding package intasiitAddings.pysAdding files from directorys.Files added with writepy() must end with ".py"s Adding fileN(R/tpathtsplittpathnametdirROtisdirtjointinitnametisfiletbasenameR*RSt _get_codenameRvRRtlistdirtdirlisttremoveR tsplitexttroottexttwritepyRR( R*RRRRORRR RRRvRR((R R s^     "           c CsA|d} |d}|d}tii|o/ti|iti| ijo |}ntii| p%ti|iti| ijogdk }|i o dG| GHny|i | |dtWn |ij o}|iGHnX|}n|}tii|d}|od||f}n||fS(sReturn (filename, archivename) for the path. Given a module name path, return the correct file path and archive name, compiling if necessary. For example, given /python/lib/string, return (/python/lib/string.pyc, string). s.pys.pycs.pyoNt Compilingis%s/%s(Rtfile_pytfile_pyctfile_pyoR/RRRRRvt py_compileR*RStcompileR_RtPyCompileErrorterrtmsgRt archivenameR( R*RRRRRRRRvR((R RKs(   8 9     (RR RIRR(((R Rs  A(8RIRR/RRRQt ImportErrorR_t__all__t ExceptionRRRRRRRjRiRERFt _CD_SIGNATUREt_CD_CREATE_VERSIONt_CD_CREATE_SYSTEMt_CD_EXTRACT_VERSIONt_CD_EXTRACT_SYSTEMt _CD_FLAG_BITSt_CD_COMPRESS_TYPEt_CD_TIMEt_CD_DATEt_CD_CRCt_CD_COMPRESSED_SIZEt_CD_UNCOMPRESSED_SIZERkRlRmt_CD_DISK_NUMBER_STARTt_CD_INTERNAL_FILE_ATTRIBUTESt_CD_EXTERNAL_FILE_ATTRIBUTESRnt _FH_SIGNATUREt_FH_EXTRACT_VERSIONt_FH_EXTRACT_SYSTEMt_FH_GENERAL_PURPOSE_FLAG_BITSt_FH_COMPRESSION_METHODt_FH_LAST_MOD_TIMEt_FH_LAST_MOD_DATEt_FH_CRCt_FH_COMPRESSED_SIZEt_FH_UNCOMPRESSED_SIZERsRtRRRRR(4RERRtRRkRRRFRRRRsRRRRRRRRRlRRRRRRRRRjRRmRRRRRQRRRiRRRnRRRRRR/RRR((R t?sl    #8_