Did I find the right examples for you? yes no      Crawl my project      Python Jobs

All Samples(4)  |  Call(3)  |  Derive(0)  |  Import(1)
Scans for all eggs used by a buildout in eggs, and return their names.

If install_folder is given, the eggs scanned are fetched and installed
with their dependencies there.

If ignore is given, it is a list of glob-like values
the egg names are tested against them and if they match
they are excluded from installation.

        def project_eggs(cfg, tarball=None, filter_eggs=None):
    """Scans for all eggs used by a buildout in eggs, and return their names.

    If install_folder is given, the eggs scanned are fetched and installed
    with their dependencies there.

    If ignore is given, it is a list of glob-like values
    the egg names are tested against them and if they match
    they are excluded from installation.
    """
    def _install(egg):
        if filter_eggs is None:
            return True
        
        for pattern in filter_eggs:
            if fnmatch(egg, pattern):
                return True
        return False

    buildout_dir, buildout_file = os.path.split(cfg)
    if buildout_dir == '':
        buildout_dir = os.path.realpath(os.curdir)

    def _(lines):
        return [l.strip() 
                for l in lines.split('\n') if l.strip() != '']
   
    tmp = mkdtemp()
    _log('Working in %s' % tmp)

    
    eggs_directory = join(tmp, 'eggs')
    install_folder = tmp
    if tarball is None:
        tarball = cfg.split('.')[0]

    downloads_directory = join(tmp, 'downloads')
    os.mkdir(downloads_directory)
    develop_eggs_directory = join(tmp, 'develop-eggs')
    os.mkdir(develop_eggs_directory)
    
    # forcing a few values
    config = ConfigParser.ConfigParser()
    config.read([cfg])

    for name, value in (('eggs-directory', eggs_directory),
                        ('develop-eggs-directory', develop_eggs_directory), 
                        ('download-cache', downloads_directory)):
        config.set('buildout', name, value)

    cfg_file = join(tmp, buildout_file)
    config.write(open(cfg_file, 'w'))

    # copying dependencies
    def _copy_extends(config, buildout_dir, tmp):
        if not 'buildout' in config.sections():
            return
        if not 'extends' in config.options('buildout'):
            return
        extends = chain(*[el.split() for el in _(config.get('buildout', 'extends'))])
        for extend in extends:
            shutil.copyfile(join(buildout_dir, extend), join(tmp, extend))
            subconfig = ConfigParser.ConfigParser()
            subconfig.read([join(buildout_dir, extend)])
            _copy_extends(subconfig, buildout_dir, tmp) 

    _copy_extends(config, buildout_dir, tmp)

    buildout = Buildout(cfg_file, [], user_defaults=False)
    buildout._load_extensions()

    # let's get the find-links 
    if 'find-links' in buildout['buildout']:
        find_links = _(buildout['buildout']['find-links'])
    else:
        find_links = []

    if 'index' in buildout['buildout']:
        index = buildout['buildout']['index']
    else:
        index = None

    # let's catch the versions
    versions = {}
    if 'versions' in  buildout['buildout']:
        versions = buildout['buildout']['versions']
        if 'versions' in buildout:
            versions = buildout['versions']
     
    # let's collect the used eggs
    _eggs = []
    for section in buildout:
        if section == 'versions':
            continue
        if 'eggs' in buildout[section]:
            _eggs.extend(_(buildout[section]['eggs']))
        if 'recipe' in buildout[section]:
            _eggs.append(buildout[section]['recipe'].strip())

    
    eggs = []
    for egg in _eggs:
        egg.strip()
        if egg not in eggs:
            eggs.append(egg)

    # installing the eggs
    _log('Installing eggs in %s.' % install_folder)
    installer = Installer(eggs_directory, links=find_links, 
                          index=index, versions=versions)

    target = join(install_folder, 'eggs')
    if not os.path.exists(target):
        os.mkdir(target)
    
    for egg in eggs:
        if not _install(egg):
            continue
        try:
            _log('Installing %s' % egg)
            installer.install((egg,))
        except (MissingDistribution, DistutilsError):
            _log('Could not install %s' % egg)

    def _check(egg):
        egg = egg.strip()
        if '==' in egg:
            egg, version = egg.split('==')
            if not _install(egg.strip()):
                return None, egg
            return '%s (%s)' % (egg.strip(), version.strip()), egg
        if not _install(egg):
            return None, egg
        if egg in versions:
            return '%s (%s)' % (egg, versions[egg]), egg
        return egg, egg

    # copying the final selection
    
    
    def _remove(element):
        if os.path.isdir(element):  
            shutil.rmtree(element, ignore_errors=True)
        else:
            os.remove(element)
    
    def _copy(element, target):
        if os.path.exists(target):
            return
        if os.path.isdir(element):  
            shutil.copytree(element, target)
        else:
            shutil.copyfile(element, target)
    
    if target != eggs_directory:
        for egg in os.listdir(eggs_directory):
            if egg == 'eggs':
                continue
            eggname = egg.split('-')[0]
            if not _install(eggname):
                _remove(join(eggs_directory, egg)) 
                continue
            if os.path.exists(join(target, egg)):
                continue
            _copy(join(eggs_directory, egg), join(target, egg))
            _remove(join(eggs_directory, egg))
    else:
        for egg in os.listdir(eggs_directory):
            eggname = egg.split('-')[0]
            if not _install(eggname):
                _remove(join(eggs_directory, egg))

    # let's copy the .cfg files as well
    _log('Copying all cfg files')
    for file_ in os.listdir(buildout_dir):
        if not os.path.splitext(file_) in ('.txt', '.cfg'):
            continue
        to_ = join(install_folder, file_)
        if os.path.exists(to_):
            os.remove(to_)
        shutil.copyfile(join(buildout_dir, file_), to_) 

    # archiving
    _log('Archiving to %s' % tarball)
    exclude = ['downloads', 'develop-eggs']    
    archive_contents(tarball, install_folder, exclude=exclude)
    installed = ['%s (%s)' % (egg.split('-')[0], egg.split('-')[1]) 
                 for egg in os.listdir(target)]
    _remove(tmp)
    return installed
        


src/i/w/iw.releaser-0.4.1/iw/releaser/tests/test_project.py   iw.releaser(Download)
from zc.buildout.testing import rmdir
 
from iw.releaser.project import (archive_contents, _python, 
                                 diff_releases, _set_dynlibs, 
                                 _extract_url,  _make_python,
        project = join(os.path.dirname(__file__), 'buildout')
        tarball = join(os.path.dirname(__file__), 'res.tgz') 
        eggs =  project_eggs(join(project, 'buildout.cfg'), tarball)
 
        self.assert_(eggs[0].startswith('iw.fss'))
        folder = tempfile.mkdtemp()
        try: 
            eggs =  project_eggs(join(project, 'buildout.cfg'), tarball) 
 
            # what do we have
        folder = tempfile.mkdtemp()
        try: 
            eggs =  project_eggs(join(project, 'buildout.cfg'), tarball,
                                 ('iw.f*',)) 
            # what do we have