• Facebook
  • Twitter
  • Reddit
  • StumbleUpon
  • Digg
  • email

import sys
import os
import buildkit
import logging
 
log = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
 
python = sys.executable
version = sys.version[:3]
 
# Variables
base = os.getcwd()
docs_path = os.path.join(
    base,
    'website',
    'work', 
    'code'
)
template_path = os.path.abspath(
    os.path.join(docs_path, '..', '..', 'Templates', 'main.dwt')
)
exclude = ['eggs', 'eggs_3rd_party', 'website', 'not_yet']
# Get package list
packages = []
if len(sys.argv)>1:
    packages += [arg.strip('/') for arg in sys.argv[1:]]
else:
    for directory in os.listdir(base):
        if os.path.isdir(directory) and directory not in exclude:
            packages.append(directory)
packages.sort()
log.info("Processing these packages: %s", (', '.join(packages)))
# Get all the distributions up to date for the tests so that you can test
# the latest versions of all the packages together
for package in packages:
    log.debug("Building %r"%package)
    buildkit.remove_dist(os.path.join(base, package))
    buildkit.build_dist(
        path=os.path.join(base, package),
        third_party_eggs_path=os.path.join(base, 'eggs_3rd_party'),
        python=python,
        setuptools_version='setuptools-0.6c11-py%s.egg'%version,
    )
    buildkit.prepare_sources(
        os.path.join(base, package, 'dist'), 
        os.path.join(base, 'eggs'),
    )
# Now with all the latest development eggs produced, run the tests from 
# the generated eggs. We don't run from the initial source because we need
# to check that the packaging worked correctly.
passed = []
failed = []
for package in packages:
    log.debug("Testing %r"%package)
    # We have to get the pkg_info from the main package though 
    pkg_info = buildkit.get_pkg_info(
        package, 
        os.path.join(
            base, 
            package, 
        )
    )
    # Now we can extract and use the distribution
    buildkit.run(
        ['tar', 'zxfv', package+'-'+pkg_info['Version'][0]+'.tar.gz'],
        cwd=os.path.join(
            base, 
            package, 
            'dist', 
        )
    )
    # test_dist() tests the distribution
    result, stdout = buildkit.test_dist(
        requirement=package+'=='+pkg_info['Version'][0], 
        path=os.path.join(
            base, 
            package, 
            #'dist', 
            #package+'-'+pkg_info['Version'][0],
        ),
        eggs_path=os.path.join(base, 'eggs'),
        third_party_eggs_path=os.path.join(base, 'eggs_3rd_party'),
        python=python,
        test_requirement=package+'[test]',
    )
    if result == 0:
        buildkit.cp(
            os.path.join(
                base, 
                'eggs', 
                package+'-'+pkg_info['Version'][0]+'.tar.gz'
            ),       
            os.path.join(
                docs_path, 
                'eggs', 
                package+'-'+pkg_info['Version'][0]+'.tar.gz'
            ),
        )
        # Check the docs build from the source
        result = buildkit.build_html_docs(
            package, 
            path=os.path.join(
                base, 
                package, 
                'dist', 
                package+'-'+pkg_info['Version'][0],
            ),
        )
        print result
        buildkit.copy_html_docs(
            docs_path,
            name=package,
            path=os.path.join(
                base, 
                package, 
                'dist', 
                package+'-'+pkg_info['Version'][0],
            ),
            version=pkg_info['Version'][0],
        )
        buildkit.package_index_page(
            eggs_path=os.path.join(base, 'eggs'),
            package_docs_path = os.path.join(docs_path, package.lower()),
            template_path = template_path,
            pkg_info=pkg_info,
        )
        #buildkit.remove_dist(
        #    os.path.join(base, package)
        #)
        passed.append(package)
        log.error(stdout)
    else:
        failed.append(package)
        log.error(stdout)
if passed:
    buildkit.egg_index_page(
        docs_path = docs_path,
        template_path = template_path,
        eggs_path=os.path.join(base, 'eggs'),
    )
    buildkit.egg_index_page(
        docs_path = docs_path,
        template_path = template_path,
        eggs_path=os.path.join(base, 'eggs_3rd_party'),
        eggs_dir='eggs_3rd_party',
        copy_eggs=True,
    )
    buildkit.main_index_page(
        docs_path = docs_path,
        template_path = template_path,
        exclude = exclude,
    )
log.info("Passed: %s"%(', '.join(passed),))
log.info("Failed: %s"%(', '.join(failed),))