ó
©¶"Pc           @   s   d  d l  Z  d  d l Z d  d l Z y d  d l Z Wn e k
 rS e  j   Z n Xd d d     YZ d e  j f d     YZ d S(   i’’’’Nt   CTOCc           B   sS   e  Z d  Z d Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 RS(	   sr   A class encapsulating the table of contents of a CArchive.

       When written to disk, it is easily read from C.s   !iiiibcc         C   s   g  |  _  d  S(   N(   t   data(   t   self(    (    sL   /home/salopaasi/Documents/Moo/pyinstaller-2.0/PyInstaller/loader/carchive.pyt   __init__    s    c         C   s  t  j |  j  } d } xń | t |  k  rt  j |  j | | | | ! \ } } } } } }	 | | }
 | | } t  j |
 d | | | |
 ! \ } | |
 } | j d  } | d k  rā |  j j | | | | |	 | f  q |  j j | | | | |	 | |  f  q Wd S(   sS   Decode the binary string into an in memory list.

            S is a binary string.i    t   st    N(   t   structt   calcsizet   ENTRYSTRUCTt   lent   unpackt   findR   t   append(   R   R   t   entrylent   pt   slent   dpost   dlent   ulent   flagt   typcdt   nmlent   nmt   pos(    (    sL   /home/salopaasi/Documents/Moo/pyinstaller-2.0/PyInstaller/loader/carchive.pyt
   frombinary#   s    &

%
%c         C   sß   t  j |  j  } g  } x· |  j D]¬ \ } } } } } } t |  d }	 |	 | }
 |
 d d k rm d } n" d |
 d } d | } |	 | }	 | j t  j |  j |	 d |	 | | | | | | | |   q" Wd j |  S(   s   Return self as a binary string.i   i   i    R   R   t    (   R   R   R   R   R	   R   t   packt   join(   R   R   t   rsltR   R   R   R   R   R   R   t   toclent   padt   padlen(    (    sL   /home/salopaasi/Documents/Moo/pyinstaller-2.0/PyInstaller/loader/carchive.pyt   tobinary<   s    "
	

(c         C   s&   |  j  j | | | | | | f  d S(   s+  Add an entry to the table of contents.

           DPOS is data position.
           DLEN is data length.
           ULEN is the uncompressed data len.
           FLAG says if the data is compressed.
           TYPCD is the "type" of the entry (used by the C code)
           NM is the entry's name.N(   R   R   (   R   R   R   R   R   R   R   (    (    sL   /home/salopaasi/Documents/Moo/pyinstaller-2.0/PyInstaller/loader/carchive.pyt   addT   s    	c         C   s   |  j  | S(   s)   return the toc entry (tuple) at index NDX(   R   (   R   t   ndx(    (    sL   /home/salopaasi/Documents/Moo/pyinstaller-2.0/PyInstaller/loader/carchive.pyt   get_   s    c         C   s   |  j  | S(   N(   R   (   R   R"   (    (    sL   /home/salopaasi/Documents/Moo/pyinstaller-2.0/PyInstaller/loader/carchive.pyt   __getitem__c   s    c         C   s8   x1 t  |  j  D]  \ } } | d | k r | Sq Wd S(   sT   Return the index of the toc entry with name NAME.

           Return -1 for failure.i’’’’(   t	   enumerateR   (   R   t   namet   iR   (    (    sL   /home/salopaasi/Documents/Moo/pyinstaller-2.0/PyInstaller/loader/carchive.pyR   f   s    (   t   __name__t
   __module__t   __doc__R   R   R   R    R!   R#   R$   R   (    (    (    sL   /home/salopaasi/Documents/Moo/pyinstaller-2.0/PyInstaller/loader/carchive.pyR       s   						t   CArchivec           B   s   e  Z d  Z d Z d Z e Z d Z d Z d Z	 d d d d  Z d   Z d   Z d	   Z d
   Z d   Z d   Z d   Z d   Z RS(   s^   An Archive subclass that an hold arbitrary data.

       Easily handled from C or from Python.s   MEI
i    s   !8siiiii   i	   c         C   s#   | |  _  t j j |  | |  d S(   sŚ   Constructor.

           PATH is path name of file (create an empty CArchive if path is None).
           START is the seekposition within PATH.
           LEN is the length of the CArchive (if 0, then read till EOF). N(   R	   t   archivet   ArchiveR   (   R   t   patht   startR	   (    (    sL   /home/salopaasi/Documents/Moo/pyinstaller-2.0/PyInstaller/loader/carchive.pyR   y   s    	c         C   sZ  |  j  r) |  j j |  j |  j  d  n |  j j d d  |  j j   } |  j  r{ |  j j |  j |  j  |  j d  n |  j j |  j d  t j |  j |  j j	 |  j   \ } } } } } | |  j
 k rö t d |  j |  j j f   n  | | |  _ |  j  rC| |  j  k s-|  j |  j k rCt d |  j  qCn  | | |  _ |  _ d S(   s\   Verify that self is a valid CArchive.

            Magic signature is at end of the archive.i    i   s!   %s is not a valid %s archive files#   Problem with embedded archive in %sN(   R	   t   libt   seekR/   t   tellt   TRLLENR   R
   t	   TRLSTRUCTt   readt   MAGICt   RuntimeErrorR.   t	   __class__R(   t   pkgstartt   tocposR   (   R   t   filelent   magict   totallenR:   R   t   pyvers(    (    sL   /home/salopaasi/Documents/Moo/pyinstaller-2.0/PyInstaller/loader/carchive.pyt
   checkmagic   s"    	 	''	!c         C   sR   |  j    |  _ |  j j |  j |  j  |  j j |  j  } |  j j |  d S(   s'   Load the table of contents into memory.N(	   t   TOCTMPLTt   tocR0   R1   R9   R:   R5   R   R   (   R   t   tocstr(    (    sL   /home/salopaasi/Documents/Moo/pyinstaller-2.0/PyInstaller/loader/carchive.pyt   loadtoc   s    c         C   s+  t  |  t  d  k r= |  j j |  } | d k rC d Sn | } |  j j |  \ } } } } } } |  j j |  j |  |  j j |  }	 | d k rį d d l	 a	 |	 d  }
 t	 j
 |
 t	 j d t	 j  j |	 d  }	 n  | d k sł | d k rt j |	  }	 n  | d k r!d |	 f Sd	 |	 f S(
   sŚ   Get the contents of an entry.

           NAME is an entry name.
           Return the tuple (ispkg, contents).
           For non-Python resoures, ispkg is meaningless (and 0).
           Used by the import mechanism.R   i’’’’i   Ni    R   i   t   Mi    (   t   typeRA   R   t   NoneR#   R0   R1   R9   R5   t   AESt   newt   MODE_CFBt
   block_sizet   decryptt   zlibt
   decompress(   R   R&   R"   R   R   R   R   R   R   R   t   key(    (    sL   /home/salopaasi/Documents/Moo/pyinstaller-2.0/PyInstaller/loader/carchive.pyt   extract¢   s"    $
/
c         C   s=   g  } x0 |  j  D]% \ } } } } } } | j |  q W| S(   s   Return the names of the entries(   RA   R   (   R   R   R   R   R   R   R   R   (    (    sL   /home/salopaasi/Documents/Moo/pyinstaller-2.0/PyInstaller/loader/carchive.pyt   contents¾   s    "c         C   sÕ  | d  \ } } } } yb | d k r4 d } d } nC | d k rb t  | d  j   } | d } n t  | d	  j   } Wn) t k
 r£ d
 | | | | f GH  n Xt |  } | t d  k sČ t  | d k są | d k rų t j | |  j  } n  | d k rZd d l	 a	 d d l
 } | j d  }	 |	 t	 j |	 t	 j d t	 j  j |  } n  t |  }
 |  j j   } | d k r¢| j d  d k r¢d } q¢n  |  j j | |
 | | | |  |  j j |  d S(   sx  Add an ENTRY to the CArchive.

           ENTRY must have:
             entry[0] is name (under which it will be saved).
             entry[1] is fullpathname of the file.
             entry[2] is a flag for it's storage format (0==uncompressed,
             1==compressed)
             entry[3] is the entry's type code.
             Version 5:
               If the type code is 'o':
                 entry[0] is the runtime option
                 eg: v  (meaning verbose imports)
                     u  (menaing unbuffered)
                     W arg (warning option arg)
                     s  (meaning do site.py processing.i   t   ot   dR   i    R   t   rUs   
 t   rbs"   Cannot find ('%s', '%s', %s, '%s')i   i   i   i’’’’Ni    R   t   ms   .__init__.pyRD   (   RQ   RR   (   t   openR5   t   IOErrorR	   t   ranget   AssertionErrorRL   t   compresst   LEVELRG   t   Cryptt   gen_random_keyRH   RI   RJ   t   encryptR0   R2   R   RA   R!   t   write(   R   t   entryR   t   pathnmR   R   R   R   R\   RN   R   t   where(    (    sL   /home/salopaasi/Documents/Moo/pyinstaller-2.0/PyInstaller/loader/carchive.pyR!   Å   s6    	/c         C   s;   | |  _  |  j j   } t |  |  _ |  j j |  d S(   s#   Save the table of contents to disk.N(   R:   RA   R    R	   R   R0   R_   (   R   R:   RB   (    (    sL   /home/salopaasi/Documents/Moo/pyinstaller-2.0/PyInstaller/loader/carchive.pyt   save_toc÷   s    	c         C   sk   | |  j  |  j } t j d d t j d } t j |  j |  j | | |  j  |  } |  j j	 |  d S(   s~   Save the trailer to disk.

           CArchives can be opened from the end - the trailer points
           back to the start. i    i
   i   N(
   R   R3   t   syst   version_infoR   R   R4   R6   R0   R_   (   R   R:   R=   R>   t   trl(    (    sL   /home/salopaasi/Documents/Moo/pyinstaller-2.0/PyInstaller/loader/carchive.pyt   save_trailerž   s
    c   	      C   s   |  j  j |  } | d k r7 t d | |  j f  n  |  j  j |  \ } } } } } } | rm t d  n  t |  j |  j | |  S(   s;   Open a CArchive of name NAME embedded within this CArchive.i’’’’s   Member '%s' not found in %ss*   Cannot open compressed archive %s in place(   RA   R   t   KeyErrorR.   R#   t
   ValueErrorR+   R9   (	   R   R&   R"   R   R   R   R   R   R   (    (    sL   /home/salopaasi/Documents/Moo/pyinstaller-2.0/PyInstaller/loader/carchive.pyt   openEmbedded	  s    $N(   R(   R)   R*   R6   t   HDRLENR    R@   R4   R3   R[   RF   R   R?   RC   RO   RP   R!   Rc   Rg   Rj   (    (    (    sL   /home/salopaasi/Documents/Moo/pyinstaller-2.0/PyInstaller/loader/carchive.pyR+   o   s    						2		(    (	   R,   R   Rd   RL   t   ImportErrort	   DummyZlibR    R-   R+   (    (    (    sL   /home/salopaasi/Documents/Moo/pyinstaller-2.0/PyInstaller/loader/carchive.pyt   <module>   s   T