import os

import pkg_resources
import zipfile


def activate_egg(eggpath):
    """Activate a Scrapy egg file. This is meant to be used from egg runners
    to activate a Scrapy egg file. Don't use it from other code as it may
    leave unwanted side effects.
    """
    try:
        d = next(pkg_resources.find_distributions(eggpath))
    except StopIteration:
        raise ValueError("Unknown or corrupt egg")
    d.activate()
    settings_module = d.get_entry_info('scrapy', 'settings').module_name
    os.environ.setdefault('SCRAPY_SETTINGS_MODULE', settings_module)

def activate_scrapycfg(eggpath,extrpath):
    """Add scrapy.cfg to the environment
    """
    with zipfile.ZipFile(eggpath, 'r') as myzip:
        myzip.extractall(extrpath)

    os.environ.setdefault('XDG_CONFIG_HOME', extrpath)
