σ
ΌS]c           @@ sL  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
 m Z d  d l m Z d  d l m Z d  d l m Z m Z e j j d  Z e e  d e f d	     YZ d
   Z d   Z d   Z d d  Z d d  Z d d  Z d d  Z d   Z  d   Z! d   Z" d S(   i    (   t   print_functiont   absolute_importN(   t   makedirs_ok_if_exists(   t   LocalStateFile(   t   _load_string(   t   possible_project_file_namest   DEFAULT_PROJECT_FILENAMEs   ./build/tmpt   TmpDirc           B@ s#   e  Z d    Z d   Z d   Z RS(   c         C@ s   t  j d | d t  |  _ d  S(   Nt   prefixt   dir(   t   tempfilet   mkdtempt	   local_tmpt   _dir(   t   selfR   (    (    sK   lib/python2.7/site-packages/anaconda_project/internal/test/tmpfile_utils.pyt   __init__   s    c         C@ s  d   } t  j   d k r$ | } n d  } y t j d |  j d |  WnΎ t k
 r} | d  k rΨ t d |  j t |  f d t	 j
 y0 t d |  j t j |  j  f d t	 j
 Wn t k
 rΞ n X|  qt d |  j t |  f d t	 j
 |  n Xd  S(	   Nc         S@ s$   t  d | |  | f d t j d  S(   Ns   %s: Error on func %r exc %rt   file(   t   printt   syst   stderr(   t   funct   patht   exc(    (    sK   lib/python2.7/site-packages/anaconda_project/internal/test/tmpfile_utils.pyt
   log_errors    s    t   WindowsR   t   onerrors#   Exception cleaning up TmpDir %s: %sR   s   Files in %s: %rs    Failed to clean up TmpDir %s: %s(   t   platformt   systemt   Nonet   shutilt   rmtreeR   t	   ExceptionR   t   strR   R   t   ost   listdir(   R   t   typet   valuet	   tracebackR   R   t   e(    (    sK   lib/python2.7/site-packages/anaconda_project/internal/test/tmpfile_utils.pyt   __exit__   s     		&0	&c         C@ s   |  j  S(   N(   R   (   R   (    (    sK   lib/python2.7/site-packages/anaconda_project/internal/test/tmpfile_utils.pyt	   __enter__=   s    (   t   __name__t
   __module__R   R'   R(   (    (    (    sK   lib/python2.7/site-packages/anaconda_project/internal/test/tmpfile_utils.pyR      s   		c         C@ sΎ   t  d d  © } x |  j   D]{ \ } } t j j | |  } | d  k rY t |  q t t j j |   t j	 | d d   } | j
 |  Wd  QXq W| t j j |   SWd  QXd  S(   NR   s   test-t   ws   utf-8(   R   t   itemsR!   R   t   joinR   R   t   dirnamet   codecst   opent   writet   realpath(   t   contentsR   R.   t   filenamet   file_contentR   t   f(    (    sK   lib/python2.7/site-packages/anaconda_project/internal/test/tmpfile_utils.pyt   with_directory_contentsA   s    c         C@ sπ   t  |   } | d  k r+ t d |    n  |  } d | k rZ | d d d d d } n  d | k rw | d d } n  d	 | k r | d d
 } n  | |  k	 rθ y t  |  | SWqμ t k
 rδ } t d | d t j |  qμ Xn |  Sd  S(   Ns   Broken yaml: %rt	   env_specss   
s   env_specs:
s     default:
s       description: default
t   names   name: some_name
t	   platformss&   platforms: [linux-64, osx-64, win-64]
s   Failed to parse: R   (   R   R   t   AssertionErrorR   R   R   R   (   t   contentt   yamlt   modifiedR&   (    (    sK   lib/python2.7/site-packages/anaconda_project/internal/test/tmpfile_utils.pyt   complete_project_file_contentO   s$    
c         C@ s   i  } x? |  j    D]1 \ } } | t k r: t |  } n  | | | <q Wt g  | j   D] } | t k rX | ^ qX  d k r t d  | t <n  t | |  S(   Ni    t    (   R,   R   R?   t   lent   keysR   R7   (   R3   R   t   new_contentsR4   R5   t   key(    (    sK   lib/python2.7/site-packages/anaconda_project/internal/test/tmpfile_utils.pyt/   with_directory_contents_completing_project_filej   s    7c         C@ si   | d  k r t } n  d d  l } | j d | d t  } z |  |  SWd  | j   t j | j  Xd  S(   Ni    R	   t   delete(	   R   R   R
   t   NamedTemporaryFilet   Falset   closeR!   t   removeR9   (   R   R	   R
   R6   (    (    sK   lib/python2.7/site-packages/anaconda_project/internal/test/tmpfile_utils.pyt   with_temporary_fileu   s    	
c      
   C@ s   | d  k r t } n  t d d  ^ } t j j | |   } t j | d d d  } | j |  | j	   Wd  QX| |  SWd  QXd  S(   NR   s   test-R+   t   encodings   utf-8(
   R   R   R   R!   R   R-   R/   R0   R1   t   flush(   R4   R3   R   R	   R.   t   fullR6   (    (    sK   lib/python2.7/site-packages/anaconda_project/internal/test/tmpfile_utils.pyt   with_named_file_contents   s    	c         @ s"      f d   } t  | d | S(   Nc         @ s7   |  j    j d   |  j   |  j    |  j  S(   Ns   UTF-8(   R1   t   encodeRM   RI   R9   (   R6   (   R3   R   (    sK   lib/python2.7/site-packages/anaconda_project/internal/test/tmpfile_utils.pyt   with_file_object   s    

R	   (   RK   (   R3   R   R	   RQ   (    (   R3   R   sK   lib/python2.7/site-packages/anaconda_project/internal/test/tmpfile_utils.pyt   with_file_contents   s    c         @ s"     f d   } t  |  | d | S(   Nc         @ s     d t  j j |   g  S(   Nt   python(   R!   R   t   abspath(   R4   (   R   (    sK   lib/python2.7/site-packages/anaconda_project/internal/test/tmpfile_utils.pyt   script_wrapper   s    R	   (   RR   (   R3   R   R	   RU   (    (   R   sK   lib/python2.7/site-packages/anaconda_project/internal/test/tmpfile_utils.pyt!   with_temporary_script_commandline   s    c      	   C@ si   d d  l  } | j d t d t d d d d  } | j |  j d   | j   d	 t j j	 | j
  g S(
   Ni    R	   RF   t   suffixs   .pyR   t   script_s   utf-8RS   (   R
   RG   R   RH   R1   RP   RI   R!   R   RT   R9   (   R3   R
   R6   (    (    sK   lib/python2.7/site-packages/anaconda_project/internal/test/tmpfile_utils.pyt   tmp_script_commandline€   s
    $
c          C@ sQ   d d  l  }  |  j d t d t  } t | j  } | j   t j | j  | S(   Ni    R	   RF   (	   R
   RG   R   RH   R   R9   RI   R!   RJ   (   R
   R6   t   local_state(    (    sK   lib/python2.7/site-packages/anaconda_project/internal/test/tmpfile_utils.pyt   tmp_local_state_file―   s    
c         @ s      f d   } t  |  S(   sF   Call 'f' with a zip of 'contents' and an empty working directory name.c      	   @ sv   t  j   j d  = } x3  j   D]% \ } } | j | | j d   q% WWd  QX   f d   } t t   |  S(   NR+   s   utf-8c         @ s      j  |   S(   N(   R9   (   R.   (   R6   t   handle(    sK   lib/python2.7/site-packages/anaconda_project/internal/test/tmpfile_utils.pyt   using_directoryΑ   s    (   t   zipfilet   ZipFileR9   R,   t   writestrRP   R7   t   dict(   R\   t   zfRD   R$   R]   (   R3   R6   (   R\   sK   lib/python2.7/site-packages/anaconda_project/internal/test/tmpfile_utils.pyt   using_temporary_fileΌ   s
    #(   RK   (   R3   R6   Rc   (    (   R3   R6   sK   lib/python2.7/site-packages/anaconda_project/internal/test/tmpfile_utils.pyt   with_tmp_zipfileΉ   s    
(#   t
   __future__R    R   R/   R!   R   R   R   R
   R^   t"   anaconda_project.internal.makedirsR   t!   anaconda_project.local_state_fileR   t   anaconda_project.yaml_fileR   t   anaconda_project.project_fileR   R   R   RT   R   t   objectR   R7   R?   RE   R   RK   RO   RR   RV   RY   R[   Rd   (    (    (    sK   lib/python2.7/site-packages/anaconda_project/internal/test/tmpfile_utils.pyt   <module>   s0   
&					
