ó
?…]c           @   sƒ  d  d l  Z  d  d l m Z m Z d  d l m Z m Z m Z m Z d  d l	 Z	 d  d l
 Z
 d  d l m Z d  d l Z d  d l Z d  d l m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z d  d l Z d  d l Z d  d l m Z m Z m Z m  Z  m! Z! d  d l" m# Z# m$ Z$ m% Z% m& Z& d  d l' Z' d  d l( m) Z) m* Z* d  d l+ Z, d  d	 l- m. Z. e' j/ d
 k Z0 e j1 e2 ƒ Z3 d Z4 d „  Z5 d „  Z6 d e f d „  ƒ  YZ7 d „  Z8 d „  Z9 d d „ Z; d „  Z< d „  Z= d „  Z> e? d „ Z@ e@ ZA ZB ZC d „  ZD d „  ZE d eF f d „  ƒ  YZG e  jH d „  ƒ ZI d „  ZJ d( d" „ ZK d# „  ZL d$ d% „ ZM d& „  ZN d' „  ZO d S()   iÿÿÿÿN(   t   ProcessPoolExecutort   Executor(   t   ENOENTt   EACCESt   EPERMt   EROFS(   t   chain(
   t   isdirt   isfilet   basenamet   dirnamet   joint   splitt   lexistst   normpatht   abspatht   islink(   t   S_IEXECt   S_IMODEt   S_ISDIRt   S_ISREGt   S_IWRITE(   t   check_outputt   CalledProcessErrort   STDOUTt   list2cmdline(   t   mkdtempt   NamedTemporaryFile(   t   string_typest   win32s   .c~c         C   s   d d l  m } | |  ƒ S(   Niÿÿÿÿ(   t   find_executable(   t   distutils.spawnR   (   t
   executableR   (    (    s;   lib/python2.7/site-packages/conda_package_handling/utils.pyt   which   s    c         C   s  yŒ t  j |  ƒ j } t | ƒ rB t  j |  t | ƒ t Bt Bƒ nE t | ƒ sZ t	 |  ƒ rw t  j |  t | ƒ t Bƒ n t
 j d |  ƒ t SWnŒ t k
 r} t | d d  ƒ } | t f k rÕ t
 j d |  ƒ ‚  q| t t t f k rt
 j d |  | ƒ t St
 j d |  | ƒ ‚  n Xd  S(   Ns    path cannot be made writable: %st   errnos,   tried to make writable, but didn't exist: %ss%   tried make writable but failed: %s
%rs!   Error making path writable: %s
%r(   t   ost   lstatt   st_modeR   t   chmodR   R   R   R   R   t   logt   debugt   Truet	   Exceptiont   getattrt   NoneR   R   R   R   t   Falset   warn(   t   patht   modet   et   eno(    (    s;   lib/python2.7/site-packages/conda_package_handling/utils.pyt   make_writable   s$    !t   DummyExecutorc           B   s   e  Z d  „  Z RS(   c         g   s1   x* | D]" } x | D] } | | ƒ Vq Wq Wd  S(   N(    (   t   selft   funct	   iterablest   iterablet   thing(    (    s;   lib/python2.7/site-packages/conda_package_handling/utils.pyt   map7   s    (   t   __name__t
   __module__R:   (    (    (    s;   lib/python2.7/site-packages/conda_package_handling/utils.pyR4   6   s   c         C   s    |  d k r t  ƒ  St d |  ƒ S(   Ni   t   max_workers(   R4   R    (   t	   processes(    (    s;   lib/python2.7/site-packages/conda_package_handling/utils.pyt   get_executor=   s    c         C   s   t  |  ƒ rs x| t j |  ƒ D]P \ } } } x> t j | | f ƒ D]' }  y t t | |  ƒ ƒ WqA qA XqA Wq Wn y t |  ƒ Wn n Xd  S(   N(   R   R#   t   walkR   t   from_iterableR3   R   (   R/   t   roott   dirst   files(    (    s;   lib/python2.7/site-packages/conda_package_handling/utils.pyt   recursive_make_writableA   s    c            sÃ   | s t  r d n d } n  | d k r1 t |  ƒ Sg  } xx |  D]p ‰  d ˆ  k rY d } n@ d ˆ  k rn d } n+ t ‡  f d †  d	 Dƒ ƒ s“ d } n d } | j | ˆ  | ƒ q> Wd j | ƒ Sd  S(
   Ns   cmd.exet   basht   "t   'c         3   s   |  ] } | ˆ  k Vq d  S(   N(    (   t   .0t   _(   t   arg(    s;   lib/python2.7/site-packages/conda_package_handling/utils.pys	   <genexpr>e   s    t    s   
t    (   RL   s   
(   t   on_winR   t   anyt   appendR   (   t	   argumentst   shellt   quotedt   quote(    (   RK   s;   lib/python2.7/site-packages/conda_package_handling/utils.pyt   quote_for_shellS   s    
			c         O   sf  t  |  ƒ }  t r£y% t d j |  ƒ d t d t ƒ} WqUyd  } t d d d t ƒ h } | j	 d j t
 |  g ƒ ƒ ƒ | j	 d ƒ | j	 d j t
 |  g ƒ ƒ ƒ | j	 d	 ƒ | j } Wd  QXt | d
 ƒ % } | j ƒ  } |  | k sõ t ‚ Wd  QXt j d } d }	 t | d d | g d t d d  d d  d d  d |	 ƒ} Wq t k
 r›}
 |
 j d k r‚t j d j | ƒ ƒ ‚  qœt j d j | ƒ ƒ q XqUXn² y t j d ƒ Wn n Xt d ƒ } | rUt d ƒ rUy? t | d d d t t j ƒ  d ƒ d |  d g d t ƒ} Wn' t k
 rDt j d j | ƒ ƒ n Xt j d ƒ n  t j |  ƒ d  S(   Ns   RD /S /Q "{}" > NUL 2> NULRR   t   stderrt   suffixs   .batt   deletes	   RD /S {}
s   chcp 65001
s   EXIT 0
t   rt   COMSPECi   s   /ds   /ct   stdoutt   stdint   creationflagsi   s7   Removing folder {} the fast way failed.  Output was: {}s:   removing dir contents the fast way failed.  Output was: {}s   .emptyt   rsyncs   -as   --forces   --deletet   /(   R   RN   R   t   formatR)   R   R,   R   R-   t   writeRU   t   namet   opent   readt   AssertionErrorR#   t   environR   t
   returncodeR'   t   errorR(   t   makedirsR!   R   R   t   getcwdt   shutilt   rmtree(   R/   t   argst   kwargst   outRb   t
   batch_filet   contentst   contentt   comspect   CREATE_NO_WINDOWR1   R^   (    (    s;   lib/python2.7/site-packages/conda_package_handling/utils.pyRl   m   sX    

"c         C   s  y t  |  ƒ t j |  ƒ Wnmt k
 rŠy t j |  |  d ƒ Wq‹t k
 r†t rmt d d ƒ  } t | d ƒ . } | j d ƒ | j d ƒ | j d ƒ Wd QXt	 |  ƒ \ } } |  d } d	 } x4 t
 | ƒ r| j d
 d j | ƒ } | d	 7} qÏ Wd } y. t d d | | | t | ƒ g d t ƒ} Wn* t k
 rct j d j |  | ƒ ƒ n XWd QXn  t j d j |  ƒ ƒ q‹Xn Xd S(   sõ   If files are in use, especially on windows, we can't remove them.
    The fallback path is to rename them (but keep their folder the same),
    which maintains the file handle validity.  See comments at:
    https://serverfault.com/a/503769
    s   .conda_trashRW   s   .batt   ws   @pushd "%1"
s   @REM Rename src to dests   @ren "%2" "%3" > NUL 2> NUL")Ni   i    s   .conda_trash_{}s	   < empty >s   cmd.exes   /CRV   s6   renaming file path {} to trash failed.  Output was: {}sn   Could not remove or rename {}.  Please remove this file manually (you may need to reboot to free file handles)(   R3   R#   t   unlinkt   EnvironmentErrort   renameRN   R   Rc   Ra   R   R   t   splitextR`   R   R	   R   R   R'   R.   (   R/   t   trash_scriptt   ft   _dirnamet   _fnt   dest_fnt   counterRo   (    (    s;   lib/python2.7/site-packages/conda_package_handling/utils.pyt   unlink_or_rename_to_trash³   s:    

c         C   sI   t  |  ƒ } x6 t | ƒ rD t j | ƒ rD t | ƒ t  | ƒ } q Wd  S(   N(   R
   R   R#   t   listdirt   rmdir(   R/   t   parent_path(    (    s;   lib/python2.7/site-packages/conda_package_handling/utils.pyt   remove_empty_parent_pathsÜ   s    
c         O   s½   t  |  ƒ z_ t |  ƒ }  t |  ƒ r? t |  ƒ r? t |  ƒ n) t |  ƒ rX t |  ƒ n t j d |  ƒ Wd t |  ƒ rŒ t j	 d |  ƒ t
 SXt |  ƒ r¦ t |  ƒ n  | r¹ t |  ƒ n  t S(   s÷   
    Completely delete path
    max_retries is the number of times to retry on failure. The default is 5. This only applies
    to deleting a directory.
    If removing path fails and trash is True, files will be moved to the trash directory.
    s0   rm_rf failed. Not a link, file, or directory: %sNs   rm_rf failed for %s(   RE   R   R   R   R‚   R   R€   R'   R(   t   infoR-   t   delete_trashR„   R)   (   R/   t   clean_empty_parentsRm   t   kw(    (    s;   lib/python2.7/site-packages/conda_package_handling/utils.pyt   rm_rfä   s     
c   	      C   s  |  s t  j }  n  t d g ƒ } xç t j |  d t ƒD]Ð \ } } } g  | D] } | | k rM | ^ qM | (x˜ | D] } t j | d ƒ s¡ t j | d t ƒ rs t | | ƒ } y t j	 | ƒ t
 | ƒ Wqt t f k
 rÿ } t j d | | j | ƒ qXqs qs Wq7 Wd  S(   Nt   envst   topdowns   *.conda_trash*t   *s   %r errno %d
Cannot unlink %s.(   t   syst   prefixt   setR#   R@   R)   t   fnmatcht   CONDA_TEMP_EXTENSIONR   Rv   R„   t   OSErrort   IOErrorR'   R(   R"   (	   RŽ   t   excludeRB   RC   RD   t   dt   fnt   filenameR1   (    (    s;   lib/python2.7/site-packages/conda_package_handling/utils.pyR†     s    %&c         C   sy   t  |  ƒ s d  Sy t |  ƒ Wn n XxJ t j |  d t ƒD]3 \ } } } x! | D] } t t | | ƒ ƒ qT Wq> Wd  S(   NR‹   (   R   Rl   R#   R@   R-   R€   R   (   t   dirpathRB   RC   RD   R{   (    (    s;   lib/python2.7/site-packages/conda_package_handling/utils.pyR‚     s    %t   TemporaryDirectoryc           B   se   e  Z d  Z d	 Z e Z d d e j ƒ  d „ Z	 d „  Z
 d „  Z e e d „ Z d „  Z d „  Z RS(
   s+  Create and return a temporary directory.  This has the same
    behavior as mkdtemp but can be used as a context manager.  For
    example:

        with TemporaryDirectory() as tmpdir:
            ...

    Upon exiting the context, the directory and everything contained
    in it are removed.
    RM   s   .cph_tmpc         C   s   t  | | | ƒ |  _ d  S(   N(   R   Rb   (   R5   RW   RŽ   t   dir(    (    s;   lib/python2.7/site-packages/conda_package_handling/utils.pyt   __init__4  s    c         C   s   d j  |  j j |  j ƒ S(   Ns	   <{} {!r}>(   R`   t	   __class__R;   Rb   (   R5   (    (    s;   lib/python2.7/site-packages/conda_package_handling/utils.pyt   __repr__7  s    c         C   s   |  j  S(   N(   Rb   (   R5   (    (    s;   lib/python2.7/site-packages/conda_package_handling/utils.pyt	   __enter__:  s    c         C   s€   |  j  r| |  j r| y t |  j  ƒ Wn | j d |  j  ƒ n Xt |  _ | r| | j r| | j d j |  ƒ | j ƒ q| n  d  S(   Ns   Conda-package-handling says: "I tried to clean up, but I could not.  There is a mess in %s that you might want to clean up yourself.  Sorry..."s   Implicitly cleaning up {!r}(   Rb   t   _closedR‰   R.   R)   R`   t   ResourceWarning(   R5   t   _warnt	   _warnings(    (    s;   lib/python2.7/site-packages/conda_package_handling/utils.pyt   cleanup=  s    		c         C   s   |  j  ƒ  d  S(   N(   R£   (   R5   t   exct   valuet   tb(    (    s;   lib/python2.7/site-packages/conda_package_handling/utils.pyt   __exit__J  s    c         C   s   |  j  d t ƒ d  S(   NR¡   (   R£   R)   (   R5   (    (    s;   lib/python2.7/site-packages/conda_package_handling/utils.pyt   __del__M  s    N(   R;   R<   t   __doc__R,   Rb   R-   RŸ   R#   Rj   R›   R   Rž   R¢   R£   R§   R¨   (    (    (    s;   lib/python2.7/site-packages/conda_package_handling/utils.pyR™   $  s   
			c         c   s7   t  j ƒ  } z t  j |  ƒ d  VWd  t  j | ƒ Xd  S(   N(   R#   Rj   t   chdir(   t   destt   curdir(    (    s;   lib/python2.7/site-packages/conda_package_handling/utils.pyt	   tmp_chdirR  s
    	c         C   sD   t  |  t ƒ s t |  d ƒ r@ |  d  k	 r7 |  g }  q@ g  }  n  |  S(   Nt   __iter__(   t
   isinstanceR   t   hasattrR,   (   RK   (    (    s;   lib/python2.7/site-packages/conda_package_handling/utils.pyt   ensure_list\  s
    	s   (.*[\\\\/])?\.git[\\\\/].*s   (.*[\\\\/])?\.git$s   (.*)?\.DS_Store.*s   .*\.la$s   conda-meta.*c         C   sz   x? | D]7 } t  j | ƒ } t |  ƒ t t | j |  ƒ ƒ }  q Wg  |  D]- } t j j t j j | | ƒ ƒ sI | ^ qI S(   sI   Remove things like the .git directory from the list of files to be copied(	   t   ret   compileR   t   filtert   matchR#   R/   R   R   (   t
   files_listRŽ   t   filter_patternst   patternRY   R{   (    (    s;   lib/python2.7/site-packages/conda_package_handling/utils.pyt   filter_filese  s    &c         C   s   t  |  | d d ƒS(   NR·   s   info[\\/]index.jsons   info[\\/]filess   info[\\/]paths.jsons   info[\\/]about.jsons   info[\\/]has_prefixs   info[\\/]hash_input_filess   info[\\/]hash_input.jsons   info[\\/]run_exports.yamls   info[\\/]run_exports.jsons   info[\\/]gits   info[\\/]recipe[\\/].*s   info[\\/]recipe_log.jsons   info[\\/]recipe.tars   info[\\/]test[\\/].*s   info[\\/]LICENSE.txts   info[\\/]requiress   info[\\/]metas   info[\\/]platforms   info[\\/]no_links   info[\\/]link.jsons   info[\\/]icon.png(   s   info[\\/]index.jsons   info[\\/]filess   info[\\/]paths.jsons   info[\\/]about.jsons   info[\\/]has_prefixs   info[\\/]hash_input_filess   info[\\/]hash_input.jsons   info[\\/]run_exports.yamls   info[\\/]run_exports.jsons   info[\\/]gits   info[\\/]recipe[\\/].*s   info[\\/]recipe_log.jsons   info[\\/]recipe.tars   info[\\/]test[\\/].*s   info[\\/]LICENSE.txts   info[\\/]requiress   info[\\/]metas   info[\\/]platforms   info[\\/]no_links   info[\\/]link.jsons   info[\\/]icon.png(   R¹   (   R¶   RŽ   (    (    s;   lib/python2.7/site-packages/conda_package_handling/utils.pyt   filter_info_filesq  s,                        i   c            ss   t  t | ƒ } | s- t d j | ƒ ƒ ‚ n	 | ƒ  } x0 t ‡  ‡ f d †  d ƒ D] } | j | ƒ qR W| j ƒ  S(   Ns   Unrecognized hash algorithm: {}c              s   ˆ j  ˆ  ƒ S(   N(   Rd   (    (   t
   buffersizet   fd(    s;   lib/python2.7/site-packages/conda_package_handling/utils.pyt   <lambda>‘  RM   RM   (   R+   t   hashlibt
   ValueErrorR`   t   itert   updatet	   hexdigest(   R¼   t	   algorithmR»   t	   hash_implt   block(    (   R»   R¼   s;   lib/python2.7/site-packages/conda_package_handling/utils.pyt	   _checksum‹  s    	"c         C   s   t  |  d ƒ S(   Nt   sha256(   RÆ   (   R¼   (    (    s;   lib/python2.7/site-packages/conda_package_handling/utils.pyt   sha256_checksum–  s    c         C   s   t  |  d ƒ S(   Nt   md5(   RÆ   (   R¼   (    (    s;   lib/python2.7/site-packages/conda_package_handling/utils.pyt   md5_checksumš  s    (   s   (.*[\\\\/])?\.git[\\\\/].*s   (.*[\\\\/])?\.git$s   (.*)?\.DS_Store.*s   .*\.la$s   conda-meta.*(P   t
   contextlibt   concurrent.futuresR    R   R"   R   R   R   R   R   R¾   t	   itertoolsR   t   loggingR#   t   os.pathR   R   R	   R
   R   R   R   R   R   R   R²   Rk   t   statR   R   R   R   R   t
   subprocessR   R   R   R   R   t   tempfileR   R   t   warningsR¢   t   sixR   t   platformRN   t	   getLoggert   __file__R'   R‘   R!   R3   R4   R?   RE   R,   RU   Rl   R€   R„   R-   R‰   t   try_rmdir_all_emptyt   move_to_trasht   move_path_to_trashR†   R‚   t   objectR™   t   contextmanagerR­   R±   R¹   Rº   RÆ   RÈ   RÊ   (    (    (    s;   lib/python2.7/site-packages/conda_package_handling/utils.pyt   <module>   sX   "F("					F	)			.
		    		