'''
Generate the downloads.yaml file with a list of the contents of the code.playcanvas.com root containing all builds of the engine
'''
import sys
import os
import re
from distutils.version import StrictVersion

def get_file_list(path):
    '''generate a list of all builds of the engine'''
    
    files = []
    for file in os.listdir(path):
        if file.endswith(".js"):
            files.append(file)
    
    return files

def run(path, output):
    results = []
    
    files = get_file_list(path)
    
    r = re.compile("playcanvas-(.+?)(.min)?.js")
    
    for file in files:
        m = r.match(file)
        if m:
            version = m.group(1)
            if version in ['stable', 'latest']:
                continue
            results.append({
                "version": version,
                "url": file
            })
    
    results.sort(key = lambda x: StrictVersion(x['version']), reverse=True)
    
    write(results, output)

def write_entry(f, version, url):
    s = """  - version: %s\n    url: http://code.playcanvas.com/%s\n"""
    
    f.write(s % (version, url))
        
def write(results, output):
    f = open(output, "w")
    
    s = """  - version: %s
    url: http://code.playcanvas.com/%s"""
    
    f.write("latest:\n")
    write_entry(f, "latest", "playcanvas-latest.js")
    write_entry(f, "latest", "playcanvas-latest.min.js")
    
    f.write("current:\n")
    write_entry(f, results[0]['version'], results[0]['url'])
    write_entry(f, results[1]['version'], results[1]['url'])
    
    f.write("versioned:\n")
    for result in results[2:]:
        write_entry(f, result['version'], result['url'])

    f.close()
    
path = "/srv/www/code.playcanvas.com/public_html"
output = "downloads.yaml"

if len(sys.argv) > 1:
    path = sys.argv[1]
    
if len(sys.argv) > 2:
    output = sys.argv[2]

run(path, output)
@@ -1374,24 +1374,53 @@
 n.js%22)%0A    %0A
+    if len(results) %3E 1:%0A    
     f.write(
@@ -1429,24 +1429,28 @@
 urrent:%5Cn%22)%0A
+    
     write_en
@@ -1494,24 +1494,28 @@
 %5B0%5D%5B'url'%5D)%0A
+    
     write_en
@@ -1564,24 +1564,28 @@
 url'%5D)%0A    %0A
+    
     f.write(
@@ -1600,24 +1600,28 @@
 ed:%5Cn%22)%0A    
+  
+  
 for result i
@@ -1635,16 +1635,20 @@
 ts%5B2:%5D:%0A
+    
         

Fix for low number of build files