#!/usr/bin/env python
"""
Generate stdlib/ from pyjs/lib, /usr/lib/python and pypy/lib
"""

import os
import sys
import shutil
from os.path import join, dirname, basename, abspath, exists, isdir
from optparse import OptionParser

root_path = dirname(abspath(__file__))
pyjslib_path = join(root_path, 'pyjs', 'src', 'pyjs', 'lib')
dest_path = join(root_path, 'stdlib')

pyjslib_excludes = ['output']
pypy_excludes = ['site-packages']
cpython_excludes = ['site-packages', 'lib-dynload', 'test']

mod_src = {}
def copy_libs(dest, src, src_name, excludes):
    for p in os.listdir(src):
        if isdir(join(src, p)):
            mod_name = p
        elif p.endswith('.py'):
            mod_name = p.split('.')[0]
        else:
            continue
        
        if (not exists(join(dest, mod_name)) and 
            not exists(join(dest, mod_name + '.py')) and
            not mod_name in excludes):
            if isdir(join(src, p)):
                shutil.copytree(join(src, p), join(dest, p))
            else:
                shutil.copy2(join(src, p), join(dest, p))
            mod_src[mod_name] = src_name
            

def main():
    parser = OptionParser()
    parser.add_option(
        "--pypy",
        dest="pypy",
        help="Path to PyPy libraries")
    parser.add_option(
        "--cpython",
        dest="cpython",
        help="Path to CPython libraries")
    
    options, args = parser.parse_args()
    if not options.cpython:        
        cpython_path = dirname(os.__file__)
    else:
        cpython_path = options.cpython
    
    pypy_path = options.pypy
    
    print ("Exporting data from:\n"
           "PyJS lib: {0}\nPyPy lib: {2}\nCPython lib: {1}\n".
           format(pyjslib_path, cpython_path, pypy_path))
    
    if exists(dest_path):
        shutil.rmtree(dest_path)
    os.mkdir(dest_path)
    
    copy_libs(dest_path, pyjslib_path, 'pyjs', pyjslib_excludes)
    if pypy_path:
        copy_libs(dest_path, pypy_path, 'pypy', pypy_excludes)
    copy_libs(dest_path, cpython_path, 'cpython', cpython_excludes)
    f_mod_src = open(join(dest_path, 'modules_sources'), 'w')
    for mod, src in mod_src.iteritems():
        f_mod_src.write("{}:{}\n".format(mod, src))
    f_mod_src.close()
        
    
if __name__ == '__main__':
    main()
@@ -266,137 +266,233 @@
 _))%0A
-pyjslib_path = join(root_path, 'pyjs', 'src', 'pyjs', 'lib')%0Adest_path = join(root_path, 'stdlib')%0A%0Apyjslib_excludes = %5B'output'%5D
+dest_path = join(root_path, 'stdlib')%0A%0Aimports = %5B%0A    ('pyjs', join(root_path, 'pyjs', 'src', 'pyjs', 'lib'), %5B'output', 'test'%5D),%0A    ('pyjs', join(root_path, 'pyjs', 'src', 'pyjs', 'lib_trans'), %5B'output', 'test'%5D),%0A    %5D%0A
 %0Apyp
@@ -520,16 +520,29 @@
 ackages'
+, 'pypy_test'
 %5D%0Acpytho
@@ -609,16 +609,382 @@
 rc = %7B%7D%0A
+def copy_tests(dest, src):%0A    dest = join(dest, 'test')%0A    src = join(src, 'test')%0A    if not exists(src):%0A        return%0A    if not isdir(src):%0A        return%0A    for p in os.listdir(src):%0A        if (not isdir(join(src, p)) %0A            and p.endswith('.py')%0A            and not exists(join(dest, p))):%0A            shutil.copy2(join(src, p), join(dest, p))%0A    %0A
 def copy
@@ -2035,19 +2035,10 @@
     
-pypy_path =
+if
  opt
@@ -2050,410 +2050,470 @@
 pypy
+:
 %0A    
-%0A
     
-print (%22Exporting data from:%5Cn%22%0A  
+imports.append(('pypy', options.pypy, pypy_excludes))%0A
     
- 
+%0A
     
-%22PyJS lib: %7B0%7D%5CnPyPy lib: %7B2%7D%5CnCPython lib: %7B1%7D%5Cn%22.%0A           format(pyjslib_path, cpython_path, pypy_path))%0A    %0A    if exists(dest_path):%0A        shutil.rmtree(dest_path)%0A    os.mkdir(dest_path)%0A    %0A    copy_libs(dest_path, pyjslib_path, 'pyjs', pyjslib_excludes)%0A    if pypy_path:%0A        copy_libs(dest_path, pypy_path, 'pypy', pypy_excludes)%0A
+if cpython_path:%0A        imports.append(('cpython', cpython_path, cpython_excludes))%0A        %0A    if exists(dest_path):%0A        shutil.rmtree(dest_path)%0A    os.mkdir(dest_path)%0A    os.mkdir(join(dest_path, 'test'))%0A    %0A    print (%22Exporting data from:%22)%0A    for name, path, exc in imports:%0A        print (%22%7B0%7D: %7B1%7D%22.format(name, path))  %0A        copy_libs(dest_path, path, name, exc)%0A    
     
@@ -2509,35 +2509,36 @@
 c)%0A        copy_
-lib
+test
 s(dest_path, cpy
@@ -2538,50 +2538,14 @@
 th, 
-cpython_path, 'cpython', cpython_excludes)
+path)%0A
 %0A   

Include lib_trans and tests in generated stdlib