m
fIc           @   s  d  Z  d k Z d k Z d k Z d k Z y d k Z Wn e j
 o e Z n Xd d d d d d d d	 g Z d e	 f d
     YZ
 e
 Z d Z d Z d Z d Z d Z d Z d Z d Z d Z d Z d Z d Z d Z d Z d Z d Z d Z d Z d Z d Z d Z  d Z! d Z" d  Z# d! Z$ d" Z% d# Z& d Z' d Z( d Z) d Z* d Z+ d Z, d Z- d Z. d Z/ d Z0 d Z1 d Z2 d$   Z3 d%   Z4 d f  d&     YZ5 d f  d'     YZ6 d	 e6 f d(     YZ7 d S()   s   Read and write ZIP files.Nt
   BadZipfilet   errort
   ZIP_STOREDt   ZIP_DEFLATEDt
   is_zipfilet   ZipInfot   ZipFilet	   PyZipFilec           B   s   t  Z RS(   N(   t   __name__t
   __module__(    (    (    t$   /mit/python/lib/python2.4/zipfile.pyR       s   i    i   s   <4s4H2lHs   PKs   <4s4B4HlLL5HLls   PKs   <4s2B4HlLL2Hs   PKi   i   i   i   i   i   i   i	   i
   i   i   i   i   i   i   i   i   c         C   sR   y8 t  |  d  } t |  } | i   | o t Sn Wn t j
 o n Xt S(   s?   Quickly see if file is a ZIP file by checking the magic number.t   rbN(	   t   opent   filenamet   fpint   _EndRecDatat   endrect   closet   Truet   IOErrort   False(   R   R   R   (    (    R
   R   B   s     
c         C   sd  |  i d d  |  i   d } |  i   } | d d !t j oU | d d j oD t i t	 |  } t |  } | i d  | i | d  | Sn t | d	 d  } |  i | | d  |  i   } | i t  } | d j o| t i t	 | | | d ! } t |  } | | d } | d
 t |  j o* | i |  | i | | |  | Sq`n d S(   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.ii   i   i    i   it     t    i   i   N(   R   t   seekt   tellt   filesizet   readt   datat   stringEndArchivet   structt   unpackt   structEndArchiveR   t   listt   appendt   mint	   END_BLOCKt   rfindt   startt   commentt   len(   R   R#   R&   R%   R   R   R   (    (    R
   R   N   s.     %c           B   s&   t  Z d  Z d d d  Z d   Z RS(   s>   Class with attributes describing each file in the ZIP archive.t   NoNamei  i   i    c         C   s   | |  _ | i t d   } | d j o | d | !} n t i d j o | i t i d  } n | |  _  | |  _	 t
 |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d  S(   Ni    t   /R   i   (   R   t   selft   orig_filenamet   findt   chrt	   null_bytet   ost   sept   replacet	   date_timeR   t   compress_typeR&   t   extrat   create_systemt   create_versiont   extract_versiont   reservedt	   flag_bitst   volumet   internal_attrt   external_attr(   R*   R   R2   R.   (    (    R
   t   __init__t   s&    													c         C   s   |  i } | d d d >| d d >B| d B} | d d >| d	 d >B| d d B} |  i d
 @o d } } } n |  i } |  i } |  i } t	 i
 t t |  i |  i |  i |  i | | | | | t |  i  t |  i   } | |  i |  i S(   s'   Return the per-file header as a string.i    i  i	   i   i   i   i   i   i   i   N(   R*   R2   t   dtt   dosdatet   dostimeR9   t   CRCt   compress_sizet	   file_sizeR   t   packt   structFileHeadert   stringFileHeaderR7   R8   R3   R'   R   R4   t   header(   R*   RG   RA   RB   RC   R?   R>   R@   (    (    R
   t
   FileHeader   s     	&&			(   i  i   i   i    i    i    (   R   R	   t   __doc__R=   RH   (    (    (    R
   R   q   s    !c           B   s   t  Z d  Z e Z d e d  Z d   Z d   Z d   Z	 d   Z
 d   Z d   Z d	   Z d
   Z d   Z e e d  Z d   Z d   Z d   Z RS(   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).
    t   rc         C   s  | t j o n/ | t j o t p t d  q? n
 t d  d |  _ h  |  _ g  |  _ | |  _  | i
 d d  d |  _	 } t | t  oM d |  _ | |  _ h  d d <d d	 <d
 d <} t | | |  |  _ n( d |  _ | |  _ t | d d  |  _ | d j o |  i   n | d j o n | d
 j oQ y$ |  i   |  i i |  i d  Wqt j
 o |  i i d d  qXn. |  i p |  i i   d |  _ n t d  d S(   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 supportedi    t   bR   RJ   R   t   wt   wbt   as   r+bi   t   namei   s   Mode must be "r", "w" or "a"N(   t   compressionR   R   t   zlibt   RuntimeErrorR*   t   debugt
   NameToInfot   filelistt   modeR1   t   keyt
   isinstancet   filet
   basestringt   _filePassedR   t   modeDictR   t   fpt   getattrt   Nonet   _GetContentst   _RealGetContentsR   t	   start_dirR    R   (   R*   RY   RV   RP   RW   R\   (    (    R
   R=      sD     							!		

c         C   sO   y |  i   Wn: t j
 o. |  i p |  i i   d |  _ n   n Xd S(   sO   Read the directory, making sure we close the file if the format
        is bad.N(   R*   Ra   R    R[   R]   R   R_   (   R*   (    (    R
   R`      s     
c         C   sl  |  i } t |  } | p t d  n |  i d j o	 | GHn | d } | d }
 | d |  _ | d | } | |
 } |  i d j o d G|
 G| G| GHn |
 | |  _ | i |  i d	  d	 } x| | j  o| i d
  } | d
 } | d	 d !t j o t d  n t i t |  } |  i d j o	 | GHn | i | t  } t |  } | i | t  | _ | i | t  | _ | | t | t | t } | t | | _ | d d !\ | _ | _ | _ | _ | _  | _! } } | _$ | _% | _& | d d !\ | _' | _( | _) | d ?d | d ?d @| d @| d ?| d ?d @| d @d f | _* |  i+ i, |  | |  i- | i <|  i d j o d G| GHq q Wx |  i+ D] } | i | i d	  | i d  }	 |	 d	 d !t0 j o t d  n t i t1 |	  }	 | i d |	 t2 |	 t3 | _4 | i |	 t2  } | | i6 j o t7 d | i6 | f  qqWd S(   s/   Read in the table of contents for the ZIP file.s   File is not a zip filei   i   i   i   i	   i   s   given, inferred, offseti    i.   i   s&   Bad magic number for central directoryi   i   i   i  i   i   i?   t   totali   s    Bad magic number for file headers3   File name in directory "%s" and header "%s" differ.N(8   R*   R]   R   R   R    RS   t   size_cdt	   offset_cdR&   t   xt   concatRb   R   Rc   R   t   centdirt   stringCentralDirR   R   t   structCentralDirt   _CD_FILENAME_LENGTHR   R   t   _CD_EXTRA_FIELD_LENGTHR4   t   _CD_COMMENT_LENGTHt   _CD_LOCAL_HEADER_OFFSETt   header_offsetR6   R5   R7   R8   R9   R3   t   tt   dRA   RB   RC   R:   R;   R<   R2   RU   R!   RT   R   t   fheaderRF   RE   t   _FH_FILENAME_LENGTHt   _FH_EXTRA_FIELD_LENGTHt   file_offsett   fnameR+   RR   (   R*   Rh   Rg   Rc   R   R   Rv   Rd   R]   Rr   Re   R   Rq   Rp   Rf   (    (    R
   Ra      sd     		


 
	IC
  c         C   s.   g  } x! |  i D] } | i | i  q W| S(   s+   Return a list of file names in the archive.N(   t   lR*   RU   R   R!   R   (   R*   R   Rw   (    (    R
   t   namelist2  s     
 c         C   s   |  i S(   sJ   Return a list of class ZipInfo instances for files in the
        archive.N(   R*   RU   (   R*   (    (    R
   t   infolist9  s     c         C   sF   d d GHx6 |  i D]+ } d | i } d | i | | i f GHq Wd S(	   s+   Print a table of contents for the zip file.s   %-46s %19s %12ss	   File Names   Modified    t   Sizes   %d-%02d-%02d %02d:%02d:%02ds   %-46s %s %12dN(   s	   File Names   Modified    Rz   (   R*   RU   t   zinfoR2   t   dateR   RC   (   R*   R|   R{   (    (    R
   t   printdir>  s     	
 c         C   sI   xB |  i D]7 } y |  i | i  Wq
 t j
 o | i Sq
 Xq
 Wd S(   s%   Read all the files and check the CRC.N(   R*   RU   R{   R   R   R    (   R*   R{   (    (    R
   t   testzipE  s     
 c         C   s   |  i | S(   s,   Return the instance of ZipInfo given 'name'.N(   R*   RT   RO   (   R*   RO   (    (    R
   t   getinfoM  s     c         C   sb  |  i d j o t d  n |  i p t d  n |  i |  } |  i i   } |  i i	 | i
 d  |  i i | i  } |  i i	 | d  | i t j o n | i t j od t p t d  n t i d  } | i |  } | i d  | i   } | o | | } q.n t d	 | i | f  t i |  } | | i j o t d
 |  n | S(   s)   Return file bytes (as a string) for name.RJ   RN   s   read() requires mode "r" or "a"s3   Attempt to read ZIP archive that was already closedi    s1   De-compression requires the (missing) zlib moduleit   Zs-   Unsupported compression method %d for file %ss   Bad CRC-32 for file %sN(   RJ   RN   (   R*   RV   RR   R]   R   RO   R{   R   t   fileposR   Ru   R   RB   t   bytesR3   R   R   RQ   t   decompressobjt   dct
   decompresst   flusht   exR    t   binasciit   crc32t   crcRA   (   R*   RO   R   R   R   R{   R   R   (    (    R
   R   Q  s2     
c         C   s   | i |  i j o |  i o d G| i GHq1 n |  i d	 j o t d  n |  i p t d  n | i t	 j o t
 o t d  n | i t t	 f j o t d  n d S(
   s6   Check for errors before writing a file to the archive.s   Duplicate name:RL   RN   s    write() requires mode "w" or "a"s4   Attempt to write ZIP archive that was already closeds.   Compression requires the (missing) zlib modules(   That compression method is not supportedN(   RL   RN   (   R{   R   R*   RT   RS   RV   RR   R]   R3   R   RQ   R   (   R*   R{   (    (    R
   t   _writechecks  s     

c         C   s  t  i |  } t i | i  } | d d !} | d j o t | |  } n t | |  } | d d @d >| _ | d j o |  i | _ n
 | | _ |  i |  t | d  }
 d | _ |  i i   | _ d | _ } d | _ }	 d | _ } |  i i | i    |  i i   | _ | i t j o t i t i  t i! d  } n d } x |
 i# d d	  } | p Pn | t% |  } t& i' | |  } | o# | i( |  } |	 t% |  }	 n |  i i |  qVW|
 i)   | o9 | i*   } |	 t% |  }	 |  i i |  |	 | _ n
 | | _ | | _ | | _ |  i i   } |  i i, | i d
 d  |  i i t- i. d | i | i | i   |  i i, | d  |  i/ i0 |  | |  i1 | i <d S(   sL   Put the bytes from filename into the archive under the name
        arcname.i    i   i  l    R   ii   i   i   i   s   <lLLN(2   R/   t   statR   t   stt   timet	   localtimet   st_mtimet   mtimeR2   t   arcnameR_   R   R{   R<   R3   R*   RP   R   R   R]   R9   R   Ro   RA   RB   RC   t   writeRH   Ru   R   RQ   t   compressobjt   Z_DEFAULT_COMPRESSIONt   DEFLATEDt   cmprR   t   bufR'   R   R   t   compressR   R   t   positionR   R   RD   RU   R!   RT   (   R*   R   R   R3   R   R   RA   RC   R{   RB   R]   R   R2   R   R   (    (    R
   R     sf     		  
			c         C   s  t  | t  p4 t d | d t i t i     } |  i | _ n | } |  i	 |  t
 |  | _ t i |  | _ | i t j oG t i t i t i d  } | i |  | i   } t
 |  | _ n | i | _ |  i i   | _ |  i i | i    |  i i   | _ |  i i |  | i d @o/ |  i i t  i! d | i | i | i   n |  i" i# |  | |  i$ | i% <d S(   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.R   R2   ii   s   <lLLN(&   RX   t   zinfo_or_arcnameR   R   R   R{   R*   RP   R3   R   R'   R   RC   R   R   RA   R   RQ   R   R   R   t   coR   R   RB   R]   R   Ro   R   RH   Ru   R9   R   RD   RU   R!   RT   R   (   R*   R   R   R   R{   (    (    R
   t   writestr  s0     	c         C   s   |  i   d S(   s2   Call the "close()" method in case the user forgot.N(   R*   R   (   R*   (    (    R
   t   __del__  s     c   
      C   s  |  i d j o d Sn |  i d j od } |  i i   }	 x@|  i D]5} | d } | i	 } | d d d >| d d >B| d	 B} | d
 d >| d d >B| d d	 B} t i t t | i | i | i | i | i | i | | | i | i | i t | i  t | i  t | i  d | i | i | i   } |  i i" |  |  i i" | i  |  i i" | i  |  i i" | i  qG W|  i i   } t i t$ t% d d | | | |	 |	 d 	 } |  i i" |  |  i i'   n |  i( p |  i i)   n d |  _ d S(   sJ   Close the file, and for mode "w" and "a" write the ending
        records.NRL   RN   i    i   i  i	   i   i   i   i   i   (   RL   RN   (*   R*   R]   R_   RV   t   countR   t   pos1RU   R{   R2   R>   R?   R@   R   RD   Rj   Ri   R6   R5   R7   R8   R9   R3   RA   RB   RC   R'   R   R4   R&   R;   R<   Ro   Rh   R   t   pos2R   R   R   R   R[   R   (
   R*   R   Rh   R{   R   R?   R>   R   R@   R   (    (    R
   R     s@     
 
	&&		$
(   R   R	   RI   R_   R]   R   R=   R`   Ra   Rx   Ry   R}   R~   R   R   R   R   R   R   R   (    (    (    R
   R      s    	 ,		E						"	<		c           B   s#   t  Z d  Z d d  Z d   Z RS(   sD   Class to create ZIP archives with Python library files and packages.R   c         C   s  t  i i |  \ } } t  i i |  oit  i i | d  } t  i i	 |  o| o d | | f } n | } |  i o d G| Gd G| GHn |  i | d d !|  \ }
 }	 |  i o d G|	 GHn |  i |
 |	  t  i |  } | i d  x| D] } t  i i | |  } t  i i |  \ } } t  i i |  o: t  i i	 t  i i | d   o |  i | |  qq | d j oJ |  i | d d !|  \ }
 }	 |  i o d G|	 GHn |  i |
 |	  q q Wq|  i o d	 G| GHn xt  i |  D] } t  i i | |  } t  i i |  \ } } | d j oJ |  i | d d !|  \ }
 }	 |  i o d G|	 GHn |  i |
 |	  qqWne | d d j o t d
  n |  i | d d !|  \ }
 }	 |  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/%ss   Adding package int   asi    it   Addings   .pys   Adding files from directorys.   Files added with writepy() must end with ".py"s   Adding fileN(   R/   t   patht   splitt   pathnamet   dirRO   t   isdirt   joint   initnamet   isfilet   basenameR*   RS   t   _get_codenameRv   R   R   t   listdirt   dirlistt   removeR   t   splitextt   roott   extt   writepyRR   (   R*   R   R   R   RO   R   R   R   R   R   Rv   R   R   (    (    R
   R   
  s^     

 "

 

c   
      C   sA  | d }	 | d } | d } t i i |  o/ t i |  i t i |	  i j o
 | } n t i i |  p% t i |  i t i |	  i j  og d k
 } |  i o d G|	 GHn y | i |	 | d t  Wn  | i j
 o } | i GHn X| } n | } t i i |  d } | o d | | f } n | | f S(   s   Return (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	   Compilingi   s   %s/%s(   R   t   file_pyt   file_pyct   file_pyoR/   R   R   R   R   Rv   t
   py_compileR*   RS   t   compileR_   R   t   PyCompileErrort   errt   msgR   t   archivenameR   (
   R*   R   R   R   R   R   R   R   Rv   R   (    (    R
   R   K  s(     


8
9	

(   R   R	   RI   R   R   (    (    (    R
   R     s    A(8   RI   R   R/   R   R   RQ   t   ImportErrorR_   t   __all__t	   ExceptionR    R   R   R   R   R   Rj   Ri   RE   RF   t   _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_SIZERk   Rl   Rm   t   _CD_DISK_NUMBER_STARTt   _CD_INTERNAL_FILE_ATTRIBUTESt   _CD_EXTERNAL_FILE_ATTRIBUTESRn   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_SIZERs   Rt   R   R   R   R   R   (4   RE   R   Rt   R   Rk   R   R   RF   R   R   R   Rs   R   R   R   R   R   R   R   R   Rl   R   R   R   R   R   R   R   R   Rj   R   Rm   R   R   R   R   RQ   R   R   Ri   R   R   Rn   R    R   R   R   R   R/   R   R   R   (    (    R
   t   ?   sl   			#8 _