B
    XM\                 @   s  d Z ddlmZmZmZ ddlZddlZddlmZ ddl	m
Z
mZ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 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 yddl m!Z" W n  e#k
r   ddl m"Z" Y nX dd	l$m%Z%m&Z& dd
l'm(Z( ddl)m*Z*m+Z+m,Z,m-Z- ddl.m/Z/m0Z0 ddl1m2Z2m3Z3 ddl4m5Z5m6Z6 dZ7dZ8dZ9dZ:dZ;dZ<dZ=dZ>dZ?dZ@dddddddd d!d"d#d$d%d&d'd(d)d*d+d,gZAd-ZBd.ZCeDd/ZEd0d1d2d3d4d5d6ZFd7d8 ZGd9d: ZHd;d< ZId=d> ZJd?d@ ZKdAdB ZLdCdD ZMdEdF ZNdGdH ZOdjdJdKZPdkdLdMZQdldOdPZRdQdR ZSdSdT ZTdUdV ZUdWdX ZVdmd]d^ZWd_d` ZXdndbdcZYdodddeZZdfdg Z[dhdi Z\dS )pz6
Tools for converting Cran packages to conda recipes.
    )absolute_importdivisionprint_functionN)chain)makedirslistdirsepenviron)
basenamecommonprefixexistsisabsisdirisfilejoinnormpathrealpathrelpath)CSafeDumper)
SafeDumper)sourcemetadata)get_or_merge_config)	text_type	iteritemsTemporaryDirectorycc_conda_build)allowed_license_familiesguess_license_family)rm_rfensure_list)get_package_variantsDEFAULT_VARIANTSzQ  {archive_keys}
  {git_url_key} {git_url}
  {git_tag_key} {git_tag}
  {patches}
z*  url: {cranurl}{sel}
  {hash_entry}{sel}
z+{{% set version = '{cran_version}' %}}{sel}aU  {version_source}
{version_binary1}
{version_binary2}

{{% set posix = 'm2-' if win else '' %}}
{{% set native = 'm2w64-' if win else '' %}}

package:
  name: {packagename}
  version: {{{{ version|replace("-", "_") }}}}

source:
{source}
{binary1}
{binary2}

build:
  merge_build_host: True{sel_src_and_win}
  # If this is a new build for the same version, increment the build number.
  number: {build_number}
  {noarch_generic}

  # This is required to make R link correctly on Linux.
  rpaths:
    - lib/R/lib/
    - lib/
  {script_env}
{suggests}
requirements:
  build:{build_depends}

  host:{host_depends}

  run:{run_depends}

test:
  commands:
    # You can put additional test commands to be run here.
    - $R -e "library('{cran_packagename}')"           # [not win]
    - "\"%R%\" -e \"library('{cran_packagename}')\""  # [win]

  # You can also put a file called run_test.py, run_test.sh, or run_test.bat
  # in the recipe that will be run at test time.

  # requires:
    # Put any additional test requirements here.

about:
  {home_comment}home:{homeurl}
  license: {license}
  {summary_comment}summary:{summary}
  license_family: {license_family}
  {license_file}

{extra_recipe_maintainers}

# The original CRAN metadata for this package was:

{cran_metadata}

# See
# http://docs.continuum.io/conda/build.html for
# more information about meta.yaml

a&  #!/bin/bash

# 'Autobrew' is being used by more and more packages these days
# to grab static libraries from Homebrew bottles. These bottles
# are fetched via Homebrew's --force-bottle option which grabs
# a bottle for the build machine which may not be macOS 10.9.
# Also, we want to use conda packages (and shared libraries) for
# these 'system' dependencies. See:
# https://github.com/jeroen/autobrew/issues/3
export DISABLE_AUTOBREW=1

# R refuses to build packages that mark themselves as Priority: Recommended
mv DESCRIPTION DESCRIPTION.old
grep -va '^Priority: ' DESCRIPTION.old > DESCRIPTION
$R CMD INSTALL --build .

# Add more build steps here, if they are necessary.

# See
# http://docs.continuum.io/conda/build.html
# for a list of environment variables that are set during the build process.
a
  #!/bin/bash

if {source_pf_bash}; then
  export DISABLE_AUTOBREW=1
  mv DESCRIPTION DESCRIPTION.old
  grep -v '^Priority: ' DESCRIPTION.old > DESCRIPTION
  $R CMD INSTALL --build .
else
  mkdir -p $PREFIX/lib/R/library/{cran_packagename}
  mv * $PREFIX/lib/R/library/{cran_packagename}

  if [[ $target_platform == osx-64 ]]; then
    pushd $PREFIX
      for libdir in lib/R/lib lib/R/modules lib/R/library lib/R/bin/exec sysroot/usr/lib; do
        pushd $libdir || exit 1
          for SHARED_LIB in $(find . -type f -iname "*.dylib" -or -iname "*.so" -or -iname "R"); do
            echo "fixing SHARED_LIB $SHARED_LIB"
            install_name_tool -change /Library/Frameworks/R.framework/Versions/3.5.0-MRO/Resources/lib/libR.dylib "$PREFIX"/lib/R/lib/libR.dylib $SHARED_LIB || true
            install_name_tool -change /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libR.dylib "$PREFIX"/lib/R/lib/libR.dylib $SHARED_LIB || true
            install_name_tool -change /usr/local/clang4/lib/libomp.dylib "$PREFIX"/lib/libomp.dylib $SHARED_LIB || true
            install_name_tool -change /usr/local/gfortran/lib/libgfortran.3.dylib "$PREFIX"/lib/libgfortran.3.dylib $SHARED_LIB || true
            install_name_tool -change /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libquadmath.0.dylib "$PREFIX"/lib/libquadmath.0.dylib $SHARED_LIB || true
            install_name_tool -change /usr/local/gfortran/lib/libquadmath.0.dylib "$PREFIX"/lib/libquadmath.0.dylib $SHARED_LIB || true
            install_name_tool -change /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libgfortran.3.dylib "$PREFIX"/lib/libgfortran.3.dylib $SHARED_LIB || true
            install_name_tool -change /usr/lib/libgcc_s.1.dylib "$PREFIX"/lib/libgcc_s.1.dylib $SHARED_LIB || true
            install_name_tool -change /usr/lib/libiconv.2.dylib "$PREFIX"/sysroot/usr/lib/libiconv.2.dylib $SHARED_LIB || true
            install_name_tool -change /usr/lib/libncurses.5.4.dylib "$PREFIX"/sysroot/usr/lib/libncurses.5.4.dylib $SHARED_LIB || true
            install_name_tool -change /usr/lib/libicucore.A.dylib "$PREFIX"/sysroot/usr/lib/libicucore.A.dylib $SHARED_LIB || true
            install_name_tool -change /usr/lib/libexpat.1.dylib "$PREFIX"/lib/libexpat.1.dylib $SHARED_LIB || true
            install_name_tool -change /usr/lib/libcurl.4.dylib "$PREFIX"/lib/libcurl.4.dylib $SHARED_LIB || true
            install_name_tool -change /usr/lib/libc++.1.dylib "$PREFIX"/lib/libc++.1.dylib $SHARED_LIB || true
            install_name_tool -change /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libc++.1.dylib "$PREFIX"/lib/libc++.1.dylib $SHARED_LIB || true
          done
        popd
      done
    popd
  fi
fi
zo#!/bin/bash
mkdir -p $PREFIX/lib/R/library/{cran_packagename}
mv * $PREFIX/lib/R/library/{cran_packagename}
fi
z9"%R%" CMD INSTALL --build .
IF %ERRORLEVEL% NEQ 0 exit 1
a  if "%target_platform%" == "win-64" goto skip_source_build
"%R%" CMD INSTALL --build .
IF %ERRORLEVEL% NEQ 0 exit 1
exit 0
:skip_source_build
mkdir %PREFIX%\lib\R\library
robocopy /E . "%PREFIX%\lib\R\library\{cran_packagename}"
if %ERRORLEVEL% NEQ 1 exit 1
exit 0
z
    - ZSiteZArchsDependsZEnhancesImportsLicenseLicense_is_FOSSLicense_restricts_use	LinkingToZMD5sumNeedsCompilationZOS_typePackageZPathZPrioritySuggestsVersionZTitleZAuthorZ
Maintainer)baseZcompilerZdatasetsZgraphicsZ	grDevicesZgridmethodsZparallelZsplinesZstatsZstats4ZtcltkZtoolsZutils)ZMASSZlatticeZMatrixZnlmeZsurvivalZbootZclusterZ	codetoolsZforeignZ
KernSmoothZrpartclassZnnetZspatialZmgcvz^\s*(?P<name>[a-zA-Z0-9.+\-]{1,})(\s*\(\s*(?P<relop>[>=<]+)\s*(?P<version>[0-9a-zA-Z:\-+~.]+)\s*\))?(\s*\[(?P<archs>[\s!\w\-]+)\])?\s*$z
=~ linux.*z== linux-32z== linux-64z	== win-32z	== win-64z	== osx-64)linuxZlinux32Zlinux64win32win64osxc             C   s   dS )NT )package_namer4   r4   9lib/python3.7/site-packages/conda_build/skeletons/cran.pypackage_exists   s    r7   c             C   sR  | j ddtjd}|jdddd |jdd	d
d |jdddd |jddd |jddd |jddd |jdddd |jddd |jdddd |jd d!d |jd"d#d$d%d& |jd'dd(d)d* |jd+dd,d |jd-dd.d |jd/d0d1d2d* |jd3d0d4d5d* |jd6dd7d |jd8d9d:d;d<d= |jd>d?td@d dAd d S )BNcranz
    Create recipe skeleton for packages hosted on the Comprehensive R Archive
    Network (CRAN) (cran.r-project.org).
        )helpZformatter_classpackages+z-CRAN packages to create recipe skeletons for.)nargsr9   z--output-dirz5Directory to write recipes to (default: %(default)s)..)r9   defaultz--output-suffixzLSuffix to add to recipe dir, can contain other dirs (eg: -feedstock/recipe). z--add-maintainerz@Add this github username as a maintainer if not already present.)r9   z	--versionz(Version to use. Applies to all packages.z	--git-tagz"Git tag to use for GitHub recipes.z
--all-urls
store_truez`Look at all URLs, not just source URLs. Use this if it can't
                find the right URL.)actionr9   z
--cran-urlz+URL to use for as source package repositoryz
--r-interpzr-basezDeclare R interpreter package)r>   r9   z--use-binaries-verzURepackage binaries from version provided by argument instead of building from source.z--use-when-no-binary)srcoldzsrc-oldzold-srcerrorrB   a  Sometimes binaries are not available at the correct version for
                a given platform (macOS). You can use this flag to specify what
                fallback to take, either compiling from source or using an older
                binary or trying one then the other.)choicesr>   r9   z--use-noarch-genericuse_noarch_genericz?Mark packages that do not need compilation as `noarch: generic`)rA   destr9   z--use-rtools-winz/Use Rtools when building from source on Windowsz--recursivez=Create recipes for dependencies if they do not already exist.z--no-recursiveZstore_false	recursivezCDon't create recipes for dependencies if they do not already exist.z--no-archivearchivez&Don't include an Archive download url.z--version-comparezCompare the package version of the recipe with the one available
        on CRAN. Exits 1 if a newer version is available and 0 otherwise.z--update-policyZstore)rD   zskip-up-to-datezskip-existing	overwritezmerge-keep-build-numzmerge-incr-build-numrD   a  Dictates what to do when existing packages are encountered in the
        output directory (set by --output-dir). In the present implementation, the
        merge options avoid overwriting bld.bat and build.sh and only manage copying
        across patches, and the `build/{number,script_env}` fields. When the version
        changes, both merge options reset `build/number` to 0. When the version does
        not change they either keep the old `build/number` or else increase it by one.)rA   rE   r>   r9   z-mz--variant-config-filesZskeleton_config_yamlz}Variant config file to add.  These yaml files can contain
        keys such as `cran_mirror`.  Only one can be provided here.)
add_parserargparseZArgumentDefaultsHelpFormatteradd_argumentr   get)Zreposr8   r4   r4   r6   rK   )  s    

rK   c          	   C   s~   i }xl| D ]d}|sq
y.d|kr0| dd\}}n| dd\}}W n" tk
rd   td|  Y nX |||< q
W | |d< |S )Nz:    :z$Error: Could not parse metadata (%s)
orig_lines)split
ValueErrorsysexit)linesdlinekvr4   r4   r6   dict_from_cran_lines  s    
r[   c             C   s   d}d}d}d}d}|  d xt| D ]\}}||rd|  }|rd|sRt||7 }d| |< qd}|d }| | | }d}d| |< q(|r(|st|| |< d}d}d}q(W |rdd	 | D } |  d | S )
aw  
    >>> chunk = [
        'Package: A3',
        'Version: 0.9.2',
        'Depends: R (>= 2.15.0), xtable, pbapply',
        'Suggests: randomForest, e1071',
        'Imports: MASS, R.methodsS3 (>= 1.5.2), R.oo (>= 1.15.8), R.utils (>=',
        '        1.27.1), matrixStats (>= 0.8.12), R.filesets (>= 2.3.0), ',
        '        sampleSelection, scatterplot3d, strucchange, systemfit',
        'License: GPL (>= 2)',
        'NeedsCompilation: no']
    >>> remove_package_line_continuations(chunk)
    ['Package: A3',
     'Version: 0.9.2',
     'Depends: R (>= 2.15.0), xtable, pbapply',
     'Suggests: randomForest, e1071',
     'Imports: MASS, R.methodsS3 (>= 1.5.2), R.oo (>= 1.15.8), R.utils (>= 1.27.1), matrixStats (>= 0.8.12), R.filesets (>= 2.3.0), sampleSelection, scatterplot3d, strucchange, systemfit, rgl,'
     'License: GPL (>= 2)',
     'NeedsCompilation: no']
    ) 	NFr?   r\   TrO   c             S   s   g | ]}|r|qS r4   r4   ).0cr4   r4   r6   
<listcomp>  s    z5remove_package_line_continuations.<locals>.<listcomp>)append	enumerate
startswithlstripAssertionError)chunkZcontinuationZcontinued_ixZcontinued_lineZhad_continuationZaccumulating_continuationsirX   r4   r4   r6   !remove_package_line_continuations  s8    




rh   c             C   s   t j| tdddddS )a  
    Quote a string for use in YAML.

    We can't just use yaml.dump because it adds ellipses to the end of the
    string, and it in general doesn't handle being placed inside an existing
    document very well.

    Note that this function is NOT general.
    )ZDumperz
...
r?   
z
  )yamldumpr   replace)stringr4   r4   r6   yaml_quote_string  s    
rn   c             C   sJ   g }d}x6|   D ]*}| }|dkr.|dks8|| |}qW d|S )Nr?   ri   )
splitlinesrstripra   r   )rm   rV   	last_linerX   r4   r4   r6   clear_whitespace
  s    
rr   c          
   C   s   | d | d }| |}y|  W nB tjjk
rh } z |jjdkrVtd|   W d d }~X Y nX |j	}t
t| }||d< |S )Nz/web/packages/z/DESCRIPTIONi  zERROR: %s (404 Not Found)orig_description)rN   raise_for_statusrequests
exceptionsZ	HTTPErrorresponseZstatus_coderT   rU   textr[   rh   ro   )cran_urlpackagesessionurlreDESCRIPTIONrW   r4   r4   r6   get_package_metadata  s    
r   c             C   s   t jddddddgt jt j| jd}| \}}|d}|d}|sN|jr\td	|  |	 
 }|svtd
 td|d   |d S )Ngitzfor-each-refz	refs/tagsz--sort=-committerdatez--format=%(refname:short)z	--count=1)stdoutstderrcwdzutf-8zError: git tag failed (%s)zError: no tags foundzUsing tag %s)
subprocessPopenPIPEwork_dircommunicatedecode
returncoderT   rU   stripro   print)configpr   r   Ztagsr4   r4   r6   get_latest_git_tag$  s     




r   c               C   s   t dd  dkS )zGets whether the SSL_NO_VERIFY environment variable is set to 1 or True.

    This provides a workaround for users in some corporate environments where
    MITM style proxies make it difficult to fetch data over HTTPS.
    ZSSL_NO_VERIFYr?   )1true)r	   rN   r   lowerr4   r4   r4   r6   _ssl_no_verify:  s    r   Tc             C   sf   t  }t |_ydd l}dd l}W n  tk
rD   |r@td Y nX |j||j	
t| dd}|S )Nr   zRTip: install CacheControl and lockfile (conda packages) to cache the CRAN metadataz
.web_cache)cache)ru   ZSessionr   ZverifycachecontrolZcachecontrol.cachesImportErrorr   ZCacheControlZcachesZ	FileCacher   )
output_dirverboser{   r   r4   r4   r6   get_sessionC  s    
r   c             C   s`   t ||d}|rtd|   || d }|  |j}dd |dD }dd tt|D S )	N)r   zFetching metadata from %sz/src/contrib/PACKAGESc             S   s   g | ]}t | qS r4   )rh   ro   )r^   rg   r4   r4   r6   r`   [  s   z%get_cran_metadata.<locals>.<listcomp>z

c             S   s   i | ]}||d    qS )r*   )r   )r^   rW   r4   r4   r6   
<dictcomp>]  s    z%get_cran_metadata.<locals>.<dictcomp>)r   r   rN   rt   rx   rR   mapr[   )ry   r   r   r{   r}   ZPACKAGESpackage_listr4   r4   r6   get_cran_metadataT  s    
r   Fc             C   sl   g }y|  |g }W n   g }Y nX |s.|rF||dd d  x |D ]}|djt|d qLW |S )N/r   rP   z{indent}{old_val})indentold_val)	get_valuera   rR   formatINDENT)mkeyZallow_emptyresultZold_valsr   r4   r4   r6   
make_arraya  s    

r   c             C   sx   d }t t| |r"tt| |}nRt t| || rHtt| || }n,t t| d| | rttt| d| | }|S )Nzr-)r   r   r   )r   output_suffixrz   r   r4   r4   r6   existing_recipe_dirn  s    r   c             C   s    |  |r| d t|  S | S )N)endswithlen)rm   endr4   r4   r6   	strip_endy  s    
r   c             C   s  t |rdS td|  t|d}t|t}d|kr>t|d}t| }t|d}|rbt||}|drx|dd }|d	r|d	d
}t|}t	d|
d }t| |d| }ntt|rt|| f}|| krtd|| f |}t| |d| }n0d|kr"|}t| |d| }nt| || }}|rZyt|}	W n   d}	Y nX nd}	|}
|r|	rd|kr|	dd
}d|kr|}|	dd}
t| d| | }td|||f  |||
|	|dS )a  
    Converts `package` (*) into a tuple of:

    pkg_name (without leading 'r-')
    location (in a subdir of output_dir - may not exist - or at GitHub)
    old_git_rev (from existing metadata, so corresponds to the *old* version)
    metadata or None (if a recipe does *not* already exist)

    (*) `package` could be:
    1. A package name beginning (or not) with 'r-'
    2. A GitHub URL
    3. A file:// URL to a tarball
    4. A relative path to a recipe from output_dir
    5. An absolute path to a recipe (fatal unless in the output_dir hierarchy)
    6. Any of the above ending (or not) in sep or '/'

    So this function cleans all that up:

    Some packages may be from GitHub but we'd like the user not to have to worry
    about that on the command-line (for pre-existing recipes). Also, we may want
    to get version information from them (or existing metadata to merge) so lets
    load *all* existing recipes (later we will add or replace this metadata with
    any that we create).
    NzParsing input package %s:r   z
github.comz.gitz
-feedstockzr-   zfile://r?   z	(.*)_(.*)rO   z;package %s specified with abs path outside of output-dir %szsource/git_urlzsource/git_revz).. name: %s location: %s new_location: %s)zpkg-namelocationzold-git-revzold-metadataznew-location)r   r   r   r   r
   r   rc   rl   rematchgroupr   r   r   RuntimeErrorr   MetaDatar   r   )r   r   git_tagrz   pkg_namer   Zpkg_filenameZexisting_locationZcommpr   old_git_revgit_urlZnew_locationr4   r4   r6   package_to_inputs_dict  s`    









r   c                s   dd l ddlm  di f fdd	}| d |d  }|d }|||}xr|D ]j}t|}|d	\}}	}
|
|\}
}	}	||d
 kr|d
 |  |
|f qP|
|fg|d
 | < qPW d S )Nr   )BeautifulSoupr?   c                sL   j |d}|jr|j}n| S |d} fdd|dD }|S )N)paramszhtml.parserc                s*   g | ]"}| d  r| d  qS )Zhref)rN   r   )r^   Znode)extr|   r4   r6   r`     s    zAget_available_binaries.<locals>.get_url_paths.<locals>.<listcomp>a)rN   okrx   rt   Zfind_all)r|   r   r   rw   Zresponse_textZsoupparent)r   ru   )r   r|   r6   get_url_paths  s    
z-get_available_binaries.<locals>.get_url_pathsr   dirr   _binaries)ru   Zbs4r   r
   
rpartitionr   extend)ry   Zdetailsr   r|   r   r   r   filenamepkgr   verr4   )r   ru   r6   get_available_binaries  s    


r   r=   r?   r-baserB   c       e      C   s  |dkr8|dkr8|dkr8|dkr8t d| td t|}t||d}|st }t||d }W d Q R X t|	d	t
d	 d }t| dkr|	rtd
|
dkr| stdi }g }|d}t||}ddddddd|d|rdndddd|d|rdnddd}x>t|D ]2\}}t |d< |dkr"|d r"t|| q"W x8| D ]0}t||||}|r^||d d|ii q^W x | D ]\}}|| qW x|r~||  d }|d }|d } |od |k}!|ot|ot|}"|d }#|d! }$t d"|  |!s0|"rt|j |!rtjjdd#|ii|d$}%t |%!d|%j"j#|%j"j |r~|nt$|}&t%j&d%d&|&gt%j't%j'|jd'}'|'( \}(})|()d(}(|))d(})|'j*rtd)|&|)+ f  |(rt |(tj,d* |)rHt |)tj-d* n<tjjdd+|ii|d$}%t.|%!d|%j"j|%j"j/||%j"j t0|jd,}*t|*st0|jd-d,}+t0|j|1dd. d,},t|+r|+}*n$t|,r|,}*ntd/||+|,f  t2|*}-t3|-4 }.W d Q R X t5t6|.7 }/|.|/d0< |/d1 8 }|/||< n| }| |kr"td2|   ||8  d1 }|!s`|"s`t9|}0||8  t:|||0 ||8  }1||8  |d3|8  d4dd4d4d4d5d4d5d4d6 ||8  }/d4|/d7< d4|/d8< |r||/d9< t;d:|1d; |/d<< |/d< <d=d>|/d?< |	rt|	|$|/d?   g }2g }3g }4d}5|
=d@r|dA r|dA }%t>|%dB}2t>|%dC}3t>|%dD|}4|%? |/d? krt@|%AdEd}5|5|
dFkrdnd7 }5|rdGjtB|dH}6|6|4krt|4st>i dDd}4|4|6 t|4r|4dd  C  |4DddI d40|4|/dJ< d40|2|/dK< d40|3|/dL< |5|/dM< d }7tEF|}8i }9xPt|8D ]B\}}d4}:|/d< |d<< |/d? |d?< |dkrzdn$||d ko|/d< |d | d k};|;sV|dkrt dN td n|=drV||d kr|Gdrd};d|d< qFnt dO td |d | d. d |dP< |d | d. d |d?< |d? <d>d=|d<< d};d }7tHI }<|d rF|;rF|"rtJ|}=tK||$}:|: }>}?|}7n|!r|dkrTdQ||d< |dR }@dS||dR  }=dT|dU }:|dV|dU  }>|>|@ }?t dW||? y&tL|j/d4|?|d= |@ dX\}7}AW n$   t dY|? td Y nX i }B|dZ |BdZ< |d< |Bd<< |d? |Bd?< |7r|<t2|7d[4  |?|dP< |=|Bd\< |:|Bd]< |>|Bd^< d_|<M |Bd`< |7|Bda< |dk	rh|!	r.d4|Bdb< dc|Bdd< de|Bdf< dg}C|C|Bd`< d4|Bd\< d4|BdP< |#|Bd#< |&|Bdh< d4|Bdi< n8dj|Bdb< d4|Bdd< d4|Bdf< dk|: |= |BdP< d4|Bd#< d4|Bdh< n|dP |BdP< |/dK |BdK< |B|9|< qFW dldmddg}D|Dd d  }Ed}Fx\t|9D ]P\}}|dk	rx:t|D ]\}G}H|H|/|G< 	qW n|dZ }I|EN|I |Fd7 }F	qW |E|Dk
r d4}Jdn}Kdo}LnJdpdq0|E dr }Jdpdq0dsdt |ED  dr }Kdpdq0dudt |ED  dr }L|J|/dv< |K|/dw< |L|/dx< |E|/dy< d|9kr2|9d }B|J|Bdz< |Bd\ }=d]|Bkr|Bd] }:|r|"
rtB|: |BdP< n.tB|: |= |J tB |: d{| |= |J |BdP< ndk|: |= |J |BdP< |!s2d|jf |B|Bdi< d}0d~d |1d D |/d< d}Fd4 |/d< |/d< xt|9D ]\}}|dkrtOjf ||/d< tPjf ||/d< nLdp|dZ  dr |dz< tQjf ||/dtR|F < tPjf ||/dtR|F < |Fd7 }FqlW |1	dd|/d< tS|/d tT|/d< tU|/d |/d< d|1kr>|/d  d7  < |1	ddkr^|/d  d7  < d|1krd4|/d< dktV|1d  |/d< n,d4|/d< |!rd||/d< nd||/d< |r|1	dddkrd4|/d< nd|/d< d|1krd4|/d< dktV|1d  |/d< d|1krd|1d  |/d< nd4|/d< dd |1	dd41dD }Mdd |1	dd41dD }Ndd |1	dd41dD }Oi }PtW }QxtXtY|N|M|OD ]}RtZ[|R}S|Sstd||Rf  |S\d}T|T|QkrԐq|Q]|T |S\d}U|S\dpd4}V|S\d9pd4}W|W<d=d>}W|Vr$|Ws$t^|Ur<td||Rf  dj|V|Wd|P|T< qW d|Pkrfd4|Pd< |!}X|1	dddkrt2|9d da }Yt_dd |YD }Z|Zrdnt_dd |YD }[t_dd |YD }\t_dd |YD }]t_|]|Z|\|[frdnt_dd |YD }^W d Q R X nd }[ }\ }Z }]}^d|Pks>d|PkrBd}\|\rLd}[x*dD ] }_g }`|_dkr"|[r|`djtB|Ld |\r|`djtB|Ld |Zr|`djtB|Ld |rd }[ }\ }Z }]}^|`djtB|Kd |[s|\s|Zr
|`djtB|Kd |]s|^s|Xr0|`djtB|Kd |XrH|`djtBd |]r|`djtB|Kd |`djtB|Kd |`djtB|Jd |`djtB|Ld |`djtB|Kd |`djtBd |^r|`djtB|Jd |]s|`djtB|Kd |`djtB|Kd |`djtB|Kd n0|_dkrR|[s>|\s>|ZrR|`djtB|Kd |_dksf|_dkrbxt`|PD ]}T|Ttakrqp|Tdkr|`DddjtB|d΍ nd3|T8  }a|P|T r|`dj|a|P|T tBdЍ n|`dj|atBdҍ |rp|T8 }b|b|krpt||||b}|b|d ks2t^d|T|d f |b|ks@t^||bd|ii ||b qpW d40|`|/d|_ < qTW qW x8|D ].}|| }/|/d d! }$tb|$r|	s|
dkrtcd|$ n|
dkrt|$ n@|
dkrtd||/d dA rqn|
dkr|/d dA rq|/dy }cddڄ t|/D }/ytet0|$ W n   Y nX t d|8   t2t0|$d܃d݃}-|-ft3tgjf |/ W d Q R X tbt0|$dރr|
dkrBt2t0|$dރd݃z}-|cthkr|-ftijf |/ nV|cg kr |-ftjjf |/ n8dd |cD }dd0dd |dD |/d< |-ftkjf |/ W d Q R X tbt0|$dr\|
dkrt2t0|$dd݃D}-tdd |cD dkr|-ftljf |/ n|-ftmjf |/ W d Q R X qW d S )NrD   rB   rC   zold-srcz2ERROR: --use_when_no_binary={} not yet implementedrO   )variant_config_filesr   Zcran_mirrorz7--version-compare only works with one package at a timez%At least one package must be suppliedr   z{others}zsrc/contrib/z.tar.gzT)selectorr   r   use_thisr2   zbin/windows/contrib/{}/z.zipFr3   z!bin/macosx/el-capitan/contrib/{}/z.tgz)r   zwin-64zosx-64r   r   r   zpkg-nameinputsr   z
github.comznew-locationzMaking/refreshing recipe for {}r   )r   r   Zcheckout)r   r   r   zutf-8z2Error: 'git checkout %s' failed (%s).
Invalid tag?)filer|   r   r   r   zJ%s does not appear to be a valid R package (no DESCRIPTION file in %s, %s)rs   r*   zPackage %s not foundzr-r?   #)Zcran_packagenameZpackagenamepatchesbuild_numberZbuild_dependsZhost_dependsZrun_dependshome_commenthomeurlsummary_commentsummaryZbinary1Zbinary2versionz2Package versions from CRAN are not yet implementedr,   Zcran_version-r   Zconda_versionmergezold-metadatazsource/patcheszbuild/script_envzextra/recipe-maintainerszbuild/numberzmerge-incr-build-numz{indent}{add_maintainer})r   add_maintainerz	extra:
  extra_recipe_maintainersr   
script_envr   z=ERROR: --use-when-no-binary is error (and there is no binary)zfERROR: No binary nor old binary found (maybe pass --use-when-no-binary=old-src to fallback to source?)Zcranurlz{}_{}{}r   z{}_{{{{ version }}}}z{{{{ cran_mirror }}}}/{}r   z/{}zDownloading {} from {})r|   fnzHlogic error, file {} should exist, we found it in a dir listing earlier.r   rbr   contrib_urlcontrib_url_renderedz
sha256: {}Z
hash_entrycached_pathZurl_keyzgit_url:Zgit_url_keyzgit_tag:Zgit_tag_keyz=# You can add a hash for the file here, (md5, sha1 or sha256)r   Zarchive_keyszurl:r\   r0   r1   z	  # [win]z  # [not win]z  # [z or ]c             s   s   | ]}| d r|V  qdS )winN)rc   )r^   fsr4   r4   r6   	<genexpr>  s    zskeletonize.<locals>.<genexpr>c             s   s   | ]}| d s|V  qdS )r   N)rc   )r^   r   r4   r4   r6   r     s    sel_srcsel_src_and_winsel_src_not_winfrom_sourceselzArchive/{}/z/{url_key}{sel}    {cranurl}
  {hash_entry}{sel}ri   c             S   s   g | ]}|rd | qS )z# %sr4   )r^   lr4   r4   r6   r`   :  s    zskeletonize.<locals>.<listcomp>rQ   cran_metadataZversion_binary1Zversion_binary2Zversion_sourceZbinaryZversion_binaryr%   NonelicenseZlicense_familylicense_filer&   z (FOSS)r'   Zyesz (Restricts use)ZURLr   r   z {}z& https://CRAN.R-project.org/package={}r)   ZnoZnoarch_genericznoarch: genericZDescriptionr   r   r+   z# Suggests: %sZsuggestsc             S   s   g | ]}|  r|  qS r4   )r   )r^   sr4   r4   r6   r`   o  s    r#   ,c             S   s   g | ]}|  r|  qS r4   )r   )r^   r   r4   r4   r6   r`   q  s    r$   c             S   s   g | ]}|  r|  qS r4   )r   )r^   r   r4   r4   r6   r`   s  s    r(   z1Could not parse version from dependency of %s: %snamearchsrelopz@Don't know how to handle archs from dependency of package %s: %sz{relop}{version})r   r   Rc             S   s   g | ]}|j  d qS ))z.fz.f90z.f77)r   r   r   )r^   fr4   r4   r6   r`     s    c             S   s   g | ]}|j  d qS )z.c)r   r   r   )r^   r   r4   r4   r6   r`     s    c             S   s   g | ]}|j  d qS ))z.cxxz.cppz.ccz.c++)r   r   r   )r^   r   r4   r4   r6   r`     s   c             S   s   g | ]}|j  d qS )z
/configure)r   r   r   )r^   r   r4   r4   r6   r`     s    c             S   s   g | ]}|j  d qS ))z	/makefilez	/makevars)r   r   r   )r^   r   r4   r4   r6   r`     s   ZRcppZRcppArmadillo)buildhostrunr   z*{indent}{{{{ compiler('c') }}}}      {sel})r   r   z*{indent}{{{{ compiler('cxx') }}}}    {sel}z*{indent}{{{{ compiler('fortran') }}}}{sel}z&{indent}rtools                   {sel}z*{indent}{{{{native}}}}toolchain      {sel}z*{indent}{{{{posix}}}}filesystem      {sel}z{indent}{{{{posix}}}}git)r   z*{indent}{{{{posix}}}}sed             {sel}z*{indent}{{{{posix}}}}grep            {sel}z*{indent}{{{{posix}}}}autoconf        {sel}z*{indent}{{{{posix}}}}automake        {sel}z*{indent}{{{{posix}}}}automake-wrapper{sel}z{indent}{{{{posix}}}}pkg-configz*{indent}{{{{posix}}}}make            {sel}z*{indent}{{{{posix}}}}coreutils       {sel}z*{indent}{{{{posix}}}}zip             {sel}r   z*{indent}{{{{native}}}}gcc-libs       {sel}r   z{indent}{r_name})r   Zr_namez{indent}{name} {version})r   r   r   z{indent}{name})r   r   z%name %s != inputs_dict['pkg-name'] %sz
%s_dependsz=directory already exists (and --update-policy is 'error'): %srJ   zskip-up-to-datezskip-existingc             S   s.   i | ]&\}}t d t|dd |qS )ZNFKDasciiignore)unicodedataZ	normalizer   encoder   )r^   rY   rZ   r4   r4   r6   r     s   zskeletonize.<locals>.<dictcomp>zWriting recipe for %sz	meta.yamlwzbuild.shc             S   s   g | ]}t | qS r4   ) target_platform_bash_test_by_sel)r^   tr4   r4   r6   r`   )  s    z || c             S   s   g | ]}d | d qS )z[[ $target_platform z ]]r4   )r^   r   r4   r4   r6   r`   *  s   Zsource_pf_bashzbld.batc             S   s   g | ]}| d r|qS )r   )rc   )r^   r   r4   r4   r6   r`   0  s    r   )nr   r   rT   rU   r   r   r   r!   r    rN   r"   r   rS   rp   r   r   dictr   r   updateitemsra   popr   tarfile
is_tarfiler   r   r   r   Zfromdictr   Z
git_sourceZget_sectionr   Z	git_cacher   r   r   r   r   r   r   r   r   r   ZunpackZ	src_cacher   rR   openrr   readr[   rh   ro   r   r   r   NotImplementedErrorrl   rc   r   r   intr   r   sortinsertcopydeepcopyr   hashlibsha256r
   r   Zdownload_to_cacheZ	hexdigestremoveSOURCE_METAVERSION_METABINARY_METAstrr   r   get_license_filern   setlistr   VERSION_DEPENDENCY_REGEXr   r   addre   anysortedR_BASE_PACKAGE_NAMESr   r   
up_to_dater   write	CRAN_METAallCRAN_BUILD_SH_SOURCECRAN_BUILD_SH_BINARYCRAN_BUILD_SH_MIXEDCRAN_BLD_BAT_SOURCECRAN_BLD_BAT_MIXED)eZin_packagesr   r   r   r   r   ry   rH   rI   version_compareZupdate_policyZr_interpZuse_binaries_verrF   Zuse_when_no_binaryZuse_rtools_winr   r   r  Z_variantZpackage_dictsr   r   Zcran_layout_templateZarchive_typeZarchive_detailsrz   Zinputs_dictr5   Zpackage_dictr   r   r   Zis_github_urlr  r|   Zdir_pathr   Znew_git_tagr   r   r   r   Zsub_description_pkgZsub_description_namer   Zdescription_textrW   r{   Zcran_packager   r   r   r   Znew_maintainerr   Zcran_layoutZ	availabler   Zavaliable_artefactr  r   r   Zpackage_urlZfilename_renderedr   Zavailable_detailsZhash_msgZ_allr   Z	binary_idrY   rZ   r   r   r   r   ZdependsZimportsZlinksZdep_dictseenr   r   r   r   r   r   Zneed_gitZtfZneed_fZneed_cZneed_cxxZneed_autotoolsZ	need_makeZdep_typeZdepsZ
conda_nameZ
lower_nameZfrom_sourcesZtpbtr4   r4   r6   skeletonize  s   



































0





























 


r+  c             C   sB   t | }| }t| }td||f  td||f  ||kS )Nz#Local recipe for %s has version %s.z!The version on CRAN for %s is %s.)r   r   r   r
   r   )Z
recipe_dirZnewest_conda_versionr   Zlocal_versionrz   r4   r4   r6   r)  6  s    
r)  r4   c             C   s   g }t | }x|D ]}|drt|s*q|dd  }|rL||ksL||ksLq||krbtd|  qtt| ||| d dd td|  || qW |S )Nzr-r   zSkipping %s, not found on CRANr,   r   r   zUpdating %s)r   rc   r   r   r)  r   rl   ra   )r   r   r:   Z	to_updaterecipesreciperecipe_namer4   r4   r6   get_outdatedB  s     

r/  c             C   s^   g }t | }xL|D ]D}|drt|s*q|dd  }|rL||ksL||ksLq|| qW |S )Nzr-r   )r   rc   r   ra   )r   r   r:   existingr,  r-  r.  r4   r4   r6   get_existing[  s    
r1  c             C   s\   |\}}}}|dd  }|s dS d|kr,dS || kr8dS |  | | d ddkrXdS dS )Nr   Fz
github.comr,   r   r   T)r   rl   )r   rz   Z
r_pkg_namer   r   r   Zcran_pkg_namer4   r4   r6   r   m  s    r   c          
   C   s   ddddgddgddd	gd
ddddgddgddgddddgd}d}x6|  D ]&}| || krN|j|| d d}P qNW d}|S )a  
    Most R packages on CRAN do not include a license file. Instead, to avoid
    duplication, R base ships with common software licenses:

    complete: AGPL-3, Artistic-2.0, GPL-2, GPL-3, LGPL-2, LGPL-2.1, LGPL-3
    template: BSD_2_clause BSD_3_clause, MIT

    The complete licenses can be included in conda binaries by pointing to the
    license file shipped with R base. The template files are more complicated
    because they would need to be combined with the license information provided
    by the package authors (e.g. copyright owners and date).

    This function returns the path to the license file for the unambiguous
    cases. Any time an R package refers to one of the templates or a custom
    license file (e.g. 'GPL-2 | file LICENSE'), an empty string is returned.
    zAGPL-3zAGPL (>= 3)ZAGPLz!GNU Affero General Public LicensezArtistic-2.0zArtistic License 2.0zGPL-2z
GPL (>= 2)z!GNU General Public License (>= 2)zGPL-3z
GPL (>= 3)z!GNU General Public License (>= 3)ZGPLzGNU General Public LicensezLGPL-2zLGPL (>= 2)zLGPL-2.1zLGPL (>= 2.1)zLGPL-3zLGPL (>= 3)ZLGPLz!GNU Lesser General Public License)Zagpl3Z	artistic2Zgpl2Zgpl3Zlgpl2Zlgpl21Zlgpl3zMlicense_file: '{{{{ environ["PREFIX"] }}}}/lib/R/share/licenses/{license_id}'r   )
license_idr?   )keysr   )Zlicense_textZ	d_licenseZlicense_file_templater2  r   r4   r4   r6   r    s"    
r  )T)T)F)r=   r?   NNNNFTFr?   r   NFrB   FNN)r4   )r4   )]__doc__Z
__future__r   r   r   rL   r  	itertoolsr   osr   r   r   r	   os.pathr
   r   r   r   r   r   r   r   r   r   r   r   rT   r  ru   r  r   rj   r   r   r   Zconda_buildr   r   Zconda_build.configr   Zconda_build.conda_interfacer   r   r   r   Zconda_build.license_familyr   r   Zconda_build.utilsr   r    Zconda_build.variantsr!   r"   r  r  r  r"  r$  r&  r%  r'  r(  r   Z	CRAN_KEYSr  ZR_RECOMMENDED_PACKAGE_NAMEScompiler  r  r7   rK   r[   rh   rn   rr   r   r   r   r   r   r   r   r   r   r   r+  r)  r/  r1  r   r  r4   r4   r4   r6   <module>   s   0C)	 ;	


S    
    I

