
 X9                 @   s   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 m Z m Z m	 Z	 d  d l
 m Z d  d l m Z d  d l m Z Gd d   d e  Z d S)    N)display_path
backup_dirrmtree)logger)InstallationError)Commandc                   s   e  Z d  Z d Z d Z d Z d Z   f d d   Z d d   Z d	 d
   Z	 d d   Z
 d d   Z d d   Z d d   Z d d   Z d d   Z d d   Z d d   Z   S)
ZipCommandzZip individual packages.zipz#
     %prog [options] <package> ...z$DEPRECATED. Zip individual packages.c          
      s9  t  t |   j | |   |  j d k rM |  j j d d d d d d d n( |  j j d	 d d
 d d d d d d |  j j d d d d d d d |  j j d d d d d d d d |  j j d d d d d d d |  j j d d d d d d d |  j j d d d d d d |  j j d |  j  d  S) Nr	   z--unzipaction
store_truedestunziphelpz"Unzip (rather than zip) a package.z--zipstore_falsedefaultTz"Zip (rather than unzip) a package.z--no-pycno_pyczEDo not include .pyc files in zip files (useful on Google App Engine).z-lz--listlistz2List the packages available, and their zip status.z--sort-files
sort_fileszDWith --list, sort packages according to how many files they contain.z--pathappendpathsz?Restrict operations to the given paths (may include wildcards).z-nz
--simulatez0Do not actually perform the zip/unzip operation.r   )superr   __init__namecmd_opts
add_optionparserinsert_option_group)selfargskw)	__class__ ;/home/ubuntu/projects/ifolica/build/pip/pip/commands/zip.pyr      sL    	
								zZipCommand.__init__c             C   sp  |  j  s t j Sg  } t   } x t j D] } t j j t j j |   } x |  j  D] } t j j t j j |   } d | k r t j t	 j
 | d  |  r | j |  | j |  Pq qW | j |  rW | j |  | j |  PqW qW Wt j d | d j |  j   f  q) WxJ |  j  D]? } | | k r)d | k r)| j |  t j d |  q)q)W| S)z:All the entries of sys.path, possibly restricted by --path*z,Skipping path %s because it doesn't match %sz, zDAdding path %s because it doesn't match anything already on sys.path)select_pathssyspathsetosnormcaseabspathresearchfnmatch	translater   add
startswithr   debugjoin)r   result	match_anyr&   matchr!   r!   r"   r   >   s2    				zZipCommand.pathsc             C   sV  t  j d d  | j |  _ | j |  _ | j rA |  j | |  S| sV t d   n  g  } x | D] } |  j |  \ } } | j r t	 j
 j |  r t d | | f   n6 | j r t	 j
 j |  r t d | | f   n  | j | | f  qc Wd  } xJ | D]B \ } } | j r6|  j | |  } q|  j | | | j  } qW| S)Nz1.7z_DEPRECATION: 'pip zip' and 'pip unzip` are deprecated, and will be removed in a future release.z2You must give at least one package to zip or unzipz;The module %s (in %s) is not a zip file; cannot be unzippedz:The module %s (in %s) is not a directory; cannot be zipped)r   
deprecatedr   r$   simulater   r   find_packager   r(   r&   isdirr   unzip_packagezip_packager   )r   optionsr   packagesargmodule_namefilenamelast_statusr!   r!   r"   run\   s4    		zZipCommand.runc             C   s  t  j j |  } t  j j |  rP t j |  rP t d | | | f   n  t  j j |  } | |  j   k r t j	 d t
 |  t
 |  t
 |  f  n  t j d | t
 |  f  |  j r t j d  d  St j d 7_ zt j |  } g  } x| j   D]} | j } | j | t  j j  r| j |  }	 t  j j | |  }
 t  j j t  j j |
   st  j t  j j |
   n  |	 r|
 j t  j j  rt  j j |
  st  j |
  qqt |
 d  } | j |	  | j   q| j | | j |  f  qW| j   | sbt j d t
 |   t  j |  |  j |  n` t j d | t
 |  f  t j | d	  } x$ | D] \ } }	 | j | |	  qW| j   Wd  t j d 8_ Xd  S)
Nz3Module %s (in %s) isn't located in a zip file in %sz/Unpacking %s into %s, but %s is not on sys.pathzUnzipping %s (in %s)z3Skipping remaining operations because of --simulate   wbzRemoving now-empty zip file %sz(Removing entries in %s/ from zip file %sw) r(   r&   dirnameisfilezipfile
is_zipfiler   r   r   warnr   notifyr7   indentZipFileinfolistr@   r0   sepreadr2   existsmakedirsendswithopenwritecloser   infounlinkremove_filename_from_pthwritestr)r   r?   r@   zip_filenamepackage_pathr	   to_saverW   r   contentr   fr!   r!   r"   r:   {   sX    "		 
zZipCommand.unzip_packagec             C   sN  | } t  j d | t |  f  t  j d 7_ | j d  rJ | } n
 | d } z| | k r t |  } t  j d | | f  |  j s t j | |  q n  yvt  j	 d t |   |  j st
 j | d  } | j | d d	  x t j |  D] \ } } }	 | r)d
 d   |	 D }	 n  x | d f |	 d f g D] \ }
 } x |
 D] } t j j | |  } t j j | | t |  d   j t j j  |  } | r| j | d d	  qU| j | |  qUWqBWq W| j   n  t  j	 d t |   |  j st |  n  Wn   Yn X|  j |  Wd  t  j d 8_ Xd  S)NzZip %s (in %s)rC   z.eggz.zipzMoving %s aside to %szCreating zip file in %srE   / c             S   s+   g  |  ]! } | j    j d   s |  q S)z.pyc)lowerrS   ).0r_   r!   r!   r"   
<listcomp>   s   	 z*ZipCommand.zip_package.<locals>.<listcomp>TFzRemoving old directory %s)r   rK   r   rL   rS   r   r7   shutilmoverW   rH   rM   rZ   r(   walkr&   r2   lenlstriprO   rU   rV   r   add_filename_to_pth)r   r?   r@   r   orig_filenamedest_filenamer	   dirpathdirnames	filenamesfnsis_dirfnfullr   r!   r!   r"   r;      sF    	
		%7	zZipCommand.zip_packagec                s  x |  j    D] } t | d  } | j   } | j     f d d   | D } | | k r t j d t    t |  f  d d   | D s t j d t |   |  j s t j	 |  q n2 |  j s t | d  } | j
 |  | j   n  d  Sq Wt j d t     d  S)	Nrc                s(   g  |  ] } | j      k r |  q Sr!   )strip)rc   l)r@   r!   r"   rd      s   	 z7ZipCommand.remove_filename_from_pth.<locals>.<listcomp>z*Removing reference to %s from .pth file %sc             S   s   g  |  ] } | r |  q Sr!   r!   )rc   liner!   r!   r"   rd      s   	 z %s file would be empty: deletingrD   z.Cannot find a reference to %s in any .pth file)	pth_filesrT   	readlinesrV   r   rW   r   r7   r(   rX   
writelinesrJ   )r   r@   pthr_   lines	new_linesr!   )r@   r"   rY      s$    
			z#ZipCommand.remove_filename_from_pthc             C   s   t  j j |  } | d } | |  j   k rH t j d t |   n  |  j s t  j j |  r t	 |  } | j
   } | j   | r | d j d  r | d d 7<n  | j | d  n | d g } t	 | d  } | j |  | j   n  d  S)Nz.pthz.Adding .pth file %s, but it is not on sys.path   
rD   r   )r(   r&   rF   r   r   rJ   r   r7   rQ   rT   ry   rV   rS   r   rz   )r   r@   r&   r   r_   r|   r!   r!   r"   rj      s     
	
zZipCommand.add_filename_to_pthc             c   s   x |  j    D]r } t j j |  s t j j |  r? q n  x= t j |  D], } | j d  rO t j j | |  VqO qO Wq Wd  S)Nz.pth)r   r(   r&   rQ   r9   listdirrS   r2   )r   r&   r@   r!   r!   r"   rx     s    &zZipCommand.pth_filesc             C   s   x |  j    D] } t j j | |  } t j j |  rD | | f St j j |  r t j |  r t j | d  } y  | j	 t j j | d   Wn t
 k
 r Yn X| j   | | f S| j   q q Wt d |   d  S)Nrt   z__init__.pyz!No package with the name %s found)r   r(   r&   r2   rQ   r9   rH   rI   rM   rP   KeyErrorrV   r   )r   packager&   rs   r	   r!   r!   r"   r8     s    
" 

zZipCommand.find_packagec             C   sD  | r t  d   n  x(t |  j    D]} t j j |  sF q( n  t j j | j t j j   } t j j	 |  r t
 j |  r t j j |  |  j   k r( t j d t |   q( q( n  | d k r | d k r | j d d  j d  r q( n  t j d t |   t j d	 7_ g  } g  } zx t t j |   D] } t j j |  d
 j   } | d k rqKn  | d k rt j d t |   qKn  t j j | |  }	 t j j |	  r| j | |  j |	  f  qKt
 j |	  r| j |  qKt j d t |   qKW| rt j d  t j d	 7_ z" x | D] } t j |  qTWWd  t j d	 8_ Xn t j d  | r| j r| j d d d    n  t j d  t j d	 7_ z2 x+ | D]# \ } }
 t j d | |
 f  qWWd  t j d	 8_ Xn t j d  Wd  t j d	 8_ Xq( Wd  S)Nz'You cannot give an argument with --listzZipped egg: %szsite-packageszdist-packages\r`   z
lib/pythonzIn %s:rC   r~   .pth	.egg-info	.egg-linkz.pyz Not displaying %s: not a packagezUnknown file: %szZipped packages:zNo zipped packages.keyc             S   s	   |  d S)Nr~   r!   )xr!   r!   r"   <lambda>L  s    z!ZipCommand.list.<locals>.<lambda>zUnzipped packages:z%s  (%i files)zNo unzipped packages.)r   r   r   )r   sortedr   r(   r&   rQ   basenamerstriprO   rG   rH   rI   rF   r   rK   r   replacerS   rL   r   splitextrb   rW   r2   r9   r   count_packager   sort)r   r<   r   r&   r   zippedunzippedr@   extrs   countr!   r!   r"   r     sf    !!	zZipCommand.listc             C   sP   d } xC t  j |  D]2 \ } } } d d   | D } | t |  7} q W| S)Nr   c             S   s+   g  |  ]! } | j    j d   s |  q S)z.pyc)rb   rS   )rc   r_   r!   r!   r"   rd   \  s   	 z,ZipCommand.count_package.<locals>.<listcomp>)r(   rg   rh   )r   r&   totalrm   rn   ro   r!   r!   r"   r   Y  s
    zZipCommand.count_package)__name__
__module____qualname____doc__r   usagesummaryr   r   rB   r:   r;   rY   rj   rx   r8   r   r   r!   r!   )r    r"   r      s   *3,;r   )r%   r+   r-   r(   re   rH   pip.utilr   r   r   pip.logr   pip.exceptionsr   pip.basecommandr   r   r!   r!   r!   r"   <module>   s   