Plan 9 from Bell Labs’s /usr/web/sources/contrib/bichued/root/sys/lib/python/zipfile.pyc

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


�
��c@s_dZddkZddkZddkZddkZddkZddkZyddkZWnej
o
e	ZnXddddddd	d
dg	Z
defd��YZdefd
��YZ
eZdd>dZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZ dZ!d Z"d!Z#dZ$d"Z%d#Z&d$Z'd%Z(d&Z)d'Z*d(Z+d)Z,d*Z-d+Z.dZ/dZ0dZ1dZ2dZ3dZ4d Z5d!Z6dZ7d"Z8d#Z9d$Z:d,�Z;d-�Z<d.�Z=de>fd/��YZ?d	fd0��YZ@d
e@fd1��YZAe	d2�ZBeCd3joeB�ndS(4s
Read and write ZIP files.
i�Nt
BadZipfileterrort
ZIP_STOREDtZIP_DEFLATEDt
is_zipfiletZipInfotZipFilet	PyZipFiletLargeZipFilecBseZRS((t__name__t
__module__(((s/sys/lib/python/zipfile.pyRscBseZdZRS(su
    Raised when writing a zipfile, the zipfile requires ZIP64 extensions
    and those extensions are disabled.
    (R	R
t__doc__(((s/sys/lib/python/zipfile.pyRsiiiis<4s4H2lHsPKs<4s4B4HlLL5HLlsPKs<4s2B4HlLL2HsPKs<4slqlsPKs<4sqhhllqqqqsPKiiiiiii	i
iii
iiiiicCsRy8t|d�}t|�}|i�|otSnWntj
onXtS(s?Quickly see if file is a ZIP file by checking the magic number.trb(topent_EndRecDatatclosetTruetIOErrortFalse(tfilenametfpintendrec((s/sys/lib/python/zipfile.pyRRs
c
CsJtit�}|i||d�|i|�}tit|�\}}}}|tjo|Sn|djp
|djotd��ntit�}	|i|||	d�|i|	�}tit|�\
}}
}}}
}}}}}|t	jo|Sn|
|d<||d<||d<||d<||d<||d<|S(	sM
    Read the ZIP64 end-of-archive records and use that to update endrec
    iiis3zipfiles that span multiple disks are not supportediiii(
tstructtcalcsizetstructEndArchive64LocatortseektreadtunpacktstringEndArchive64LocatorRtstructEndArchive64tstringEndArchive64(RtoffsetRtlocatorSizetdatatsigtdisknotrelofftdiskstendArchiveSizetsztcreate_versiontread_versiontdisk_numtdisk_dirtdircountt	dircount2tdirsizet	diroffset((s/sys/lib/python/zipfile.pyt
_EndRecData64^s*
0






cCs�|idd�|i�d}|i�}|dd!tjo�|ddjoztit|�}t|�}|id�|i|d�|d	d
jp|d	djot	|d|�Sn|Snt
|d�}|i||d�|i�}|it�}|djo�tit|||d!�}t|�}||d}|d
t|�joe|i|�|i|||�|d	d
jp|d	djot	||||�Sn|Sq�ndS(s�Return 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.i��iiii�tti�i�l�iiNi(
RttellRtstringEndArchiveRRtstructEndArchivetlisttappendR0tmintrfindtlen(RtfilesizeR!Rt	END_BLOCKtstarttcomment((s/sys/lib/python/zipfile.pyR�s4%
"

"cBs5eZdZdZddd�Zd�Zd�ZRS(s>Class with attributes describing each file in the ZIP archive.t
orig_filenameRt	date_timet
compress_typeR>textrat
create_systemR(textract_versiontreservedt	flag_bitstvolumet
internal_attrt
external_attrt
header_offsettCRCt
compress_sizet	file_sizetNoNamei�iicCs||_|itd��}|djo|d|!}ntidjo)ti|jo|itid�}n||_||_t|_	d|_
d|_ti
djo
d|_n
d|_d|_d|_d|_d|_d|_d|_d|_dS(Nit/R2twin32ii(R?tfindtchrtostseptreplaceRR@RRAR>RBtsystplatformRCR(RDRERFRGRHRI(tselfRR@t	null_byte((s/sys/lib/python/zipfile.pyt__init__�s*	
 					
							c
Cs||i}|ddd>|dd>B|dB}|dd>|d	d>B|ddB}|id
@od}}}n|i}|i}|i}|i}|tjp
|tjold}|ti|dti	|�d	||�}d}d}t
d
|i�|_t
d
|i�|_ntit
t|i|i|i|i|||||t|i�t|��}	|	|i|S(s'Return the per-file header as a string.ii�i	iiiiiiis<hhqql�i-(R@RFRKRLRMRBtZIP64_LIMITRtpackRtmaxRDR(tstructFileHeadertstringFileHeaderRERAR:R(
RXtdttdosdatetdostimeRKRLRMRBtfmttheader((s/sys/lib/python/zipfile.pyt
FileHeader�s.	&&				 cCs�|i}ti}x�|o�|d|d �\}}|djo`|djo|d|dd!�}nv|djo|d|dd	!�}nO|d
jo|d|dd!�}n(|d
jo
d}ntd|f�d
}|idjp|idjo|||_|d7}n|idjp|idjo|||_|d7}n|idjp|idjo$|i}|||_|d7}q�n||d}qWdS(Ns<hhiiis<qqqiis<qqiis<qiisCorrupt extra field %si�l�((RBRRtRuntimeErrorRMRLRJ(RXRBRttptlntcountstidxtold((s/sys/lib/python/zipfile.pyt_decodeExtras4		






 
 
 	
(s
orig_filenamesfilenames	date_times
compress_typescommentsextras
create_systemscreate_versionsextract_versionsreserveds	flag_bitssvolumes
internal_attrs
external_attrs
header_offsetsCRCs
compress_sizes	file_size(i�iiiii(R	R
Rt	__slots__RZReRl(((s/sys/lib/python/zipfile.pyR�s,&	!cBs�eZdZdZdeed�Zd�Zd�Z	d�Z
d�Zd�Zd�Z
d	�Zd
�Zd�Zddd�Zd
�Zd�Zd�ZRS(sc Class with methods to open, read, write, close, list zip files.

    z = ZipFile(file, mode="r", compression=ZIP_STORED, allowZip64=True)

    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).
    allowZip64: if True ZipFile will create files with ZIP64 extensions when
                needed, otherwise it will raise an exception when this would
                be necessary.

    trcCs�||_t|_|tjon/|tjotp
td�qQn
td�d|_h|_g|_	||_
|idd�d|_}t
|t�oMd|_||_hdd<dd	<d
d<}t|||�|_n(d|_||_t|d
d�|_|djo|i�n�|d	jon�|djoQy$|i�|ii|id�Wq�tj
o|iidd�q�Xn.|ip|ii�d|_ntd�dS(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 supporteditbR2RRntwbtwsr+btaitnameisMode must be "r", "w" or "a"N(t_allowZip64Rt
_didModifyRRtzlibRftdebugt
NameToInfotfilelisttcompressionRUtmodet
isinstancet
basestringt_filePassedRR
tfptgetattrtNonet_GetContentst_RealGetContentsRt	start_dirRR(RXtfileR{Rzt
allowZip64tkeytmodeDict((s/sys/lib/python/zipfile.pyRZ<sH		

							!		






cCsOy|i�Wn:tj
o.|ip|ii�d|_n�nXdS(sORead the directory, making sure we close the file if the format
        is bad.N(R�RR~RRR�(RX((s/sys/lib/python/zipfile.pyR�js


c
Cs|i}t|�}|p
td�n|idjo	|GHn|d}|d}|d|_|dtjo|d|dd}n|d|}||}|id	jod
G|G|G|GHn|||_|i|id�|i|�}t	i
|�}d}x�|jo�|id�}	|d}|	dd
!tjo
td�nti
t|	�}	|id	jo	|	GHn|i|	t�}
t|
�}|i|	t�|_|i|	t�|_||	t|	t|	t}|	t|_|	dd!\|_|_|_|_|_|_}}|_|_|_|	dd!\|_|_ |_!|d?d|d?d@|d@|d?|d?d@|d@d	f|_"|i#�|i||_|i$i%|�||i&|i'<|id	jo
dG|GHqqWdS(s/Read in the table of contents for the ZIP file.sFile is not a zip fileiiiii	i8iisgiven, inferred, offsetii.is&Bad magic number for central directoryiiii�iii?ttotalN((RRRRwR>R[R�RRt	cStringIOtStringIOtstringCentralDirRRtstructCentralDirt_CD_FILENAME_LENGTHRt_CD_EXTRA_FIELD_LENGTHRBt_CD_COMMENT_LENGTHt_CD_LOCAL_HEADER_OFFSETRJR(RCRDRERFRARKRLRMRGRHRIR@RlRyR7RxR(
RXRRtsize_cdt	offset_cdtxtconcatR!R�tcentdirRtttd((s/sys/lib/python/zipfile.pyR�usX	
	






	
I&
cCs.g}x!|iD]}|i|i�qW|S(s+Return a list of file names in the archive.(RyR7R(RXtlR!((s/sys/lib/python/zipfile.pytnamelist�s

cCs|iS(sJReturn a list of class ZipInfo instances for files in the
        archive.(Ry(RX((s/sys/lib/python/zipfile.pytinfolist�scCsFddGHx6|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    R�(RyR@RRM(RXtzinfotdate((s/sys/lib/python/zipfile.pytprintdir�s
	

cCsIxB|iD]7}y|i|i�Wq
tj
o|iSq
Xq
WdS(s%Read all the files and check the CRC.N(RyRRR(RXR�((s/sys/lib/python/zipfile.pyttestzip�s
cCs|i|S(s,Return the instance of ZipInfo given 'name'.(Rx(RXRs((s/sys/lib/python/zipfile.pytgetinfo�sc
Cs
|idjo
td�n|ip
td�n|i|�}|ii�}|ii|id�|iid�}|dd!tjo
t	d�nt
it|�}|ii|t
�}|to|ii|t�n||ijot	d	|i|f�n|ii|i�}|ii|d�|itjon�|itjodtp
td
�ntid�}|i|�}|id�|i�}|o||}q�nt	d
|i|f�ti|�}	|	|ijot	d|�n|S(s)Return file bytes (as a string) for name.RnRrsread() requires mode "r" or "a"s3Attempt to read ZIP archive that was already closediiis Bad magic number for file headers3File name in directory "%s" and header "%s" differ.s1De-compression requires the (missing) zlib modulei�tZs-Unsupported compression method %d for file %ssBad CRC-32 for file %s(RnRr(R{RfRR�R3RRJRR_RRRR^t_FH_FILENAME_LENGTHt_FH_EXTRA_FIELD_LENGTHR?RLRARRRvt
decompressobjt
decompresstflushtbinasciitcrc32RK(
RXRsR�tfilepostfheadertfnametbytestdctextcrc((s/sys/lib/python/zipfile.pyR�sN




cCs
|i|ijo|iodG|iGHq1n|idjo
td�n|ip
td�n|itjoto
td�n|it	tfjo
td�n|i
tjo|ipt
d��q�n|itjo|ipt
d	��q	nd
S(s6Check for errors before writing a file to the archive.sDuplicate name:RqRrs 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 supporteds'Filesize would require ZIP64 extensionss+Zipfile size would require ZIP64 extensionsN(RqRr(RRxRwR{RfRRARRvRRMR[RtRRJ(RXR�((s/sys/lib/python/zipfile.pyt_writechecks(







cCsti|�}ti|i�}|dd!}|djo
|}ntiitii|�d�}x,|dti	ti
fjo|d}qjWt||�}|dd@d>|_|djo|i
|_n
||_|i|_d|_|ii�|_|i|�t|_t|d�}d|_}	d|_}
d|_}|ii|i��|itjotiti ti!d�}nd}xz|i"d
�}
|
pPn|t#|
�}t$i%|
|	�}	|o#|i&|
�}
|
t#|
�}
n|ii|
�q�|i'�|o9|i(�}
|
t#|
�}
|ii|
�|
|_n
||_|	|_||_|ii�}|ii)|id
d�|iit*i+d|i|i|i��|ii)|d�|i,i-|�||i.|i/<dS(sLPut the bytes from filename into the archive under the name
        arcname.iiii�lRi�iiis<lLLNi (0RStstatttimet	localtimetst_mtimeR�tpathtnormpatht
splitdriveRTtaltsepRRIRzRAtst_sizeRMRFRR3RJR�RRuR
RKRLtwriteReRRvtcompressobjtZ_DEFAULT_COMPRESSIONtDEFLATEDRR:R�R�tcompressRR�RRR\RyR7RxR(RXRtarcnameRAtsttmtimeR@R�RRKRLRMtcmprtbuftposition((s/sys/lib/python/zipfile.pyR�sj


" 
		
	




			
cCs�t|t�p4td|dtiti���}|i|_n|}t|�|_|ii	�|_
|i|�t|_
ti|�|_|itjoGtititid�}|i|�|i�}t|�|_n
|i|_|ii	�|_
|ii|i��|ii|�|ii�|id@o/|iitid|i|i|i��n|ii|�||i |i!<dS(s�Write a file into the archive.  The contents is the string
        'bytes'.  'zinfo_or_arcname' is either a ZipInfo instance or
        the name of the file in the archive.RR@i�is<lLLN("R|RR�R�RzRAR:RMRR3RJR�RRuR�R�RKRRvR�R�R�R�R�RLR�ReRFRR\RyR7RxR(RXtzinfo_or_arcnameR�R�tco((s/sys/lib/python/zipfile.pytwritestr[s2
	
cCs|i�dS(s2Call the "close()" method in case the user forgot.N(R(RX((s/sys/lib/python/zipfile.pyt__del__|scCs�|idjodSn|idjo�|io~d}|ii�}xD|iD]9}|d}|i}|ddd>|dd>B|d	B}|d
d>|dd>B|dd	B}g}|itjp|i	tjo0|i
|i�|i
|i	�d
}d
}	n|i}|i	}	|itjo|i
|i�d}
n
|i}
|i}|o\t
iddt|�ddt|�|�|}td|i�}td|i�}
n|i}|i}
t
itt|
|i||i|i|i|||i|	|t|i�t|�t|i�d|i|i|
�}|ii|�|ii|i�|ii|�|ii|i�qQW|ii�}|tjo�t
itt ddddd|||||�}|ii|�t
it!t"d|d�}|ii|�|ii�}t
it#t$dd||||dd�}|ii|�n<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.NRqRriii�i	iiiiil�i�s<hhtqii-i,(RqRr((RR�R{RuR3RyR@RMR[RLR7RJRBRR\R:R]RDR(R�R�RCRERFRARKRR>RHRIR�RRRRR5R4R�R~R(RXtcounttpos1R�R`RaRbRBRMRLRJt
extra_dataRDR(R�tpos2tzip64endrectzip64locrectpos3R((s/sys/lib/python/zipfile.pyR�s�

	&&
		
					!	
%
N(R	R
RR�RRRRZR�R�R�R�R�R�R�RR�R�R�R�R(((s/sys/lib/python/zipfile.pyR+s 
.		;							3	A	!	cBs#eZdZdd�Zd�ZRS(sDClass to create ZIP archives with Python library files and packages.R2c
Cs�tii|�\}}tii|�oitii|d�}tii|�o�|od||f}n|}|iodG|GdG|GHn|i|dd!|�\}}|io
dG|GHn|i||�ti	|�}|i
d�x�|D]�}	tii||	�}
tii|	�\}}tii|
�o:tiitii|
d��o|i|
|�q�q�djoJ|i|
dd!|�\}}|io
dG|GHn|i||�q���io
d	G|GHnxti	|�D]�}	tii||	�}
tii|	�\}}|djoJ|i|
dd!|�\}}|io
dG|GHn|i||�qqWne|ddjo
t
d
�n|i|dd!|�\}}|io
dG|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 intasii�tAddings.pysAdding files from directorys.Files added with writepy() must end with ".py"sAdding fileN(RSR�tsplittisdirtjointisfileRwt
_get_codenameR�tlistdirtremovetsplitexttwritepyRf(
RXtpathnametbasenametdirRstinitnameR�R�tdirlistRR�troottext((s/sys/lib/python/zipfile.pyR��s^



"










c
	CsD|d}|d}|d}tii|�o/ti|�iti|�ijo
|}n�tii|�p%ti|�iti|�ijojddk}|io
dG|GHny|i||dt	�Wn |i
j
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.pyoi�Nt	Compilingis%s/%s(
RSR�R�R�R�t
py_compileRwtcompileR�RtPyCompileErrortmsgR�(
RXR�R�tfile_pytfile_pyctfile_pyoR�R�terrtarchivename((s/sys/lib/python/zipfile.pyR�s*


%
%



(R	R
RR�R�(((s/sys/lib/python/zipfile.pyR�sAc

s�ddk}|id�}|djotid}n|p|ddjo|GHtid�n|ddjoTt|�d	jo|GHtid�nt|dd
�}|i�|i	�n%|ddjoOt|�d	jo|GHtid�nt|dd
�}|i
�dGHn�|ddjot|�djo|GHtid�nt|dd
�}|d	}x�|i�D]�}|id
�ot
ii||d	�}nt
ii||�}t
ii|�}t
ii|�pt
i|�nt|d�}|i|i|��|i	�q�W|i	�n�|ddjo�t|�djo|GHtid�n�fd��t|dddt�}x.|d	D]"}	�||	t
ii|	��q�W|i	�ndS(Ni�sE        Usage:
            zipfile.py -l zipfile.zip        # Show listing of a zipfile
            zipfile.py -t zipfile.zip        # Test if a zipfile is valid
            zipfile.py -e zipfile.zip target # Extract zipfile into target dir
            zipfile.py -c zipfile.zip src ... # Create zipfile from sources
        iis-ls-cs-es-tiRnsDone testingis./Rpcs�tii|�o|i||t�n`tii|�oLxIti|�D]4}�|tii||�tii||��qMWndS(N(RSR�R�R�RR�R�R�(tzfR�tzippathtnm(taddToZip(s/sys/lib/python/zipfile.pyR�tsRqR�(s-ls-cs-es-t(ttextwraptdedentR�RVtargvtexitR:RR�RR�R�t
startswithRSR�R�tdirnametexiststmakedirsR
R�RRR�(
targsR�tUSAGER�toutR�ttgtttgtdirRtsrc((R�s/sys/lib/python/zipfile.pytmain:sd	


	

	 t__main__(DRRRSR�RVR�R�RvtImportErrorR�t__all__t	ExceptionRRRR[RRR5R4R�R�R^R_RRRRt
_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_SIZER�R�R�t_CD_DISK_NUMBER_STARTt_CD_INTERNAL_FILE_ATTRIBUTESt_CD_EXTERNAL_FILE_ATTRIBUTESR�t
_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_SIZER�R�RR0RtobjectRRRRR	(((s/sys/lib/python/zipfile.pys<module>s�0		"	'��cI


Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].