
S]c           @@ s  d  Z  d d l m Z m 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
 d d l Z d d l Z d d l Z d d l Z d d l m Z d d l m Z d d l m Z d d l m Z d d l m Z d d	 l m Z d
 e f d     YZ d   Z d e f d     YZ 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  e f d!     YZ0 d d"  Z2 d S(#   s!   Bundle up a project for shipment.i    (   t   absolute_importt   print_functionN(   t   _new_error_recorder(   t   logged_subprocess(   t   SimpleStatus(   t"   subdirectory_relative_to_directory(   t   rename_over_existing(   t   makedirs_ok_if_existst	   _FileInfoc           B@ s   e  Z d    Z RS(   c         C@ s   t  j j |  |  _ t  j j |  j d | |  _ t j   d k r` |  j j d d  |  _	 n |  j |  _	 t  j j
 |  j  |  _
 | |  _ d  S(   Nt   startt   Windowss   \t   /(   t   ost   patht   abspatht	   full_patht   relpatht   relative_patht   platformt   systemt   replacet   unixified_relative_patht   basenamet   is_directory(   t   selft   project_directoryt   filenameR   (    (    s8   lib/python2.7/site-packages/anaconda_project/archiver.pyt   __init__    s    (   t   __name__t
   __module__R   (    (    (    s8   lib/python2.7/site-packages/anaconda_project/archiver.pyR      s   c      	   C@ s4  y g  } x t  j |   D] \ } } } g  } xd | D]\ } t d |  d t  j j | |  d t  }	 | |	  rw q5 q5 | j |  | j |	  q5 W| | (xT | D]L }
 t d |  d t  j j | |
  d t  }	 | |	  s | j |	  q q Wq W| SWn1 t k
 r/} | j	 d |  t
 |  f  d  SXd  S(   NR   R   R   s   Could not list files in %s: %s.(   R   t   walkR   R   t   joint   Truet   appendt   Falset   OSErrort   errort   strt   None(   R   t   ignore_filtert   frontendt
   file_infost   roott   dirst   filest   filtered_dirst   dt   infot   ft   e(    (    s8   lib/python2.7/site-packages/anaconda_project/archiver.pyt   _list_project+   s(    *$t   _FilePatternc           B@ s   e  Z d    Z d   Z RS(   c         C@ s   | d k s t   | |  _ d  S(   Nt    (   t   AssertionErrort   pattern(   R   R6   (    (    s8   lib/python2.7/site-packages/anaconda_project/archiver.pyR   I   s    c         C@ s   d   } |  j  j d  r' |  j  } n d |  j  } d | j } | j d  rq | j rj | | | d   St Sn | | |  Sd  S(   Nc         S@ sT   |  j  d  s t  x8 |  d k rO t j |  |  r: t St j j |   }  q Wt S(   NR   (   t
   startswithR5   t   fnmatchR    R   R   t   dirnameR"   (   R   R6   (    (    s8   lib/python2.7/site-packages/anaconda_project/archiver.pyt   matchS   s    R   s   */i(   R6   R7   R   t   endswithR   R"   (   R   R/   R:   R6   t   match_against(    (    s8   lib/python2.7/site-packages/anaconda_project/archiver.pyt   matchesN   s    	
	(   R   R   R   R=   (    (    (    s8   lib/python2.7/site-packages/anaconda_project/archiver.pyR3   H   s   	c      	   C@ s   g  } y t  j |  d d  z } xp | D]h } | j   } | j d  rO q( n  | j d  rk | d } n  | d k r( | j t d |   q( q( WWd  QX| SWnP t t f k
 r } | j t j	 k r | S| j
 d |  t |  f  d  Sn Xd  S(	   Nt   rs   utf-8t   #s   \#i   R4   R6   s   Failed to read %s: %s(   t   codecst   opent   stripR7   R!   R3   R#   t   IOErrort   errnot   ENOENTR$   R%   R&   (   R   R(   t   patternsR0   t   lineR1   (    (    s8   lib/python2.7/site-packages/anaconda_project/archiver.pyt   _parse_ignore_fileq   s"    #c         C@ s"   t  j j |  d  } t | |  S(   Ns   .projectignore(   R   R   R   RH   (   R   R(   t   ignore_file(    (    s8   lib/python2.7/site-packages/anaconda_project/archiver.pyt   _load_ignore_file   s    c         C@ s   t  j j t  j j |  d   s% g  SyE t j d d d d d d g d |  } d g | j d	  j   SWnp t j	 k
 r } | j
 j d	  j d
 d  } | j d |  d  St k
 r } | j d t |   d  SXd  S(   Ns   .gitt   gits   ls-filess   --otherss	   --ignoreds   --exclude-standards   --directoryt   cwds   utf-8s   
t    s0   'git ls-files' failed to list ignored files: %s.s    Failed to run 'git ls-files'; %s(   R   R   t   existsR   R   t   check_outputt   decodet
   splitlinest
   subprocesst   CalledProcessErrort   outputR   R$   R&   R#   R%   (   R   R(   RT   R1   t   message(    (    s8   lib/python2.7/site-packages/anaconda_project/archiver.pyt   _git_ignored_files   s    !!c         @ s>   t  |  |      d  k r d  St        f d   } | S(   Nc         @ sd   |  j  } xT | d k r_ | d k s* t  |   k sF | d   k rJ t St j j |  } q Wt S(   NR4   R   (   R   R5   R    R   R   R9   R"   (   R/   R   (   t   git_ignored(    s8   lib/python2.7/site-packages/anaconda_project/archiver.pyt   is_git_ignored   s    	(   RV   R&   t   set(   R   R(   RX   (    (   RW   s8   lib/python2.7/site-packages/anaconda_project/archiver.pyt   _git_filter   s    
c         @ s2   t  |  |      d  k r d  S  f d   } | S(   Nc         @ s(   x!   D] } | j  |   r t Sq Wt S(   N(   R=   R    R"   (   R/   R6   (   RF   (    s8   lib/python2.7/site-packages/anaconda_project/archiver.pyt   matches_some_pattern   s    (   RJ   R&   (   R   R(   R[   (    (   RF   s8   lib/python2.7/site-packages/anaconda_project/archiver.pyt   _ignore_file_filter   s
    c         @ s   t  |  |    t |  |     d  k s6  d  k r: d  St    x  | D] }  j | j   qJ Wg   D] } t |  ^ qm   f d        f d   } t |  | |  } | d  k r d  S| S(   Nc         @ s(   x!   D] } | j  |   r t Sq Wt S(   N(   R=   R    R"   (   R/   R6   (   t   plugin_patterns(    s8   lib/python2.7/site-packages/anaconda_project/archiver.pyt   is_plugin_generated   s    c         @ s"     |   p!  |   p!  |   S(   N(    (   R/   (   t
   git_filtert   ignore_file_filterR^   (    s8   lib/python2.7/site-packages/anaconda_project/archiver.pyt   all_filters   s    (   RZ   R\   R&   RY   t   uniont   ignore_patternsR3   R2   (   R   R(   t   requirementst   reqt   sRa   t   infos(    (   R_   R`   R^   R]   s8   lib/python2.7/site-packages/anaconda_project/archiver.pyt   _enumerate_archive_files   s    	c         C@ s   t    } x |  D] } | | | j <q Wxp |  D]h } t j j | j  } xJ | d k r | d k sj t  | | k r | | =n  t j j |  } qL Wq. Wt | j   d d   S(   NR4   R   t   keyc         S@ s   |  j  S(   N(   R   (   t   x(    (    s8   lib/python2.7/site-packages/anaconda_project/archiver.pyt   <lambda>   R4   (   t   dictR   R   R   R9   R5   t   sortedt   values(   Rg   t   all_by_nameR/   t   parent(    (    s8   lib/python2.7/site-packages/anaconda_project/archiver.pyt   _leaf_infos   s    	
c      	   C@ s   | d  k r d } n
 d | } t j | d |  ] } xS t |  D]E } t j j |  | j  } | j d |  | j	 | j
 d | qE WWd  QXd  S(   NR4   t   :s   w%ss
     added %st   arcname(   R&   t   tarfileRA   Rq   R   R   R   R   R/   t   addR   (   t   archive_root_nameRg   R   t   compressionR(   t   tfR/   Rs   (    (    s8   lib/python2.7/site-packages/anaconda_project/archiver.pyt
   _write_tar   s    	
c      	   C@ su   t  j | d  ] } xS t |  D]E } t j j |  | j  } | j d |  | j | j	 d | q" WWd  QXd  S(   Nt   ws
     added %sRs   (
   t   zipfilet   ZipFileRq   R   R   R   R   R/   t   writeR   (   Rv   Rg   R   R(   t   zfR/   Rs   (    (    s8   lib/python2.7/site-packages/anaconda_project/archiver.pyt
   _write_zip	  s
    c         C@ s?   t  |  | d | } | d  k r% d  Sg  | D] } | j ^ q, S(   NRd   (   Rh   R&   R   (   R   R(   Rd   Rg   R/   (    (    s8   lib/python2.7/site-packages/anaconda_project/archiver.pyt0   _list_relative_paths_for_unignored_project_files  s    c      	   C@ s  |  j    } | d k	 r@ x! | j D] } |  j j |  q" W| St |  j  } t j j |  j	 j
  s | j d |  j	 j  t d t d d d | j    S|  j	 j r | j d |  j	 j  t d t d d d | j    St |  j | d |  j } | d k r%t d t d d d | j    St | |  j  } t j j |  stg  | D] } | j | k rP| ^ qP} n  | d	 t t j    } zay| j   j d
  rt |  j | | |  n | j   j d  rt |  j | | d d d | n | j   j d  r0t |  j | | d d d | ng | j   j d  rgt |  j | | d d d | n0 | j d |  t d t d d d | j    St | |  WnF t k
 r}	 | j t |	   t d t d d | d | j    SXWd y t j  |  Wn t t! f k
 rn XXg  }
 x6 |  j" j#   D]% } | j$ j% r4|
 j& | j  q4q4Wt' |
  d k r| j( d  | j( d  t' |
  t' |  j"  k r| j( d d j) t* |
    qn  t d t+ d d |  S(   s   Make an archive of the non-ignored files in the project.

    Args:
        project (``Project``): the project
        filename (str): name for the new zip or tar.gz archive file

    Returns:
        a ``Status``, if failed has ``errors``
    s   %s does not exist.t   successt   descriptions   Can't create an archive.t   errorss#   %s has been modified but not saved.Rd   s$   Failed to list files in the project.s   .tmp-s   .zips   .tar.gzRw   t   gzR(   s   .tar.bz2t   bz2s   .tars    Unsupported archive filename %s.s>   Project archive filename must be a .zip, .tar.gz, or .tar.bz2.s#   Failed to write project archive %s.Ni    sj   Warning: env specs are not locked, which means they may not work consistently for others or when deployed.sI     Consider using the 'anaconda-project lock' command to lock the project.s     Unlocked env specs are: s   , s   Created project archive %s(,   t   problems_statusR&   R   R(   R$   R   R   R   RN   t   project_fileR   R   R   R"   t
   pop_errorst   has_unsaved_changesRh   t   directory_patht"   union_of_requirements_for_all_envsR   t   isabsR   R%   t   uuidt   uuid4t   lowerR;   R   t   nameRy   R   RC   t   removeR#   t	   env_specsRn   t   lock_sett   disabledR!   t   lenR/   R   Rm   R    (   t   projectR   t   failedR$   R(   Rg   t   relative_dest_fileR/   t   tmp_filenameR1   t   unlockedt   env_spec(    (    s8   lib/python2.7/site-packages/anaconda_project/archiver.pyt   _archive_project  sr    
+ """
&c         C@ s2   t  j |  d d  } t | j    SWd  QXd  S(   Nt   modeR>   (   R{   R|   Rm   t   namelist(   t   zip_pathR~   (    (    s8   lib/python2.7/site-packages/anaconda_project/archiver.pyt   _list_files_zipn  s    c         C@ s`   t  j |  d d E } t g  | j   D]' } | j   sF | j   r( | j ^ q(  SWd  QXd  S(   NR   R>   (   Rt   RA   Rm   t
   getmemberst   isregt   isdirR   (   t   tar_pathRx   t   member(    (    s8   lib/python2.7/site-packages/anaconda_project/archiver.pyt   _list_files_tars  s    c      	   C@ s  t  j   } z t j |  d d  } | j |  x | D] \ } } | j d | | f  t j j | |  } t j j	 |  r t
 |  t j | |  q; t
 t j j |   t j | |  q; WWd  QXWd  y t j |  Wn t t f k
 r n XXd  S(   NR   R>   s   Unpacking %s to %s(   t   tempfilet   mkdtempR{   R|   t
   extractallR/   R   R   R   R   R   t   shutilt   copystatR9   t   copy2t   rmtreeRC   R#   (   R   t   src_and_destR(   t   tmpdirR~   t   srct   destt   src_path(    (    s8   lib/python2.7/site-packages/anaconda_project/archiver.pyt   _extract_files_zipy  s     
c      
   C@ s  t  j |  d d  } x | D] \ } } | j d | | f  | j |  } | j   r t t j j |   | j	 | |  n | j
   s t  t |  y | j | | t  Wn! t k
 r | j | |  n X| j | |  | j | |  q WWd  QXd  S(   NR   R>   s   Unpacking %s to %s(   Rt   RA   R/   t	   getmemberR   R   R   R   R9   t   makefileR   R5   t   chownR"   t	   TypeErrort   chmodt   utime(   R   R   R(   Rx   R   R   R   (    (    s8   lib/python2.7/site-packages/anaconda_project/archiver.pyt   _extract_files_tar  s    
c         @ s1   |  j  d t j  }    f d       |  d   S(   NR   c         @ sg   t  j j |   \ } } | d k r. |  | f S| d  k rG   | |  S  | t  j j | |   Sd  S(   NR4   (   R   R   t   splitR&   R   (   t   headt   tailR9   R   (   t   _helper(    s8   lib/python2.7/site-packages/anaconda_project/archiver.pyR     s    
(   R   R   t   sepR&   (   R   (    (   R   s8   lib/python2.7/site-packages/anaconda_project/archiver.pyt   _split_after_first  s    c         C@ s  | |   } t  |  d k r/ | j d  d  Sg  t | g  | D] } t |  ^ q?  D]! \ } \ } } | | | f ^ qX }	 |	 d d }
 |
 d k r | j d |	 d d  d  S| d  k r |
 } n  t j j |  r"| d  k s t  t j j	 t j j
 |   } t j j |  } nf | d  k r=t j   } n  t j j	 t j j
 |   } t j j	 t j j
 t j j | |    } | j |  st  t j j |  r| j d |  d  Sg  } x |	 D] \ } } } | |
 k r| j d d | |
 f  d  S| d  k rqn  t j j	 t j j
 t j j | |    } | j |  sw| j d	 | | | f  d  S| j | | f  qW| | f S(
   Ni    s7   A valid project archive must contain at least one file.i   s   ..s9   Archive contains relative path '%s' which is not allowed.s   Directory '%s' already exists.s<   A valid project archive contains only one project directory s;   with all files inside that directory. '%s' is outside '%s'.s>   Archive entry '%s' would end up at '%s' which is outside '%s'.(   R   R$   R&   t   zipR   R   R   R   R5   t   realpathR   R9   t   getcwdR   R7   RN   R!   (   t   archive_patht
   list_filest   project_dirt
   parent_dirR(   t   namesR   t   prefixt	   remaindert   itemst   candidate_prefixt   canonical_project_dirt   canonical_parent_dirR   R   (    (    s8   lib/python2.7/site-packages/anaconda_project/archiver.pyt   _get_source_and_dest_files  sN    M	-	-t   _UnarchiveStatusc           B@ s   e  Z d    Z RS(   c         C@ s,   t  t |   j d | d |  | |  _ d  S(   NR   R   (   t   superR   R   R   (   R   R   R   R   (    (    s8   lib/python2.7/site-packages/anaconda_project/archiver.pyR     s    (   R   R   R   (    (    (    s8   lib/python2.7/site-packages/anaconda_project/archiver.pyR     s   c         C@ su  | d k	 r9 t j j |  r9 | d k	 r9 t d   n  t |  } d } d } |  j d  ro t } t } nt t	 g  d d d g D] } |  j |  ^ q  r t
 } t } n4 | j d |   t d t d d	 |  d
 | j    Sy7t |  | | | |  } | d k r-t d t d d	 |  d
 | j    S| \ } }	 t |	  d k r{| j d  t d t d d	 |  d
 | j    St j j |  st  t j |  y | |  |	 |  WnD t k
 r}
 y t j |  Wn t t f k
 rn X|
  n Xt d t d d | d |  SWnT t t t j t j f k
 rp}
 | j t |
   t d t d d d
 | j    SXd S(   s  Unpack an archive of files in the project.

    This takes care of several details, for example it deals with
    hostile archives containing files outside of the dest
    directory, and it handles both tar and zip.

    It does not load or validate the unpacked project.

    project_dir can be None to auto-choose one.

    If parent_dir is non-None, place the project_dir in it. This is most useful
    if project_dir is None.

    Args:
        archive_filename (str): the tar or zip archive file
        project_dir (str): the directory that will contain the project config file
        parent_dir (str): place project directory in here

    Returns:
        a ``Status``, if failed has ``errors``, on success has a ``project_dir`` property
    sJ   If supplying parent_dir to unarchive, project_dir must be relative or Nones   .zips   .tars   .tar.gzs   .tar.bz2sE   Unsupported archive filename %s, must be a .zip, .tar.gz, or .tar.bz2R   R   s   Could not unpack archive %sR   i    s9   Archive does not contain a project directory or is empty.s   Project archive unpacked to %s.R   s   Failed to read project archive.N(    R&   R   R   R   t
   ValueErrorR   R;   R   R   t   anyR   R   R$   R   R"   R   R   R   RN   R5   t   makedirst	   ExceptionR   R   RC   R#   R   R    R{   Rt   t   TarErrorR%   (   t   archive_filenameR   R(   R   R   t   extract_filest   suffixt   resultR   R   R1   (    (    s8   lib/python2.7/site-packages/anaconda_project/archiver.pyt   _unarchive_project  sZ    *	1	



!(3   t   __doc__t
   __future__R    R   R@   RD   R8   R   R   R   RR   Rt   R   R   R{   t   anaconda_project.frontendR   t   anaconda_project.internalR   t'   anaconda_project.internal.simple_statusR   t,   anaconda_project.internal.directory_containsR   t    anaconda_project.internal.renameR   t"   anaconda_project.internal.makedirsR   t   objectR   R2   R3   RH   RJ   RV   RZ   R\   Rh   Rq   Ry   R   R   R   R   R   R   R   R   R   R   R&   R   (    (    (    s8   lib/python2.7/site-packages/anaconda_project/archiver.pyt   <module>   sP   	)												T						: