
gbc        	   @   s  d  Z  d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z y d d l Z e j	 Z	 Wn  e
 k
 r e Z e j	 Z	 n Xd d d d d d d	 d
 d g	 Z d e f d     YZ d e f d     YZ e Z d d >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 Z5 d Z6 d  Z7 d! Z8 d Z9 d" Z: d# Z; d$ Z< d,   Z= d-   Z> d.   Z? d e@ f d/     YZA d0 f  d1     YZB d2 f  d3     YZC d	 f  d4     YZD d
 eD f d5     YZE e d6  ZF eG d7 k reF   n  d S(8   s   
Read and write ZIP files.
iNt
   BadZipfilet   errort
   ZIP_STOREDt   ZIP_DEFLATEDt
   is_zipfilet   ZipInfot   ZipFilet	   PyZipFilet   LargeZipFilec           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyR       s   c           B   s   e  Z d  Z RS(   su   
    Raised when writing a zipfile, the zipfile requires ZIP64 extensions
    and those extensions are disabled.
    (   R	   R
   t   __doc__(    (    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyR      s   i   i   i    i   s   <4s4H2LHs   PKs   <4s4B4HLLL5HLLs   PKs   <4s2B4HLLL2Hs   PKs   <4sLQLs   PKs   <4sQHHLLQQQQs   PKi   i   i   i   i   i   i	   i
   i   i   i   i   i   i   i   i   c         C   sK   y3 t  |  d  } t |  } | j   | r2 t SWn t k
 rF n Xt S(   s?   Quickly see if file is a ZIP file by checking the magic number.t   rb(   t   opent   _EndRecDatat   closet   Truet   IOErrort   False(   t   filenamet   fpint   endrec(    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyR   T   s    
c      
   C   s=  t  j t  } |  j | | d  |  j |  } t  j t |  \ } } } } | t k r` | S| d k sx | d k r t d   n  t  j t  }	 |  j | | |	 d  |  j |	  } t  j t |  \
 } }
 } } } } } } } } | t	 k r | S| | d <| | d <| | d <| | d <| | d <| | d <| S(	   sM   
    Read the ZIP64 end-of-archive records and use that to update endrec
    i   i    i   s3   zipfiles that span multiple disks are not supportedi   i   i   i   (
   t   structt   calcsizet   structEndArchive64Locatort   seekt   readt   unpackt   stringEndArchive64LocatorR    t   structEndArchive64t   stringEndArchive64(   R   t   offsetR   t   locatorSizet   datat   sigt   disknot   relofft   diskst   endArchiveSizet   szt   create_versiont   read_versiont   disk_numt   disk_dirt   dircountt	   dircount2t   dirsizet	   diroffset(    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyt   _EndRecData64`   s*    0





c         C   s  |  j  d d  |  j   d } |  j   } | d d !t k r | d d k r t j t |  } t |  } | j d  | j | d  | d	 d
 k r t	 |  d |  S| St
 | d  } |  j  | | d  |  j   } | j t  } | d k rt j t | | | d ! } t |  } | | d } | d t |  k r| j |  | j | | |  | d	 d
 k rt	 |  | | |  S| Sn  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   is     t    iI    i   i   Ni   (   R   t   tellR   t   stringEndArchiveR   R   t   structEndArchivet   listt   appendR0   t   mint   rfindt   len(   R   t   filesizeR!   R   t	   END_BLOCKt   startt   comment(    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyR      s4    #c           B   s5   e  Z d  Z d Z d d d  Z d   Z d   Z RS(   s>   Class with attributes describing each file in the ZIP archive.t   orig_filenameR   t	   date_timet   compress_typeR=   t   extrat   create_systemR(   t   extract_versiont   reservedt	   flag_bitst   volumet   internal_attrt   external_attrt   header_offsett   CRCt   compress_sizet	   file_sizet	   _raw_timet   NoNamei  i   i    c         C   s  | |  _  | j t d   } | d k r: | d | !} n  t j d k rp t j | k rp | j t j d  } n  | |  _ | |  _ t |  _	 d |  _
 d |  _ t j d k r d |  _ n	 d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d  S(   Ni    t   /R1   t   win32i   i   (   R>   t   findt   chrt   ost   sept   replaceR   R?   R   R@   R=   RA   t   syst   platformRB   R(   RC   RD   RE   RF   RG   RH   (   t   selfR   R?   t	   null_byte(    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyt   __init__   s*    													c   
      C   sw  |  j  } | d d d >| d d >B| d B} | d d >| d	 d >B| d d B} |  j d
 @rs d } } } n |  j } |  j } |  j } |  j } | t k s | t k rd } | t j | d t j	 |  d	 | |  } d } d } t
 d |  j  |  _ t
 d |  j  |  _ n  t j t t |  j |  j |  j |  j | | | | | t |  j  t |   }	 |	 |  j | S(   s'   Return the per-file header as a string.i    i  i	   i   i   i   i   i   i   i   s   <HHQQI    i-   (   R?   RE   RJ   RK   RL   RA   t   ZIP64_LIMITR   t   packR   t   maxRC   R(   t   structFileHeadert   stringFileHeaderRD   R@   R9   R   (
   RX   t   dtt   dosdatet   dostimeRJ   RK   RL   RA   t   fmtt   header(    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyt
   FileHeader   s.    	&&				 c         C   s  |  j  } t j } x|| r| d | d   \ } } | d k r| d k re | d | d d ! } no | d k r | d | d d	 ! } nJ | d
 k r | d | d d ! } n% | d k r d } n t d | f  d } |  j d k r| | |  _ | d 7} n  |  j d k s!|  j d k r;| | |  _ | d 7} n  |  j d k sY|  j d k r|  j } | | |  _ | d 7} qn  | | d } q Wd  S(   Ns   <HHi   i   i   s   <QQQi   i   s   <QQi   i   s   <Qi   i    s   Corrupt extra field %sl    l    i(    (   l    l    (   RA   R   R   t   RuntimeErrorRL   RK   RI   (   RX   RA   R   t   tpt   lnt   countst   idxt   old(    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyt   _decodeExtra	  s4    					(   R>   R   R?   R@   R=   RA   RB   R(   RC   RD   RE   RF   RG   RH   RI   RJ   RK   RL   RM   (   i  i   i   i    i    i    (   R	   R
   R   t	   __slots__RZ   Re   Rl   (    (    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyR      s.                     &	!t   _ZipDecrypterc           B   sD   e  Z d  Z d   Z e   Z d   Z d   Z d   Z d   Z RS(   s  
    Class to handle decryption of files stored within a ZIP archive.

    ZIP supports a password-based form of encryption. Even though known
    plaintext attacks have been found against it, it is still useful
    to be able to get data out of such a file.

    Usage ::
        zd = _ZipDecrypter(mypwd)
        plain_char = zd(cypher_char)
        plain_text = map(zd, cypher_text)
    c          C   s   d }  d g d } xh t  d  D]Z } | } xA t  d  D]3 } | d @r^ | d ?d @|  A} q9 | d ?d @} q9 W| | | <q  W| S(   s   Generate a CRC-32 table.

        ZIP encryption uses the CRC32 one-byte primitive for scrambling some
        internal keys. We noticed that a direct implementation is faster than
        relying on binascii.crc32().
        I     i    i   i   i   i(   t   range(   t   polyt   tablet   it   crct   j(    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyt   _GenerateCRCTable=  s    
c         C   s%   | d ?d @|  j  | t |  Ad @AS(   s(   Compute the CRC32 primitive on one byte.i   i i   (   t   crctablet   ord(   RX   t   chRs   (    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyt   _crc32Q  s    c         C   s=   d |  _  d |  _ d |  _ x | D] } |  j |  q" Wd  S(   NixV4igE#ixV4(   t   key0t   key1t   key2t   _UpdateKeys(   RX   t   pwdt   p(    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyRZ   U  s
    			c         C   sx   |  j  | |  j  |  _ |  j |  j d @d @|  _ |  j d d d @|  _ |  j  t |  j d ?d @ |  j  |  _ d  S(   Ni   I    ii   i   (   Ry   Rz   R{   RR   R|   (   RX   t   c(    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyR}   \  s    c         C   sP   t  |  } |  j d B} | | | d Ad ?d @A} t |  } |  j |  | S(   s   Decrypt a single character.i   i   i   i   (   Rw   R|   RR   R}   (   RX   R   t   k(    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyt   __call__b  s    (	   R	   R
   R   Ru   Rv   Ry   RZ   R}   R   (    (    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyRn   /  s   					t
   ZipExtFilec           B   sk   e  Z d  Z d d  Z d   Z d   Z d   Z d   Z d   Z	 d d  Z
 d d	  Z d d
  Z RS(   sZ   File-like object for reading an archive member.
       Is returned by ZipFile.open().
    c         C   s   | |  _  | |  _ d |  _ d |  _ d |  _ d |  _ t |  _ t |  _ d |  _	 d |  _
 | j |  _ | j |  _ t |  _ d |  _ | j |  _ d	 |  _ |  j t k r t j d  |  _ n  d  S(
   Nl    R1   s   
t   ri@   i   i(   s   
i   (   t   fileobjt	   decryptert
   bytes_readt	   rawbuffert
   readbuffert
   linebufferR   t   eoft   univ_newlinest   nlSepst   lastdiscardR@   RK   t   closedt   modeR   t   namet   compreadsizeR   t   zlibt   decompressobjt   dc(   RX   R   t   zipinfot   decrypt(    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyRZ   p  s$    													c         C   s+   | |  _  d |  _ |  j  r' d |  _ n  d  S(   Ns   
s   
s   (   s   
(   s   
s   s   
(   R   R   (   RX   R   (    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyt   set_univ_newlines  s    			c         C   s   |  S(   N(    (   RX   (    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyt   __iter__  s    c         C   s"   |  j    } | s t    n  | S(   N(   t   readlinet   StopIteration(   RX   t   nextline(    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyt   next  s    c         C   s   t  |  _ d  S(   N(   R   R   (   RX   (    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyR     s    c         C   s   d \ } } |  j  r |  j |  j  d f d k rD |  j  d |  _  n  xH |  j D]: } |  j  j |  } | d k rN t |  } | | f SqN Wn  | | f S(   Nii    s   s   
i   (   ii(   s   s   
(   R   R   R   RQ   R9   (   RX   t   nlt   nllenRT   (    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyt   _checkfornewline  s    	ic         C   s?  | d k  r t  j } n | d k r( d S|  j   \ } } | d k rX t | |  } n | t |  j  8} xo | d k  r | d k r |  j t | d   } | s Pn  |  j | 7_ | t |  8} |  j   \ } } qn W| d k  r |  j } d |  _ | S|  j |  } |  j | | | !|  _ |  j | | |  _ | d S(   sZ   Read a line with approx. size. If size is negative,
           read a whole line.
        i    R1   id   s   
(   RV   t   maxintR   R7   R9   R   R   R   (   RX   t   sizeR   R   t   buft   s(    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyR     s.    		c         C   s:   g  } x- t  r5 |  j   } | s% Pn  | j |  q	 W| S(   st   Return a list with all (following) lines. The sizehint parameter
        is ignored in this implementation.
        (   R   R   R6   (   RX   t   sizehintt   resultt   line(    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyt	   readlines  s    	 c         C   s{  | d k r d S|  j  |  j } |  j d  k	 r< | d 8} n  | d  k	 r | d k r |  j t k r t |  j  } t | | |  } q |  j t	 k r t |  j  | k r d } q t |  j
  } t | |  j |  } q n  | |  j |  j  k r	|  j  |  j } n  | d k r$|  j j |  } |  j t |  7_ |  j
 | 7_
 |  j
 r$|  j
 } d |  _
 | r|  j d  k	 rd j t |  j |   } n  | r|  j t	 k r|  j j |  } |  j j |  _
 |  j rt |  j
  d k r| |  j j   7} d  |  _ qn  |  j | 7_ q$n  | d  k sEt |  j  | k rZ|  j } d |  _ n |  j |  } |  j | |  _ | S(   Ni    R1   i   (   RK   R   R   t   NoneR@   R   R9   R   R7   R   R   R   R   R   t   joint   mapR   t
   decompresst   unconsumed_tailR   t   flush(   RX   R   t   bytesToReadt   lrt   bytest   newdata(    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyR     sL    				!	N(   R	   R
   R   R   RZ   R   R   R   R   R   R   R   R   (    (    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyR   k  s   					,c           B   s   e  Z d  Z d Z d e e d  Z d   Z d   Z	 d   Z
 d   Z d   Z d   Z d	   Z d
   Z d d  Z d d d  Z d d d  Z d d d d  Z d   Z d   Z d d d  Z d   Z d   Z d   Z RS(   sw   Class with methods to open, read, write, close, list zip files.

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

    @var 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.
    @var mode: The mode can be either read "r", write "w" or append "a".
    @var compression: ZIP_STORED (no compression) or ZIP_DEFLATED (requires zlib).
    @var allowZip64: if True ZipFile will create files with ZIP64 extensions when
                needed, otherwise it will raise an exception when this would
                be necessary.

    R   c         C   s'  | d k r t  d   n  | t k r* n* | t k rK t sT t  d  qT n	 t  d  | |  _ t |  _ d |  _ i  |  _ g  |  _	 | |  _
 | j d d	  d |  _ } d |  _ t | t  rOd |  _ | |  _ i d
 d 6d d 6d d 6} y t | | |  |  _ Wqvt k
 rK| d k rEd } } t | | |  |  _ qL  qvXn' d |  _ | |  _ t | d d  |  _ | d k r|  j   n | d k rn | d k ry$ |  j   |  j j |  j d  Wq#t k
 r|  j j d d  q#Xn+ |  j s|  j j   d |  _ n  t  d  d S(   s>   Open the ZIP file with mode read "r", write "w" or append "a".R   t   wt   as(   ZipFile() requires mode "r", "w", or "a"s.   Compression requires the (missing) zlib modules(   That compression method is not supportedi    t   bR1   R   t   wbs   r+bi   R   i   s   Mode must be "r", "w" or "a"N(   R   R   R   (   Rf   R   R   R   t   _allowZip64R   t
   _didModifyt   debugt
   NameToInfot   filelistt   compressionRU   R   R   R~   t
   isinstancet
   basestringt   _filePassedR   R   t   fpR   t   getattrt   _GetContentst   _RealGetContentsR   t	   start_dirR    R   (   RX   t   fileR   R   t
   allowZip64t   keyt   modeDict(    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyRZ   >  sZ    										

		
	c         C   sK   y |  j    Wn6 t k
 rF |  j s@ |  j j   d |  _ n    n Xd S(   sO   Read the directory, making sure we close the file if the format
        is bad.N(   R   R    R   R   R   R   (   RX   (    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyR   x  s    	c         C   s  |  j  } t |  } | s' t d  n  |  j d k r> | GHn  | d } | d } | d |  _ | d t k r | d | d d } n | d | } | | } |  j d	 k r d
 G| G| G| GHn  | | |  _ | j |  j d  | j |  } t	 j
 |  } d } x| | k  r| j d  }	 | d } |	 d d !t k rNt d  n  t j t |	  }	 |  j d	 k rw|	 GHn  | j |	 t  }
 t |
  } | j |	 t  | _ | j |	 t  | _ | |	 t |	 t |	 t } |	 t | _ |	 d d !\ | _ | _ | _ | _ | _ | _ } } | _ | _ | _ |	 d d !\ | _ | _  | _! | | _" | d ?d | d ?d @| d @| d ?| d ?d @| d @d	 f | _# | j$   | j | | _ |  j% j& |  | |  j' | j( <|  j d	 k r
d G| GHq
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	   i8   i   i   s   given, inferred, offseti    i.   i   s&   Bad magic number for central directoryi   i   i   i  i   i   i?   t   totalN()   R   R   R    R   R=   R[   R   R   R   t	   cStringIOt   StringIOt   stringCentralDirR   R   t   structCentralDirt   _CD_FILENAME_LENGTHR   t   _CD_EXTRA_FIELD_LENGTHRA   t   _CD_COMMENT_LENGTHt   _CD_LOCAL_HEADER_OFFSETRI   R(   RB   RC   RD   RE   R@   RJ   RK   RL   RF   RG   RH   RM   R?   Rl   R   R6   R   R   (   RX   R   R   t   size_cdt	   offset_cdt   xt   concatR!   R   t   centdirR   t   tt   d(    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyR     sZ    	



I	&
c         C   s.   g  } x! |  j  D] } | j | j  q W| S(   s+   Return a list of file names in the archive.(   R   R6   R   (   RX   t   lR!   (    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyt   namelist  s    c         C   s   |  j  S(   sJ   Return a list of class ZipInfo instances for files in the
        archive.(   R   (   RX   (    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyt   infolist  s    c         C   sJ   d d	 GHx: |  j  D]/ } d | j d  } d | j | | j 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:%02di   s   %-46s %s %12dN(   s	   File Names   Modified    R   (   R   R?   R   RL   (   RX   t   zinfot   date(    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyt   printdir  s    	c         C   sD   x= |  j  D]2 } y |  j | j  Wq
 t k
 r; | j SXq
 Wd S(   s%   Read all the files and check the CRC.N(   R   R   R   R    (   RX   R   (    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyt   testzip  s
    c         C   s5   |  j  j |  } | d k r1 t d |   n  | S(   s,   Return the instance of ZipInfo given 'name'.s(   There is no item named %r in the archiveN(   R   t   getR   t   KeyError(   RX   R   t   info(    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyt   getinfo  s
    c         C   s   | |  _  d S(   s)   Set default password for encrypted files.N(   R~   (   RX   R~   (    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyt   setpassword  s    c         C   s   |  j  | d |  j   S(   s)   Return file bytes (as a string) for name.R   (   R   R   (   RX   R   R~   (    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyR     s    c         C   s:  | d k r t  d  n  |  j s- t  d  n  |  j rB |  j } n t |  j d  } |  j |  } | j   } | j | j d  | j	 d  } | d d	 !t
 k r t d
  n  t j t |  } | j	 | t  } | t r | j	 | t  n  | | j k rt d | j | f  n  | j d @}	 d }
 |	 r| sF|  j } n  | s\t  d |  n  t |  }
 | j	 d  } t |
 | d d ! } | j d @r| j d ?d @} n | j d ?d @} t | d  | k rt  d |   qn  |
 d k rt | |  } n t | | |
  } d | k r6| j t  n  | S(   s#   Return file-like object for 'name'.R   t   Ut   rUs&   open() requires mode "r", "U", or "rU"s3   Attempt to read ZIP archive that was already closedR   i    i   i   s    Bad magic number for file headers3   File name in directory "%s" and header "%s" differ.i   s6   File %s is encrypted, password required for extractioni   i   i   i   i   s   Bad password for file(   R   R   R   N(   Rf   R   R   R   R   R   R2   R   RI   R   R_   R    R   R   R^   t   _FH_FILENAME_LENGTHt   _FH_EXTRA_FIELD_LENGTHR>   RE   R   R~   Rn   R   RM   RJ   Rw   R   R   R   (   RX   R   R   R~   t   zef_fileR   t   filepost   fheadert   fnamet   is_encryptedt   zdR   t   ht
   check_bytet   zef(    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyR     sX    			

c         C   sO   t  | t  s! |  j |  } n  | d k r< t j   } n  |  j | | |  S(   s#  Extract 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 ZipInfo object. You can
           specify a different directory using `path'.
        N(   R   R   R   R   RS   t   getcwdt   _extract_member(   RX   t   membert   pathR~   (    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyt   extract7  s
    c         C   sC   | d k r |  j   } n  x! | D] } |  j | | |  q" Wd S(   s   Extract all members from the archive to the current working
           directory. `path' specifies a different directory to extract to.
           `members' is optional and must be a subset of the list returned
           by namelist().
        N(   R   R   R   (   RX   R   t   membersR~   R   (    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyt
   extractallE  s    c         C   s  | d d k r | d  } n  t  j j | j  rQ t  j j | | j d  } n t  j j | | j  } t  j j |  } t  j j |  } | r t  j j |  r t  j |  n  |  j	 | j d | } t
 | d  } t j | |  | j   | j   | S(   sb   Extract the ZipInfo object 'member' to a physical
           file on the path targetpath.
        iRO   i   R~   R   (   RS   R   t   isabsR   R   t   normpatht   dirnamet   existst   makedirsR   R   t   shutilt   copyfileobjR   (   RX   R   t
   targetpathR~   t	   upperdirst   sourcet   target(    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyR   Q  s    

c         C   s   | j  |  j k r- |  j r- d G| j  GHq- n  |  j d k rH t d  n  |  j s] t d  n  | j t k r t r t d  n  | j t	 t f k r t d  n  | j
 t k r |  j s t d   q n  | j t k r |  j s t d	   q n  d
 S(   s6   Check for errors before writing a file to the archive.s   Duplicate name:R   R   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 supporteds'   Filesize would require ZIP64 extensionss+   Zipfile size would require ZIP64 extensionsN(   R   R   (   R   R   R   R   Rf   R   R@   R   R   R   RL   R[   R   R   RI   (   RX   R   (    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyt   _writechecko  s(    							c         C   s  |  j  s t d   n  t j |  } t j | j  } | d d !} | d k r[ | } n  t j j	 t j j
 |  d  } x* | d t j t j f k r | d } q Wt | |  } | d d @d >| _ | d k r |  j | _ n	 | | _ | j | _ d | _ |  j  j   | _ |  j |  t |  _ t | d  } d | _ }	 d | _ }
 d | _ } |  j  j | j    | j t k rt j  t j! t j" d  } n d } xx | j# d  } | sPn  | t$ |  } t% | |	  d @}	 | r| j& |  } |
 t$ |  }
 n  |  j  j |  qW| j'   | rq| j(   } |
 t$ |  }
 |  j  j |  |
 | _ n	 | | _ |	 | _ | | _ |  j  j   } |  j  j) | j d d  |  j  j t* j+ d | j | j | j   |  j  j) | d  |  j, j- |  | |  j. | j/ <d S(   sL   Put the bytes from filename into the archive under the name
        arcname.s7   Attempt to write to ZIP archive that was already closedi    i   i   i  l    R   ii   i   I    i   s   <LLLNi    (0   R   Rf   RS   t   statt   timet	   localtimet   st_mtimeR   R   R   t
   splitdriveRT   t   altsepR   RH   R   R@   t   st_sizeRL   RE   R2   RI   R  R   R   R   RJ   RK   t   writeRe   R   R   t   compressobjt   Z_DEFAULT_COMPRESSIONt   DEFLATEDR   R9   t   crc32t   compressR   R   R   R   R\   R   R6   R   R   (   RX   R   t   arcnameR@   t   stt   mtimeR?   R   R   RJ   RK   RL   t   cmprR   t   position(    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyR    sp    		"			
			c         C   s  t  | t  sF t d | d t j t j    d   } |  j | _ n | } |  j sd t d   n  t |  | _	 |  j j
   | _ |  j |  t |  _ t |  d @| _ | j t k rt j t j t j d  } | j |  | j   } t |  | _ n | j	 | _ |  j j
   | _ |  j j | j    |  j j |  |  j j   | j d @r|  j j t j d | j | j | j	   n  |  j j |  | |  j  | j! <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   R?   i   s7   Attempt to write to ZIP archive that was already closedI    ii   s   <lLLN("   R   R   R  R  R   R@   R   Rf   R9   RL   R2   RI   R  R   R   R  RJ   R   R   R  R  R  R  R   RK   R  Re   RE   R   R\   R   R6   R   R   (   RX   t   zinfo_or_arcnameR   R   t   co(    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyt   writestr  s8    		c         C   s   |  j    d S(   s2   Call the "close()" method in case the user forgot.N(   R   (   RX   (    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyt   __del__  s    c         C   sG  |  j  d k r d S|  j d k r!|  j r!d } |  j  j   } x|  j D]} | d } | j } | d d d >| d d >B| d	 B} | d
 d >| d d >B| d d	 B} g  } | j t k s | j	 t k r| j
 | j  | j
 | j	  d } d }	 n | j } | j	 }	 | j t k r<| j
 | j  d }
 n	 | j }
 | j } | rt j d d t |  d d t |  |  | } t d | j  } t d | j  } n | j } | j } yy t j t t | | j | | j | j | j | | | j |	 | t | j  t |  t | j  d | j | j |
  } Wn t k
 rt j  t t | | j | | j | j | j | | | j |	 | t | j  t |  t | j  d | j | j |
 f IJ  n X|  j  j! |  |  j  j! | j  |  j  j! |  |  j  j! | j  qJ W|  j  j   } | t k rt j t" t# d d d d d | | | | |  } |  j  j! |  t j t$ t% d | d  } |  j  j! |  t j t& t' d d | | | | d d 	 } |  j  j! |  n; t j t& t' d d | | | | | d 	 } |  j  j! |  |  j  j(   n  |  j) s:|  j  j*   n  d |  _  d S(   sJ   Close the file, and for mode "w" and "a" write the ending
        records.NR   R   i    i   i  i	   i   i   i   i   i   I    l    s   <HHt   Qi   i-   i,   (   R   R   (+   R   R   R   R   R2   R   R?   RL   R[   RK   R6   RI   RA   R   R\   R9   R]   RC   R(   R   R   RB   RD   RE   R@   RJ   R   R=   RG   RH   t   DeprecationWarningRV   t   stderrR  R   R   R   R   R4   R3   R   R   R   (   RX   t   countt   pos1R   R`   Ra   Rb   RA   RL   RK   RI   t
   extra_dataRC   R(   R   t   pos2t   zip64endrect   zip64locrecR   (    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyR     s    
	&&									!
!	%	N(   R	   R
   R   R   R   R   R   RZ   R   R   R   R   R   R   R   R   R   R   R   R   R   R  R  R  R  R   (    (    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyR   -  s*   :		<								K		E	&	c           B   s#   e  Z d  Z d d  Z d   Z RS(   sD   Class to create ZIP archives with Python library files and packages.R1   c         C   s  t  j j |  \ } } t  j j |  r|t  j j | d  } t  j j |  r| rj d | | f } n | } |  j r d G| Gd G| GHn  |  j | d d !|  \ } } |  j r d G| GHn  |  j | |  t  j	 |  } | j
 d  x| D] }	 t  j j | |	  }
 t  j j |	  \ } } t  j j |
  rpt  j j t  j j |
 d   r|  j |
 |  qq | d k r |  j |
 d d !|  \ } } |  j rd G| GHn  |  j | |  q q Wq|  j rd	 G| GHn  x t  j	 |  D] }	 t  j j | |	  }
 t  j j |	  \ } } | d k r|  j |
 d d !|  \ } } |  j rbd G| GHn  |  j | |  qqWn` | d d k rt d
  n  |  j | d d !|  \ } } |  j rd G| GHn  |  j | |  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(   RS   R   t   splitt   isdirR   t   isfileR   t   _get_codenameR  t   listdirt   removet   splitextt   writepyRf   (   RX   t   pathnamet   basenamet   dirR   t   initnameR   R  t   dirlistR   R   t   roott   ext(    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyR.  X  sZ    		!					c   
      C   s8  | d } | d } | d } t  j j |  r] t  j |  j t  j |  j k r] | } n t  j j |  s t  j |  j t  j |  j k  r d d l } |  j r d G| GHn  y | j | | d t	  Wn | j
 k
 r } | j GHn X| } n | } t  j j |  d }	 | r.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   .pyoiNt	   Compilingi   s   %s/%s(   RS   R   R)  R  R	  t
   py_compileR   t   compileR   R   t   PyCompileErrort   msgR'  (
   RX   R/  R0  t   file_pyt   file_pyct   file_pyoR   R7  t   errt   archivename(    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyR*    s*    


$	$		(   R	   R
   R   R.  R*  (    (    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyR   U  s   Ac   
         s  d d  l  } | j d  } |  d  k r7 t j d }  n  |  sN |  d d k rc | GHt j d  n  |  d d k r t |   d	 k r | GHt j d  n  t |  d d
  } | j   | j	   n|  d d k r t |   d	 k r | GHt j d  n  t |  d d
  } | j
   d GHn|  d d k r;t |   d k rW| GHt j d  n  t |  d d
  } |  d	 } x | j   D] } | j d  rt j j | | d	  } n t j j | |  } t j j |  } t j j |  st j |  n  t | d  } | j | j |   | j	   qW| j	   n |  d d k rt |   d k  rr| GHt j d  n    f d     t |  d d d t } x. |  d	 D]" }	   | |	 t j j |	   qW| j	   n  d  S(   NisE          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
        i   i    s   -ls   -cs   -es   -ti   R   s   Done testingi   s   ./R   c            s   t  j j |  r( |  j | | t  n] t  j j |  r xH t  j |  D]4 }   |  t  j j | |  t  j j | |   qJ Wn  d  S(   N(   RS   R   R)  R  R   R(  R+  R   (   t   zfR   t   zippatht   nm(   t   addToZip(    s/   lib/python2.7/site-packages/obitools/zipfile.pyRC    s    R   R   (   s   -ls   -cs   -es   -t(   t   textwrapt   dedentR   RV   t   argvt   exitR9   R   R   R   R   R   t
   startswithRS   R   R   R   R   R   R   R  R   R   R0  (
   t   argsRD  t   USAGER@  t   outR   t   tgtt   tgtdirR   t   src(    (   RC  s/   lib/python2.7/site-packages/obitools/zipfile.pyt   main  s`    	


	 t   __main__(H   R   R   RS   R  RV   R   t   binasciiR   R   R  t   ImportErrorR   t   __all__t	   ExceptionR    R   R   R[   R   R   R4   R3   R   R   R^   R_   R   R   R   R   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_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   R   R0   R   t   objectR   Rn   R   R   R   RO  R	   (    (    (    s/   lib/python2.7/site-packages/obitools/zipfile.pyt   <module>   s   <		"	'<  *cI