ó
âT€Mc           @   s  d  Z  d d l Z d d l Z d d l Z d d l Z d d l Z e j e j ƒ Z	 d e j
 f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ d d d „ Z d Z d d d „ Z d „  Z d „  Z d „  Z d d d d „ Z d S(   sŸ   
An incremental approach to unzipping files.  This allows you to unzip a little
bit of a file at a time, which means you can report progress as a file unzips.
iÿÿÿÿNt   ChunkingZipFilec           B   s   e  Z d  Z d „  Z RS(   sq   
    A ZipFile object which, with readfile(), also gives you access to a
    filelike object for each entry.
    c         C   s|  |  j  d k r t d ƒ ‚ n  |  j s6 t d ƒ ‚ n  |  j | ƒ } |  j j | j d ƒ |  j j t ƒ } | d d !t j	 k r• t j
 d ƒ ‚ n  t j t j | ƒ } |  j j | t j ƒ } | t j rê |  j j | t j ƒ n  | | j k rt j
 d | j | f ƒ ‚ n  | j t j k r:t |  | j ƒ S| j t j k r\t |  | j ƒ St j
 d	 | j | f ƒ ‚ d
 S(   s3   
        Return file-like object for name.
        t   rt   as   read() requires mode "r" or "a"s3   Attempt to read ZIP archive that was already closedi    i   s    Bad magic number for file headers3   File name in directory "%s" and header "%s" differ.s-   Unsupported compression method %d for file %sN(   R   R   (   t   modet   RuntimeErrort   fpt   getinfot   seekt   header_offsett   readt   _fileHeaderSizet   zipfilet   stringFileHeadert
   BadZipfilet   structt   unpackt   structFileHeadert   _FH_FILENAME_LENGTHt   _FH_EXTRA_FIELD_LENGTHt   orig_filenamet   compress_typet
   ZIP_STOREDt   ZipFileEntryt   compress_sizet   ZIP_DEFLATEDt   DeflatedZipFileEntry(   t   selft   namet   zinfot   fheadert   fname(    (    s<   /usr/lib/python2.7/dist-packages/twisted/python/zipstream.pyt   readfile   s2    	(   t   __name__t
   __module__t   __doc__R   (    (    (    s<   /usr/lib/python2.7/dist-packages/twisted/python/zipstream.pyR       s   t
   _FileEntryc           B   sV   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(	   s!  
    Abstract superclass of both compressed and uncompressed variants of
    file-like objects within a zip archive.

    @ivar chunkingZipFile: a chunking zip file.
    @type chunkingZipFile: L{ChunkingZipFile}

    @ivar length: The number of bytes within the zip file that represent this
    file.  (This is the size on disk, not the number of decompressed bytes
    which will result from reading it.)

    @ivar fp: the underlying file object (that contains pkzip data).  Do not
    touch this, please.  It will quite likely move or go away.

    @ivar closed: File-like 'closed' attribute; True before this file has been
    closed, False after.
    @type closed: L{bool}

    @ivar finished: An older, broken synonym for 'closed'.  Do not touch this,
    please.
    @type finished: L{int}
    c         C   s7   | |  _  |  j  j |  _ | |  _ d |  _ t |  _ d S(   sC   
        Create a L{_FileEntry} from a L{ChunkingZipFile}.
        i    N(   t   chunkingZipFileR   t   lengtht   finishedt   Falset   closed(   R   R$   R%   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/python/zipstream.pyt   __init__V   s
    			c         C   s   t  S(   sD   
        Returns false because zip files should not be ttys
        (   R'   (   R   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/python/zipstream.pyt   isattya   s    c         C   s   t  |  _ d |  _ |  ` d S(   s/   
        Close self (file-like object)
        i   N(   t   TrueR(   R&   R   (   R   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/python/zipstream.pyt   closeh   s    		c            sG   d } x: t  ‡  f d †  d ƒ D]  } | | 7} | d k r Pq q W| S(   s   
        Read a line.
        t    c              s   ˆ  j  d ƒ S(   Ni   (   R	   (    (   R   (    s<   /usr/lib/python2.7/dist-packages/twisted/python/zipstream.pyt   <lambda>v   s    s   
(   t   iter(   R   t   bytest   byte(    (   R   s<   /usr/lib/python2.7/dist-packages/twisted/python/zipstream.pyt   readlineq   s    
c         C   s#   |  j  ƒ  } | r | St ƒ  ‚ d S(   si   
        Implement next as file does (like readline, except raises StopIteration
        at EOF)
        N(   R2   t   StopIteration(   R   t   nextline(    (    s<   /usr/lib/python2.7/dist-packages/twisted/python/zipstream.pyt   next}   s    c         C   s
   t  |  ƒ S(   s1   
        Returns a list of all the lines
        (   t   list(   R   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/python/zipstream.pyt	   readlinesˆ   s    c         C   s   |  S(   s/   
        Returns an iterator (so self)
        (    (   R   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/python/zipstream.pyt
   xreadlines   s    c         C   s   |  S(   s/   
        Returns an iterator (so self)
        (    (   R   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/python/zipstream.pyt   __iter__–   s    (   R    R!   R"   R)   R*   R,   R2   R5   R7   R8   R9   (    (    (    s<   /usr/lib/python2.7/dist-packages/twisted/python/zipstream.pyR#   ?   s   								R   c           B   s,   e  Z d  Z d „  Z d „  Z d d „ Z RS(   sJ   
    File-like object used to read an uncompressed entry in a ZipFile
    c         C   s    t  j |  | | ƒ d |  _ d  S(   Ni    (   R#   R)   t	   readBytes(   R   R$   R%   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/python/zipstream.pyR)   £   s    c         C   s   |  j  S(   N(   R:   (   R   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/python/zipstream.pyt   tell¨   s    c         C   s©   | d  k r |  j |  j } n  | d k s4 |  j r8 d S|  j j j t | |  j |  j ƒ ƒ } |  j t | ƒ 7_ |  j |  j k s™ t | ƒ | k  r¥ d |  _ n  | S(   Ni    R-   i   (	   t   NoneR%   R:   R&   R$   R   R	   t   mint   len(   R   t   nt   data(    (    s<   /usr/lib/python2.7/dist-packages/twisted/python/zipstream.pyR	   ¬   s    $N(   R    R!   R"   R)   R;   R<   R	   (    (    (    s<   /usr/lib/python2.7/dist-packages/twisted/python/zipstream.pyR   ž   s   		R   c           B   s,   e  Z d  Z d „  Z d „  Z d d „ Z RS(   sE   
    File-like object used to read a deflated entry in a ZipFile
    c         C   sD   t  j |  | | ƒ d |  _ d |  _ t j d ƒ |  _ d |  _ d  S(   Ni    iñÿÿÿR-   (   R#   R)   t   returnedBytesR:   t   zlibt   decompressobjt   decompt   buffer(   R   R$   R%   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/python/zipstream.pyR)   ¿   s
    		c         C   s   |  j  S(   N(   RA   (   R   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/python/zipstream.pyR;   Ç   s    c         C   sÈ  |  j  r d S| d  k rÀ |  j g } | j |  j j |  j j j |  j	 |  j
 ƒ ƒ ƒ | j |  j j d ƒ ƒ | j |  j j ƒ  ƒ d |  _ d |  _  d j | ƒ } |  j t | ƒ 7_ | SxË t |  j ƒ | k  r|  j j j t | d |  j	 |  j
 ƒ ƒ } |  j
 t | ƒ 7_
 | so|  j |  j j d ƒ |  j j ƒ  } d |  _  d |  _ |  j t | ƒ 7_ | S|  j |  j j | ƒ 7_ qÃ W|  j |  } |  j | |  _ |  j t | ƒ 7_ | Sd  S(   NR-   t   Zi   i   (   R&   R<   RE   t   appendRD   t
   decompressR$   R   R	   R%   R:   t   flusht   joinRA   R>   R=   (   R   R?   t   resultR@   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/python/zipstream.pyR	   Ë   s<    				&		N(   R    R!   R"   R)   R;   R<   R	   (    (    (    s<   /usr/lib/python2.7/dist-packages/twisted/python/zipstream.pyR   º   s   		t   .i    c         C   s!   x t  |  | | ƒ D] } q Wd S(   sG  
    Unzip the file

    @param filename: the name of the zip file
    @param directory: the directory into which the files will be
    extracted
    @param overwrite: if on, overwrite files when they exist.  You can
    still get an error if you try to create a directory over a file
    with the same name or vice-versa.
    N(   t	   unzipIter(   t   filenamet	   directoryt	   overwritet   i(    (    s<   /usr/lib/python2.7/dist-packages/twisted/python/zipstream.pyt   unzipñ   s    i   c         c   sO  t  j |  d ƒ } | j ƒ  } t j j | ƒ s@ t j | ƒ n  t | j ƒ  ƒ } xö | D]î } | d 8} | j | ƒ j	 t
 @} t j j | | ƒ } | r¿ t j j | ƒ sBt j | ƒ qBnƒ t j j | ƒ d }	 t j j |	 ƒ s÷ t j |	 ƒ n  | st j j | ƒ rBt | d ƒ }
 |
 j | j | ƒ ƒ |
 j ƒ  n  | VqY Wd S(   s¢   
    Return a generator for the zipfile.  This implementation will yield
    after every file.

    The value it yields is the number of files left to unzip.
    R   i   i    t   wbN(   R   t   ZipFilet   namelistt   ost   patht   existst   makedirsR>   R   t   external_attrt   DIR_BITRJ   t   splitt   filet   writeR	   R,   (   RN   RO   RP   t   zft   namest	   remainingt   entryt   isdirt   ft   fdirt   outfile(    (    s<   /usr/lib/python2.7/dist-packages/twisted/python/zipstream.pyRM     s(    
c         C   s@   d } t  |  ƒ } x' | j ƒ  D] } | t | | ƒ 7} q W| S(   sr   
    Predict the number of chunks that will be extracted from the entire
    zipfile, given chunksize blocks.
    i    (   R    t   infolistt   countFileChunks(   RN   t	   chunksizet   totalchunksR_   t   info(    (    s<   /usr/lib/python2.7/dist-packages/twisted/python/zipstream.pyt   countZipFileChunks"  s
    c         C   s;   t  |  j | ƒ \ } } | d k r1 | d 7} n  | p: d S(   s9  
    Count the number of chunks that will result from the given L{ZipInfo}.

    @param zipinfo: a L{zipfile.ZipInfo} instance describing an entry in a zip
    archive to be counted.

    @return: the number of chunks present in the zip file.  (Even an empty file
    counts as one chunk.)
    @rtype: L{int}
    i    i   (   t   divmodt	   file_size(   t   zipinfoRi   t   countt   extra(    (    s<   /usr/lib/python2.7/dist-packages/twisted/python/zipstream.pyRh   .  s    c         C   s2   t  j d t d ƒ t j |  ƒ } t | j ƒ  ƒ S(   s£   
    Count the number of entries in a zip archive.  (Don't use this function.)

    @param filename: The filename of a zip archive.
    @type filename: L{str}
    s"   countZipFileEntries is deprecated.i   (   t   warningst   warnt   DeprecationWarningR   RT   R>   RU   (   RN   R_   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/python/zipstream.pyt   countZipFileEntries?  s    	
i   c         c   sÔ  t  |  d ƒ } t j j | ƒ s1 t j | ƒ n  t |  | ƒ } | j ƒ  } | j ƒ  } xut | | ƒ D]d\ } }	 |	 j	 t
 @}
 t j j | | ƒ } |
 rÐ t j j | ƒ s¾ t j | ƒ n  | d 8} | Vqh t j j | ƒ d } t j j | ƒ st j | ƒ n  | s!t j j | ƒ r´t | d ƒ } | j | ƒ } |	 j d k r`| d 8} | Vn  xD | j ƒ  |	 j k  r¦| j | ƒ } | j | ƒ | d 8} | VqcW| j ƒ  qh | t |	 | ƒ 8} | Vqh Wd S(   sï   
    Return a generator for the zipfile.  This implementation will yield after
    every chunksize uncompressed bytes, or at the end of a file, whichever
    comes first.

    The value it yields is the number of chunks left to unzip.
    R   i   i    RS   N(   R    RV   RW   RX   RY   Rl   RU   Rg   t   zipRZ   R[   RJ   R\   R]   R   Rn   R;   R	   R^   R,   Rh   (   RN   RO   RP   Ri   t   czfRa   R`   t   infosRb   Rk   Rc   Rd   Re   Rf   R   t   hunk(    (    s<   /usr/lib/python2.7/dist-packages/twisted/python/zipstream.pyt   unzipIterChunkyL  s>    	


	(   R"   Rr   R   t   os.pathRV   RB   R   t   calcsizeR   R
   RT   R    t   objectR#   R   R   RR   R[   RM   Rl   Rh   Ru   Rz   (    (    (    s<   /usr/lib/python2.7/dist-packages/twisted/python/zipstream.pyt   <module>   s$   -_7!			