view paint/info.py @ 57:d5e5c7496784

stubbing
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 23 Jan 2013 16:26:14 -0800
parents 042a1b2a3e8a
children 13767ee2ddf4
line wrap: on
line source

"""
interfaces to get information from a package
"""

import imp
import os
import sys

from subprocess import check_call as call

class PackageInfo(object):
    """abstract base class of package info"""
    def __init__(self, path):
        """
        - path : path to setup.py or its directory
        """
        if os.path.isdir(path):
            path = os.path.join(path, 'setup.py')
        assert os.path.exists(path), "'%s' not found" % path
        self.setup_py = os.path.abspath(path)

    def __call__(self):
        """returns dictionary of package info"""
        raise NotImplementedError("abstract base class")

class SetupOverridePackageInfo(PackageInfo):
    """
    gather setup.py information by overriding the function
    """

    def __call__(self):
        setuptools = sys.modules.get('setuptools')
        sys.modules['setuptools'] = sys.modules[__name__]
        globals()['setup'] = self._setup
        try:
            module = imp.load_source('setup', self.setup_py)
        finally:
            sys.modules.pop('setuptools')
            if setuptools:
                sys.modules['setuptools'] = setuptools
            globals().pop('setup')
        return self.__dict__.pop('_info')

    def _setup(self, **kwargs):
        self._info = kwargs

class EggInfo(PackageInfo):
    """
    use `python setup.py egg_info` to gather package information
    """

    def __call__(self):
        raise NotImplementedError("TODO")

    def _egg_info(self):
        """build the egg_info directory"""

        if self._egg_info_path:
            return self._egg_info_path

        raise NotImplementedError("TODO")