#!/usr/bin/env python

"""
Creates a yaml file with all URL that needs to be downloaded, given a file with a list of all meta.yaml file.

Usage:

./get_urls.py meta_files.list

"""

import os
import sys
import yaml

# needs to be before the conda_build import

platform = sys.argv[2]

import conda.config as cc
cc.subdir = platform

from conda_build.metadata import MetaData


res = list()
for meta_path in open(sys.argv[1]):
    input_dir = os.path.join( './bioconda-recipes', os.path.dirname(meta_path) )
    if os.path.exists(input_dir):

        for arch in ['osx-', 'linux-64']:
            package = dict()
            package['arch'] = arch
            # set the architechture before parsing the metadata
            cc.subdir = arch

            recipe_meta = MetaData(input_dir)
            package['name'] = recipe_meta.get_value('package/name')
            package['version'] = recipe_meta.get_value('package/version')
            url = recipe_meta.get_value('source/url')
            if url:
                package['sha256'] = recipe_meta.get_value('source/sha256')
                package['md5'] = recipe_meta.get_value('source/md5')
            else:
                # git_url and hopefully git_rev
                git_url = recipe_meta.get_value('source/git_url')
                git_rev = recipe_meta.get_value('source/git_rev')
                url = '%s/%s.tar.gz' % (git_url.rstrip('.git'), git_rev)
                if not git_rev:
                    sys.stderr.write('git revision is missing for: %s\n' % input_dir)
                    continue
            package['url'] = url
            res.append(package)

with open('data_%s.yml' % (platform), 'w') as outfile:
    yaml.safe_dump(res, outfile, default_flow_style=False)

@@ -539,54 +539,8 @@
 ):%0A%0A
-        for arch in %5B'osx-', 'linux-64'%5D:%0A    
     
@@ -560,20 +560,16 @@
  dict()%0A
-    
         
@@ -590,17 +590,17 @@
 %5D = 
-arch%0A    
+platform%0A
     
@@ -663,20 +663,16 @@
         
-    
 cc.subdi
@@ -679,18 +679,18 @@
 r = 
-arch%0A%0A    
+platform%0A%0A
     
@@ -727,36 +727,32 @@
 ut_dir)%0A        
-  
-  
 package%5B'name'%5D 
@@ -791,20 +791,16 @@
 /name')%0A
-    
         
@@ -865,28 +865,24 @@
 n')%0A        
-   
- 
 url = recipe
@@ -923,20 +923,16 @@
     
-    
 if url:%0A
     
@@ -927,20 +927,16 @@
 if url:%0A
-    
         
@@ -1002,36 +1002,32 @@
 6')%0A            
-    
 package%5B'md5'%5D =
@@ -1071,20 +1071,16 @@
         
-    
 else:%0A  
@@ -1089,20 +1089,16 @@
         
- 
-   
 # git_ur
@@ -1121,20 +1121,16 @@
 git_rev%0A
-    
         
@@ -1191,28 +1191,24 @@
             
-  
-  
 git_rev = re
@@ -1245,20 +1245,16 @@
 t_rev')%0A
-    
         
@@ -1326,20 +1326,16 @@
         
-   
- 
 if not g
@@ -1342,20 +1342,16 @@
 it_rev:%0A
-    
         
@@ -1440,20 +1440,16 @@
         
-    
 continue
@@ -1449,20 +1449,16 @@
 ontinue%0A
-    
         
@@ -1478,20 +1478,16 @@
 %5D = url%0A
-    
         

De-duplication data_*.yml from conda