Example #1
0
def extract(path: str) -> ExtractedMetadata:
    if os.path.basename(path) != 'setup.py':
        raise _errors.UnhandledFileError(path, 'setup.py')

    spec = importlib.util.spec_from_file_location('setuppy', path)
    setuppy = importlib.util.module_from_spec(spec)

    params = dict()  # type: Dict[str, str]

    def _fake_setup(*args, **kwargs):
        nonlocal params
        params = kwargs

    with patch('setuptools.setup') as setuptools_mock:
        with patch('distutils.core.setup') as distutils_mock:
            setuptools_mock.side_effect = _fake_setup
            distutils_mock.side_effect = _fake_setup
            # This would really fail during the use of the plugin
            # but let's be cautios and add the proper guards.
            try:
                spec.loader.exec_module(setuppy)
            except SystemExit as e:
                raise _errors.SetupPyFileParseError(path=path)
            except ImportError as e:
                raise _errors.SetupPyImportError(path=path,
                                                 error=str(e)) from e

    version = params.get('version')
    description = params.get('description')

    return ExtractedMetadata(version=version, description=description)
Example #2
0
def extract(relpath: str, *, workdir: str) -> ExtractedMetadata:
    if os.path.basename(relpath) != "setup.py":
        raise _errors.UnhandledFileError(relpath, "setup.py")

    spec = importlib.util.spec_from_file_location(
        "setuppy", os.path.join(workdir, relpath)
    )
    setuppy = importlib.util.module_from_spec(spec)

    params = dict()  # type: Dict[str, str]

    def _fake_setup(*args, **kwargs):
        nonlocal params
        params = kwargs

    with patch("setuptools.setup") as setuptools_mock:
        with patch("distutils.core.setup") as distutils_mock:
            setuptools_mock.side_effect = _fake_setup
            distutils_mock.side_effect = _fake_setup

            # Should never happen, but ensure spec.loader is set.
            loader = spec.loader
            if loader is None or not isinstance(loader, Loader):
                raise RuntimeError("Invalid spec loader")

            # This would really fail during the use of the plugin
            # but let's be cautious and add the proper guards.
            try:
                loader.exec_module(setuppy)
            except SystemExit:
                raise _errors.SetupPyFileParseError(path=relpath)
            except ImportError as e:
                raise _errors.SetupPyImportError(path=relpath, error=str(e)) from e

    version = params.get("version")
    description = params.get("description")

    return ExtractedMetadata(version=version, description=description)