B
    XM\C/                 @   s   d Z ddlZddlZddlZddlmZ ddlZddlmZ dZ	dZ
dZdZd	Zd
Zdd Zdd Zdd Zdd Zdd Zdd ZdddZdS )z:
Tools for converting luarocks packages to conda recipes.
    N)glob)platformz
    - a  
local ok,cjson = pcall(require, "cjson")
if not ok then
   print("ERROR: lua-cjson not installed. Use conda to install luarocks, "
         "then run 'luarocks install lua-cjson'.")
   os.exit()
end

local rockspecFile = "%s"
local origPackage = package
local ok, _ = pcall(dofile, rockspecFile)
if not ok then
   print("ERROR: could not load rockspecFile " .. tostring(rockspecFile))
   os.exit()
end

-- Resolve name clash
if origPackage == package then
   package = nil
end
local out = {
   rockspec_format=rockspec_format,
   package=package,
   version=version,
   description=description,
   supported_platforms=supported_platforms,
   dependencies=dependencies,
   external_dependencies=external_dependencies,
   source=source,
   build=build,
   modules=modules,
}
print(cjson.encode(out))
a  package:
  name: {packagename}
  version: "{version}"

source:
  {usefile}fn: {filename}
  {usefile}url: {url}
  {usegit}git_url: {url}
  {usegittag}git_tag: {gittag} # can also be a branch, but that is highly discouraged
  {usegitrev}git_rev: {gitrev} # prefer tags over commits, commits over branches
  {usemd5}md5:{md5}
#  patches:
   # List any patch files here
   # - fix.patch

build:
  {noarch_python_comment}noarch: generic
  # Useful to leave this on by default, will allow relocating
  # packages that have hard-coded paths in them
  detect_binary_files_with_prefix: true
  # If this is a new build for the same version, increment the build
  # number. If you do not include this key, it defaults to 0.
  # number: 1

requirements:
  build:{build_depends}

  run:{run_depends}

{test_comment}test:
  {entry_comment}commands:
    # You can put test commands to be run here.  Use this to test that the
    # entry points work.
{test_commands}

  # You can also put a file called run_test.lua in the recipe that will be run
  # at test time.

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

# See
# http://docs.continuum.io/conda/build.html for
# more information about meta.yaml
av  #!/bin/bash

# Make sure luarocks can see all local dependencies
$PREFIX/bin/luarocks-admin make_manifest --local-tree

# Install
# Rocks aren't located in a standard location, although
# they tend to be top-level or in a rocks/ directory.
# NOTE: we're just picking the first rock we find. If there's
# more than one, specify it manually.
ROCK=$(find . -name "*.rockspec" | sort -n -r | head -n 1)
$PREFIX/bin/luarocks install "$ROCK" --local-tree

# 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.
zm# Let luarocks know that we've installed a new project
$PREFIX/bin/luarocks-admin make_manifest --local-tree
zQ# Tell luarocks we've removed the project
$PREFIX/bin/luarocks remove {rockname}
c             C   sP   | j ddd}|jdddd |jdd	d
d |jddd |jdddd d S )NluarockszI
    Create recipe skeleton for luarocks, hosted at luarocks.org
        )helppackages+z1luarocks packages to create recipe skeletons for.)nargsr   z--output-dirz5Directory to write recipes to (default: %(default)s)..)r   defaultz	--versionz(Version to use. Applies to all packages.z--recursive
store_truez=Create recipes for dependencies if they do not already exist.)actionr   )
add_parseradd_argument)Zreposr    r   =lib/python3.7/site-packages/conda_build/skeletons/luarocks.pyr      s$    r   c             C   s   dS )NTr   )Zpackage_namer   r   r   package_exists   s    r   c             C   s"   || krt d| n| | S d S )NzRequired key %s not in spec)	Exception)speckr   r   r   getval   s    r   c             C   sX   t d t d t d t d t d|   t d t d t d t d t d d S )N z)=========================================zWARNING:z(Building a rock referenced to branch %s.z8This is not a tag. This is dangerous, because rebuildingz0at a later date may produce a different package.z2Please replace with a tag, git commit, or tarball.)print)branchr   r   r   warn_against_branches   s    r   c             C   s   d dd | D }|dkr4| d d dkr4d|  } | dd } dd | D }x>t|D ]2\}}|r\d	d | D }||d d |} P q\W | S )
Nr   c             S   s   g | ]}|  r|qS r   )isalpha).0cr   r   r   
<listcomp>   s    zformat_dep.<locals>.<listcomp>)lua   zlua- c             S   s   g | ]}|d kqS )z<>=~r   )r   r   r   r   r   r      s    c             S   s   g | ]}|qS r   r   )r   r   r   r   r   r      s    )joinreplacelower	enumerateinsert)depZname_without_verZspecial_char_testivZ	split_depr   r   r   
format_dep   s    
r)   c             C   sJ   dd | D }g }d|kr$| d d|kr6| d t|rF||  } | S )Nc             S   s    g | ]}d  dd |D qS )r   c             S   s   g | ]}|  r|qS r   )r   )r   r   r   r   r   r      s    z/ensure_base_deps.<locals>.<listcomp>.<listcomp>)r!   )r   r&   r   r   r   r      s    z$ensure_base_deps.<locals>.<listcomp>r   r   )appendlen)depsZ	basenamesZ
extra_depsr   r   r   ensure_base_deps   s    

r-   r	   Fc             C   s\  t dkrdnt }t }t }i }t| x| rH|  }|d d dkr\d|  n| }	|||	dddddddddddddddddddd	ddd
}
t	
dd|dg}|dkrtd|t|d }t|dkrtd|d |
d< t	jddt|
d  gt	jd}| \}}d|kr8t|ddt|}t|d|
d< t|d|
d< ddd |
d D |
d< t|d}t|d|
d< tj|
d d  }|d!kr d|
d"< tj|
d d  |
d#< d$|krt|d$}t|r||
d$< d|
d%< n|d&ks |
d d d d'krd|
d(< d)|krJd|
d*< t|d)|
d+< nJd,|krxd|
d*< t|d,|
d+< t|
d+  nd|
d*< d-|
d+< t|
d+  d.|krt|d.}d/|kr|d/ |
d0< d|
d1< d2|kr|d2 |
d2< d|
d3< d4|kr|d4 |
d4< d5|kr2t|d5}t|r2td6d |D }tdg| |
d7< |
d7 |
d8< q2W d9|kr|d:krdd;}n|dkrrd<}d }d=|d9 kr|d9 d= }nJd>|d9 kr||d9 d> krd=|d9 d> | kr|d9 d> | d= }|rtdgd?d | D  |
d@< |
d@ dkr0tdgdA|
d  g |
d@< t| x|D ]}|| }
|
dB }ttj|| t dC| tj||f  t!tj||dDdE}|"t#jf |
 W d Q R X t!tj||dFdE}|"t$jf |
 W d Q R X t!tj||dGdE}|"t% W d Q R X t!tj||dHdE}|"t&jf |
 W d Q R X qBW d S )INZlinux2Zlinuxr   zlua-zlua-%sz0.0r   z# ZUnknown)packagenameversionfilenameurlmd5usemd5usefileusegit	usegittagZ	usegitrevgittagZgitrevZnoarch_python_commentbuild_dependsrun_dependsZtest_commentZentry_commenttest_commandshome_commenthomeurllicensesummary_commentsummaryr   Zdownloadz
--rockspecr   z"Could not download rockspec for {}z
*.rockspec   zFailed to download rockspecZrockspec_filer   z-e)stdoutZERRORzERROR: packageZrocknamer/   c             S   s   g | ]}|  r|qS r   )isalnum)r   r   r   r   r   r   &  s    zskeletonize.<locals>.<listcomp>sourcer1   )z.zipz.tarz.tar.bz2z.tar.xzz.tar.gzr4   r0   r2   r3   )z.gitzgit:r5   tagr6   r7   r   ZmasterZdescriptionZhomepager<   r;   r?   r>   r=   Zdependenciesc             S   s   g | ]}t |qS r   )r)   )r   r&   r   r   r   r   S  s    r8   r9   ZbuilddarwinZmacosxZunixmodulesZ	platformsc             S   s   g | ]}d | qS )zlua -e "require '%s'"r   )r   rr   r   r   r   g  s   r:   zlua -e "require '%s'" r.   zWriting recipe for %s to %sz	meta.yamlwzbuild.shzpost-link.shzpre-unlink.sh)'	_platformosgetcwdtempfileZmkdtempchdirpopr#   
setdefault
subprocessZcallr   formatr   r+   Popenrockspec_parserPIPEZcommunicater"   jsonloadsr   r!   pathsplitextsplitr   r-   INDENTkeysmakedirsr   openwriteLUAROCKS_METALUAROCKS_BUILD_SHLUAROCKS_POSTLINK_SHLUAROCKS_PREUNLINK_SH)r   Z
output_dirr/   	recursiver   cwdZtemp_dirZpackage_dictsrB   r.   doZfspouterrr   rD   Zextr2   Zdescr,   Zour_platrH   namefr   r   r   skeletonize   s    	

$









 











rn   )r	   NF)__doc__rL   rR   rN   r   rW   sysr   rK   r\   rU   ra   rb   rc   rd   r   r   r   r   r)   r-   rn   r   r   r   r   <module>   s&   #2