
@K7]c           @` s  d  d l  m Z m Z m Z d  d l 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 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 d d l m Z d  d l m Z d  d	 l m Z m Z d  d
 l m  Z  m! Z! m" Z" m# Z# m$ Z$ m% Z% m& Z& m' Z' m( Z( m) Z) m* Z* m+ Z+ e$ rqd  d l m, Z, n  e j- d  d k rd  d l. m/ Z/ n d  d l0 m/ Z/ e j1 d  Z2 e j1 d  Z3 d   Z4 e5 d  Z6 d   Z7 e5 d e8 d  Z9 d d e8 e8 d  Z; d e8 d  Z< e8 d d  Z= d   Z> e8 d e8 d  Z? d   Z@ d   ZA d   ZB d   ZC d   ZD d d   ZE d!   ZF d S("   i    (   t   absolute_importt   divisiont   print_functionN(   t   joint   isdirt   isfilet   abspatht   basenamet   existst   normpatht
   expanduser(   t   CalledProcessErrori   (   t   downloadt   TemporaryDirectory(   t   hashsum_file(   t   external(   t   url_patht   CondaHTTPError(   t   decompressible_extst   tar_xft   safe_print_unicodet	   copy_intot   on_wint   ensure_listt   check_output_envt   check_call_envt    convert_path_for_cygwin_or_msys2t
   get_loggert   rm_rft   LoggingContext(   t   convert_unix_path_to_wini   (   t   urljoins   (?:.+)\.(.+)\.(?:.+)\s(.+)s   (.*?)(\.(?:tar\.)?[^.]+)$c         C` s   t  j d j | d   |   S(   Ns   \1_{}\2i
   (   t   ext_ret   subt   format(   t   fnt
   hash_value(    (    s1   lib/python2.7/site-packages/conda_build/source.pyt   append_hash_to_fn#   s    c         C` s  t  t  } | r& | j d |   n  t |   rV t j j |   rV t j |   n  | d } t | t	  s{ | g } n  d | k r | d n t
 | d  } } t } xM d D]/ }	 |	 | k r t | | |	  } t } Pq q W| j d j |   t |  |  }
 t |
  r3| r| j d	 |  qn| rM| j d
 |  n  x| D]o} d | k r| j d  rt |  } n  t j j |  st j j t j j | |   } n  t |  } n2 | j d  rd t | d  j d d  } n  y; | r| j d |  n  t    t | |
  Wd QXWns t k
 rr} | j d t |  j    t |
  qTt k
 r} | j d t |  j    t |
  qTX| r| j d  n  PqTWt |
  t d |   d } xo d D]g } | | k r| | } t  |
 |  } | | k rQt |
  t d | j!   | | f   n  PqqW| s| swt  |
 d  } n  t |
 |  } t j j |  st" j# |
 |  n  | }
 n  |
 | f S(   s'    Download a source to the local cache. s   Source cache directory is: %st   urlR#   i    t   md5t   sha1t   sha256sn   No hash (md5, sha1, sha256) provided for {}.  Source download forced.  Add hash to recipe to use source cache.s   Found source in cache: %ss   Downloading source to cache: %ss   ://t   ~s	   file:///~s   file:///i   s   \t   /s   Downloading %sNs	   Error: %st   Successs   Could not download %ss   %s mismatch: '%s' != '%s'(   R'   R(   R)   (   R'   R(   R)   ($   R   t   __name__t   infoR   t   ost   patht   islinkt   makedirst
   isinstancet   listR   t   FalseR%   t   Truet   warnR"   R   R   t
   startswithR
   t   isabsR	   R   t   replaceR   R   R   t   strt   stripR   t   RuntimeErrort   NoneR   t   uppert   shutilt   move(   t   cache_foldert   recipe_patht   source_dictt   verboset   logt   source_urlst   unhashed_fnR#   t
   hash_addedt	   hash_typeR0   R&   t   et   hashedt   tpt   expected_hasht	   dest_path(    (    s1   lib/python2.7/site-packages/conda_build/source.pyt   download_to_cache'   s     
*
$#



	c      
   C` s   t  j j |   } t  j |   } t    } x< | D]4 } t j t  j j |  |  t  j j | |   q4 Wt |   x< | D]4 } t j t  j j | |  t  j j | |   q} WWd QXd S(   s   Moves all files/folders one level up.

    This is for when your archive extracts into its own folder, so that we don't need to
    know exactly what that folder is called.N(	   R/   R0   t   dirnamet   listdirR   R@   RA   R   R   (   t   nested_foldert   parentt   flistt   tmpdirt   entry(    (    s1   lib/python2.7/site-packages/conda_build/source.pyt   hoist_single_extracted_foldery   s    2
i  c      
   C` s  t  | | |  |  \ } }	 t |  s7 t j |  n  | rJ t d  n  t d |  6}
 t j j |
 |	  } | j   j	 t
  r t | |
  n  t d  t | | | d | | j   j	 d  r t | | | d | n  t j |
  } t j j |
 | d  } t |  d k r=t j j |  r=t |  n  t j |
  } x< | D]4 } t j t j j |
 |  t j j | |   qSWWd QXd S(	   s!    Uncompress a downloaded source. s   Extracting downloadt   dirsN   Warning: Unrecognized source format. Source file will be copied to the SRC_DIRt   lockings   .whli    i   N(   RP   R   R/   R2   t   printR   R0   R   t   lowert   endswithR   R   R   RR   t   lenRX   R@   RA   (   RD   t   src_dirRB   RC   t   crootRE   t   timeoutRZ   t   src_pathRH   RV   t   unhashed_destRU   t   foldert   f(    (    s1   lib/python2.7/site-packages/conda_build/source.pyt   unpack   s(    
$ic	         C` s  | r d% }	 d% }
 n t t j d  } | }	 | }
 | j | t j  sc t j d | | f  n  t |  |  j	 d  } t |  |  j	 d  } | p d } | j	 d  } t
 t j j |   s t j t j j |   n  t
 |  ry | d k r"t |  d g d | d |	 d |
 nP t |  d d	 d
 g d | d |	 d |
 t |  d d d g d | d |	 d |
 Wqt k
 rd j |  } t |  t j |    qXn |  d d g } | d k r| d t |  g 7} n  y$ t | | | g d |	 d |
 Wn t k
 rt j d k rH| j d  rHt |  } n  t j j |  rit |  } n  t | | | g d |	 d |
 n Xt
 |  st  t |  d | | g d |	 d |
 | rW| } | j d  rt |  d | g d |	 d |
 } | j d  } n  | r&t d |  n  | rWt |  d | g d | d |	 d |
 qWn  yC t |  d d d d d g d |	 d | } | j d  j   } Wn t k
 rg  } n Xx | D] } t j |  } | r| j d  d d k r| j d  } | j d  } t  | d |  } t j j t j j! | |   } | rat d | | | f  n  t"   8 } t# |  | | | d  | d! | d" | d# t$ d$ | Wd% QXqqW| rt |  d& d' d( d) g d | d |	 d |
 t% | d$ | n  | s| j&   n  d% S(*   sz   Mirror (and checkout) a Git repository recursively.

        It's not possible to use `git submodule` on a bare
        repository, so the checkout must be done before we
        know which submodules there are.

        Worse, submodules can be identified by using either
        absolute URLs or relative paths.  If relative paths
        are used those need to be relocated upon mirroring,
        but you could end up with `../../../../blah` and in
        that case conda-build could be tricked into writing
        to the root of the drive and overwriting the system
        folders unless steps are taken to prevent that.
    t   wsB   Error: Attempting to mirror to %s which is outside of GIT_CACHE %sR+   t   HEADt   fetcht   cwdt   stdoutt   stderrt   origins   +HEAD:_conda_cache_origin_heads   symbolic-refs#   refs/heads/_conda_cache_origin_headsA   Failed to update local git cache. Deleting local cached repo: {} t   clones   --mirrori    s   --deptht   win32t   .s	   rev-parses   utf-8s   checkout: %rt   checkoutt   configs   --files   .gitmoduless   --get-regexpR&   i   i   s?   Relative submodule %s found: url is %s, submod_mirror_dir is %st	   git_cachet   git_reft	   git_deptht   is_top_levelRE   Nt	   submodulet   updates   --inits   --recursive('   R>   t   openR/   t   devnullR8   t   sept   syst   exitR   t   rstripR   R0   RQ   R2   R   R   R"   R[   R@   t   rmtreeR;   t   platformR   R   R	   t   AssertionErrorR   t   decodet
   splitlinest   git_submod_ret   matcht   groupR   R   R   t   git_mirror_checkout_recursiveR5   t   git_infot   close(   t   gitt
   mirror_dirt   checkout_dirt   git_urlRs   Rt   Ru   Rv   RE   Rk   Rl   t   FNULLt   git_mirror_dirt   git_checkout_dirt   msgt   argsRq   t   outputt
   submodulesRw   t   matchest   submod_namet   submod_rel_patht
   submod_urlt   submod_mirror_dirt   temp_checkout_dir(    (    s1   lib/python2.7/site-packages/conda_build/source.pyR      s    		%	

$$"!
	c         C` s  t  |  s t j |  n  t j d  } | sA t j d  n  t |  j d d   } |  j d  pk d } |  d } | j	 d  r t j
 j |  } n  | j	 d	  r t t t j
 j | |    } t j d
 k r | j d d  }	 qP| d }	 nS | j d  d j d t j  }	 |	 j	 t j  r>|	 d }	 n  |	 j d d  }	 t | |	  }
 t | |
 | | d | d | d | d t d | | S(   s?    Download a source from a Git repo (or submodule, recursively) R   sO   Error: git is not installed in your root environment or as a build requirement.Ru   it   git_revRh   R   R*   Rp   Ro   t   :t   _i   s   ://R+   Rs   Rt   Rv   RE   (   R   R/   R2   R   t   find_executableR|   R}   t   intt   getR8   R0   R
   R   R	   R   R   R:   t   splitR{   R   R6   (   RD   Rs   R_   RC   RE   R   Ru   Rt   R   t   git_dnR   (    (    s1   lib/python2.7/site-packages/conda_build/source.pyt
   git_source*  s0    
!"c      	   C` s  t  |   s t  t j d  } | sD t t  } | j d  d S| rS d } n t t	 j
 d  } | } t	 j j   } t |  d  | d <d   | j   D } x6d t f d	 t f d
 t f g D]\ } }	 y( t | j   d | d |  d | }
 Wn2 t k
 r-} |	 r.t d t |    q.n Xt j   } | sOt j j } n  | pXd } t |
 d  r|
 j | d  }
 n  | r| j d |  | r| j |
 d  qq | r t d |  t |
 d  q q Wd S(   s    Print info about a Git repo. R   sG   git not installed in root environment.  Skipping recording of git info.NRg   s   .gitt   GIT_DIRc         S` s+   i  |  ]! \ } } t  |  t  |   q S(    (   R;   (   t   .0t   keyt   value(    (    s1   lib/python2.7/site-packages/conda_build/source.pys
   <dictcomp>`  s   	 s   git log -n1s   git describe --tags --dirtys
   git statusRl   Rj   t   envs   git error: %ss   utf-8R   t   ignoreu   ==> %s <==
u   
(    R   R   R   R   R   R-   R7   R>   Ry   R/   Rz   t   environt   copyR   t   itemsR6   R5   R   R   R   t	   ExceptionR;   t   localet   getpreferredencodingR|   Rk   t   encodingt   hasattrR   t   writeR[   R   (   R_   RE   t   foR   RF   Rl   R   R   t   cmdt   check_errorRk   RK   R   (    (    s1   lib/python2.7/site-packages/conda_build/source.pyR   L  sF    			(c         C` sy  | r d } d } n t t j d  } | } | } |  d } t |  sY t j |  n  | j d  d j d d  } t | |  }	 t |	  r t	 d d g d	 |	 d
 | d | n4 t	 d d | |	 g d
 | d | t |	  s t
  |  j d  p d }
 | rt d |
  n  t	 d d |	 | g d
 | d | t	 d d d |
 g d	 | d
 | d | | su| j   n  | S(   s(    Download a source from Mercurial repo. Rg   t   hg_urlR   iR+   R   t   hgt   pullRj   Rk   Rl   Rn   t   hg_tagt   tips   checkout: %rRx   s   -CN(   R>   Ry   R/   Rz   R   R2   R   R:   R   R   R   R   R[   R   (   RD   R_   t   hg_cacheRE   Rk   Rl   R   R   t   hg_dnt
   cache_repoRx   (    (    s1   lib/python2.7/site-packages/conda_build/source.pyt	   hg_sourcez  s2    	
%"!c         C` s  | r d } d } n t t j d  } | } | } d   }	 |  d }
 |  j d  pX d } |	 |  j d  pp d  } t |  s t j |  n  |
 j d d	  d
 j d d  j d d  } t	 | |  } | r d g } n g  } t |  r"t
 d d d | g | d | d | d | nB t
 d d d | g | |
 | g d | d | t |  sdt  t | | | d t d | | s| j   n  | S(   s"    Download a source from SVN repo. Rg   c         S` s   t  |   j   j   d k S(   Nt   yest   truet   1t   on(   R   R   R   R   (   R;   R\   R<   (   t   s(    (    s1   lib/python2.7/site-packages/conda_build/source.pyt
   parse_bool  s    t   svn_urlt   svn_revt   headt   svn_ignore_externalst   noR   i   iR+   R   s   --ignore-externalst   svnt   ups   -rRj   Rk   Rl   t   cot   symlinksRZ   N(   R>   Ry   R/   Rz   R   R   R2   R   R:   R   R   R   R   R6   R   (   RD   R_   t	   svn_cacheRE   Ra   RZ   Rk   Rl   R   R   R   t   svn_revisionR   t   svn_dnR   t
   extra_args(    (    s1   lib/python2.7/site-packages/conda_build/source.pyt
   svn_source  s6    		
.#c         C` s  yt  t |  d   ra t d d d d g d |  } t d d d g d |  } d	 j | |  St t |  d
   r t d d d g d |  } t d d g d |  j   d } d	 j | |  St t |  d   rWt d d g d |  } | j d  } t j d | d t j	 j
 d  } t j d | d t j	 j
 d  } d j | |  Sd j |  t j t j j t |  d     SWnS t k
 rt t  j d |   d j |  t j t j j t |  d     SXd S(   s   This tries to get information about where a recipe came from.  This is different
    from the source - you can have a recipe in svn that gets source via git.s   .gitR   Rr   s   --gets   remote.origin.urlRj   s	   rev-parseRh   s   Origin {}, commit {}s   .hgR   t   pathst   defaultt   idi    s   .svnR   R.   s   utf-8s   Repository Root: (.*)$t   flagsi   s   Revision: (.*)$s   {}, Revision {}s   {}, last modified {}s	   meta.yamls   Failed to checkout source in N(   R   R   R   R"   R   R   R   t   ret   searcht   MR   t   timet   ctimeR/   R0   t   getmtimeR   R   R-   t   debug(   RC   Rm   t   revR.   t   servert   revision(    (    s1   lib/python2.7/site-packages/conda_build/source.pyt   get_repository_info  s0    	"$$		c         C` sh   |  d } t  |  d  I } t  | d  1 } x' | D] } | j | j d d   q5 WWd QXWd QX| S(   sF   Replace windows line endings with Unix.  Return path to modified file.t   _unixt   rbt   wbs   
s   
N(   Ry   R   R:   (   R0   t   out_patht	   inputfilet
   outputfilet   line(    (    s1   lib/python2.7/site-packages/conda_build/source.pyt   _ensure_unix_line_endings  s    
)c         C` sh   |  d } t  |  d  I } t  | d  1 } x' | D] } | j | j d d   q5 WWd QXWd QX| S(   sB   Replace unix line endings with win.  Return path to modified file.t   _winR   R   s   
s   
N(   Ry   R   R:   (   R0   R   R   R   R   (    (    s1   lib/python2.7/site-packages/conda_build/source.pyt   _ensure_win_line_endings  s    
)c         C` s  d } d   |  D } | j d d  } x> | D]6 } | j d  } | d k rV | n t | |  } q/ W| d k r~ d } nt   } d   t | d  D } xu | D]m } | j d  } xU t | d  D]C }	 t j j	 t
 | | t |  |	   r | |	 c d 7<q q Wq Wt | d | j d	 t }
 | |
 d | |
 d k r]t d
  n  t g  | j   D]& \ } } | | |
 d k rm| ^ qm } | S(   s0    Determine the patch strip level automatically. c         S` s"   h  |  ] } | j  d  d   q S(   t   errorsR   (   t   encode(   R   t   filestr(    (    s1   lib/python2.7/site-packages/conda_build/source.pys	   <setcomp>  s   	 R   R   R+   i    c         S` s   i  |  ] } d  |  q S(   i    (    (   R   t   i(    (    s1   lib/python2.7/site-packages/conda_build/source.pys
   <dictcomp>  s   	 i   R   t   reverses+   Patch level ambiguous, selecting least deepN(   R>   R   t   countt   mint   dictt   rangeR   R/   R0   R   R   R^   t   sortedR   R6   R[   R   (   t   filesstrR_   t   maxlevelt   filest   filet   numslasht
   patchlevelt   histot   partst   levelt   orderR   R   (    (    s1   lib/python2.7/site-packages/conda_build/source.pyt   _guess_patch_strip_level  s*    %		*9c         C` s  t  j d  } t   } t j |  d d  } g  } t } t } x | j   D] } | rw t  j d |  rw t } n  t } | j	 |  } | r | j
 d  d k r | j | j
 d   qO | rO | j d  rO | j d  rO t } qO qO WWd  QX| | f S(	   Ns   ^(?:---|\+\+\+) ([^\n\t]+)R   R   s   From [0-9a-f]{40}i   s	   /dev/nullR   s
   git --diff(   R   t   compilet   sett   ioRy   R6   t	   readlinesR   R5   R   R   t   appendR8   (   R0   t   re_filesR   Re   t
   first_linet   is_git_formatt   lt   m(    (    s1   lib/python2.7/site-packages/conda_build/source.pyt   _get_patch_file_details  s     		%c      
   C` s  t  |  s  t j d |  n  | j r8 d  } d  } n t t j d  } | } | } t |  \ } } | r | r t j	 }	 d |	 d <d |	 d <t
 | d d | g d	 |  d
 | d | d |	 | j d 7_ n| j r t d |  n  t j d | j  }
 |
 d  k r/t j d t j j t j   n  t | |   } d | d d | g } yL t t  } | j r|| j d  n  t
 |
 g | d	 |  d
 | d | WnMt k
 rt j d k rt |  } | | d <z y@ | j r| j d  n  t
 |
 g | d	 |  d
 | d | Wn | j r7| j d  n  | j d d  t |  } | | d <z8 y' t
 |
 g | d	 |  d
 | d | Wn
   n XWd  t j j |  rt j |  n  Xn XWd  t j j |  rt j |  n  Xq  n Xd  S(   Ns   Error: no such patch: %sRg   s   conda-buildt   GIT_COMMITTER_NAMEs   conda@conda-build.orgt   GIT_COMMITTER_EMAILt   ams   --committer-date-is-author-dateRj   Rk   Rl   R   i   s   Applying patch: %rt   patchs           Error:
            Cannot use 'git' (not a git repo and/or patch) and did not find 'patch' in: %s
            You can install 'patch' using apt-get, yum (Linux), Xcode (MacOSX),
            or conda, m2-patch (Windows),
        s   -p%ds   --ignore-whitespaces   -is   Trying to apply patch as-isRo   isQ   Applying unmodified patch failed.  Convert to unix line endings and trying again.sY   Applying unix patch failed.  Convert to CRLF line endings and trying again with --binary.i    s   --binary(   R   R|   R}   RE   R>   Ry   R/   Rz   R  R   R   t   git_commits_since_tagR[   R   R   t   build_prefixt   pathsepR   t	   dir_pathsR   R   R-   R.   R   R   R   t   insertR   R0   R   t   remove(   R_   R0   Rr   R   Rk   Rl   R   R   R  t   git_envR  t   patch_strip_levelt
   patch_argsRF   t   unix_ending_filet   win_ending_file(    (    s1   lib/python2.7/site-packages/conda_build/source.pyt   apply_patch'  sl    			

		'
 	'	
 'c         ` s  |  j  d  } t j j |  j j  s= t j |  j j  n  d } t | d  r^ | g } n | } y
x| D]    j	 d  } t j j
 |  j j | r | n d  } t   f d   d D  rt   | |  j j d |  j d	 |  j j d
 |  j j d |  j j d |  j j nd   k rQt   |  j j | |  j d
 |  j j } nd   k rt   | |  j j d
 |  j j nd   k rt   | |  j j d
 |  j j d |  j j d |  j j nSd   k rt j j   d  } t t t
 |  j |    } d   k } | rD| rDt d  t } t j d  n  | rt j j  |  }	 t |	  sxt j |	  n  |  j j rt d | | f  n  t j! | |  q|  j j rt d | | f  n  t" | | |  j j d t# d |  j j d t# n t |  st j |  n  t$   j	 d g    }
 x0 |
 D]( } t% | t
 |  j |  |  j |  q=Wqn WWn4 t& k
 rt' j( |  j j |  j j d    n X|  j j S(   sk   
    given a recipe_dir:
      - download (if necessary)
      - unpack
      - apply patches (if any)
    t   sourcet   keysRd   t    c         3` s   |  ] } |   k Vq d  S(   N(    (   R   t   k(   RD   (    s1   lib/python2.7/site-packages/conda_build/source.pys	   <genexpr>  s    R#   R&   RC   R`   RE   Ra   RZ   R   R   R   R0   t   path_via_symlinksG  WARNING: `path_via_symlink` is too dangerous without specifying a folder,
  conda could end up changing - or deleting - your local source code!
  Going to make copies instead. When using `path_via_symlink` you should
  also take care to run the build outside of your local source code folder(s)
  unless that is your intention.i   s+   Creating sybmolic link pointing to %s at %ss   Copying %s to %sR   t   clobbert   patchest   _failed_provideN(   R#   R&   ()   t   get_sectionR/   R0   R   Rr   t   build_folderR2   R>   R   R   R   t   work_dirt   anyRf   t	   src_cacheR`   RE   Ra   RZ   R   Rs   R   R   R   R   R
   R	   R   R[   R5   R|   R}   RQ   t   symlinkR   R6   R   R  R   R@   RA   (   t   metadatat   metaR   t   dictsRd   R_   t   source_pathR0   R   t   src_dir_symlinkR"  R  (    (   RD   s1   lib/python2.7/site-packages/conda_build/source.pyt   providet  sj    '
. (G   t
   __future__R    R   R   R  R   R/   t   os.pathR   R   R   R   R   R   R	   R
   R   R@   t
   subprocessR   R|   R   t   conda_interfaceR   R   R   t   conda_build.os_utilsR   t   conda_build.conda_interfaceR   R   t   conda_build.utilsR   R   R   R   R   R   R   R   R   R   R   R   R   t   version_infot   urllib.parseR   t   urlparseR  R   R    R%   R5   RP   RX   R6   Rf   R>   R   R   R   R   R   R   R   R   R   R  R  R/  (    (    (    s1   lib/python2.7/site-packages/conda_build/source.pyt   <module>   sN   :R	R	".	%)		
	
		M