Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion setuptools/command/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
'alias', 'bdist_egg', 'bdist_rpm', 'build_ext', 'build_py', 'develop',
'easy_install', 'egg_info', 'install', 'install_lib', 'rotate', 'saveopts',
'sdist', 'setopt', 'test', 'install_egg_info', 'install_scripts',
'register', 'bdist_wininst', 'upload_docs', 'upload', 'build_clib',
'register', 'bdist_wininst', 'upload_docs', 'upload', 'build_clib', 'dist_info',
]

from distutils.command.bdist import bdist
Expand Down
37 changes: 37 additions & 0 deletions setuptools/command/dist_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""
Create a dist_info directory
As defined in the wheel specification
"""

import os
import shutil

from distutils.core import Command


class dist_info(Command):

description = 'create a .dist-info directory'

user_options = [
('egg-base=', 'e', "directory containing .egg-info directories"
" (default: top of the source tree)"),
]

def initialize_options(self):
self.egg_base = None

def finalize_options(self):
pass

def run(self):
egg_info = self.get_finalized_command('egg_info')
egg_info.run()
dist_info_dir = egg_info.egg_info[:-len('.egg-info')] + '.dist-info'

bdist_wheel = self.get_finalized_command('bdist_wheel')
bdist_wheel.egg2dist(egg_info.egg_info, dist_info_dir)

if self.egg_base:
shutil.move(dist_info_dir, os.path.join(
self.egg_base, dist_info_dir))
10 changes: 10 additions & 0 deletions setuptools/dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@
__import__('pkg_resources.extern.packaging.version')


_skip_install_eggs = False


class SetupRequirementsError(BaseException):
def __init__(self, specifiers):
self.specifiers = specifiers


def _get_unpatched(cls):
warnings.warn("Do not call this function", DeprecationWarning)
return get_unpatched(cls)
Expand Down Expand Up @@ -332,6 +340,8 @@ def __init__(self, attrs=None):
self.dependency_links = attrs.pop('dependency_links', [])
assert_string_list(self, 'dependency_links', self.dependency_links)
if attrs and 'setup_requires' in attrs:
if _skip_install_eggs:
raise SetupRequirementsError(attrs['setup_requires'])
self.fetch_build_eggs(attrs['setup_requires'])
for ep in pkg_resources.iter_entry_points('distutils.setup_keywords'):
vars(self).setdefault(ep.name, None)
Expand Down
128 changes: 128 additions & 0 deletions setuptools/pep517.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
"""A PEP 517 interface to setuptools

Previously, when a user or a command line tool (let's call it a "frontend")
needed to make a request of setuptools to take a certain action, for
example, generating a list of installation requirements, the frontend would
would call "setup.py egg_info" or "setup.py bdist_wheel" on the command line.

PEP 517 defines a different method of interfacing with setuptools. Rather
than calling "setup.py" directly, the frontend should:

1. Set the current directory to the directory with a setup.py file
2. Import this module into a safe python interpreter (one in which
setuptools can potentially set global variables or crash hard).
3. Call one of the functions defined in PEP 517.

What each function does is defined in PEP 517. However, here is a "casual"
definition of the functions (this definition should not be relied on for
bug reports or API stability):

- `build_wheel`: build a wheel in the folder and return the basename
- `get_requires_for_build_wheel`: get the `setup_requires` to build
- `prepare_metadata_for_build_wheel`: get the `install_requires`
- `build_sdist`: build an sdist in the folder and return the basename
- `get_requires_for_build_sdist`: get the `setup_requires` to build

Again, this is not a formal definition! Just a "taste" of the module.
"""

import os
import sys
import subprocess
import tokenize
import shutil
import tempfile

from setuptools import dist
from setuptools.dist import SetupRequirementsError


SETUPTOOLS_IMPLEMENTATION_REVISION = 0.1

def _run_setup(setup_script='setup.py'): #
# Note that we can reuse our build directory between calls
# Correctness comes first, then optimization later
__file__=setup_script
f=getattr(tokenize, 'open', open)(__file__)
code=f.read().replace('\\r\\n', '\\n')
f.close()
exec(compile(code, __file__, 'exec'))


def _fix_config(config_settings):
config_settings = config_settings or {}
config_settings.setdefault('--global-option', [])
return config_settings


def _get_build_requires(config_settings):
config_settings = _fix_config(config_settings)
requirements = ['setuptools', 'wheel']
dist._skip_install_eggs = True

sys.argv = sys.argv[:1] + ['egg_info'] + \
config_settings["--global-option"]
try:
_run_setup()
except SetupRequirementsError as e:
requirements += e.specifiers

dist._skip_install_eggs = False

return requirements


def get_requires_for_build_wheel(config_settings=None):
config_settings = _fix_config(config_settings)
return _get_build_requires(config_settings)


def get_requires_for_build_sdist(config_settings=None):
config_settings = _fix_config(config_settings)
return _get_build_requires(config_settings)


def prepare_metadata_for_build_wheel(metadata_directory, config_settings=None):
sys.argv = sys.argv[:1] + ['dist_info', '--egg-base', metadata_directory]
_run_setup()

dist_infos = [f for f in os.listdir(metadata_directory)
if f.endswith('.dist-info')]

assert len(dist_infos) == 1
return dist_infos[0]


def build_wheel(wheel_directory, config_settings=None,
metadata_directory=None):
config_settings = _fix_config(config_settings)
wheel_directory = os.path.abspath(wheel_directory)
sys.argv = sys.argv[:1] + ['bdist_wheel'] + \
config_settings["--global-option"]
_run_setup()
if wheel_directory != 'dist':
shutil.rmtree(wheel_directory)
shutil.copytree('dist', wheel_directory)

wheels = [f for f in os.listdir(wheel_directory)
if f.endswith('.whl')]

assert len(wheels) == 1
return wheels[0]


def build_sdist(sdist_directory, config_settings=None):
config_settings = _fix_config(config_settings)
sdist_directory = os.path.abspath(sdist_directory)
sys.argv = sys.argv[:1] + ['sdist'] + \
config_settings["--global-option"]
_run_setup()
if sdist_directory != 'dist':
shutil.rmtree(sdist_directory)
shutil.copytree('dist', sdist_directory)

sdists = [f for f in os.listdir(sdist_directory)
if f.endswith('.tar.gz')]

assert len(sdists) == 1
return sdists[0]
115 changes: 115 additions & 0 deletions setuptools/tests/test_pep517.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import pytest
import os

# Only test the backend on Python 3
# because we don't want to require
# a concurrent.futures backport for testing
pytest.importorskip('concurrent.futures')

from contextlib import contextmanager
from importlib import import_module
from tempfile import mkdtemp
from concurrent.futures import ProcessPoolExecutor
from .files import build_files
from .textwrap import DALS
from . import contexts


class BuildBackendBase(object):
def __init__(self, cwd=None, env={}, backend_name='setuptools.pep517'):
self.cwd = cwd
self.env = env
self.backend_name = backend_name


class BuildBackend(BuildBackendBase):
"""PEP 517 Build Backend"""
def __init__(self, *args, **kwargs):
super(BuildBackend, self).__init__(*args, **kwargs)
self.pool = ProcessPoolExecutor()

def __getattr__(self, name):
"""Handles aribrary function invocations on the build backend."""
def method(*args, **kw):
return self.pool.submit(
BuildBackendCaller(os.path.abspath(self.cwd), self.env,
self.backend_name),
name, *args, **kw).result()

return method


class BuildBackendCaller(BuildBackendBase):
def __call__(self, name, *args, **kw):
"""Handles aribrary function invocations on the build backend."""
os.chdir(self.cwd)
os.environ.update(self.env)
return getattr(import_module(self.backend_name), name)(*args, **kw)


@contextmanager
def enter_directory(dir, val=None):
original_dir = os.getcwd()
os.chdir(dir)
yield val
os.chdir(original_dir)


@pytest.fixture
def build_backend():
tmpdir = mkdtemp()
with enter_directory(tmpdir):
setup_script = DALS("""
from setuptools import setup

setup(
name='foo',
py_modules=['hello'],
setup_requires=['six'],
entry_points={'console_scripts': ['hi = hello.run']},
zip_safe=False,
)
""")

build_files({
'setup.py': setup_script,
'hello.py': DALS("""
def run():
print('hello')
""")
})

return enter_directory(tmpdir, BuildBackend(cwd='.'))


def test_get_requires_for_build_wheel(build_backend):
with build_backend as b:
assert list(sorted(b.get_requires_for_build_wheel())) == \
list(sorted(['six', 'setuptools', 'wheel']))

def test_build_wheel(build_backend):
with build_backend as b:
dist_dir = os.path.abspath('pip-wheel')
os.makedirs(dist_dir)
wheel_name = b.build_wheel(dist_dir)

assert os.path.isfile(os.path.join(dist_dir, wheel_name))


def test_build_sdist(build_backend):
with build_backend as b:
dist_dir = os.path.abspath('pip-sdist')
os.makedirs(dist_dir)
sdist_name = b.build_sdist(dist_dir)

assert os.path.isfile(os.path.join(dist_dir, sdist_name))

def test_prepare_metadata_for_build_wheel(build_backend):
with build_backend as b:
dist_dir = os.path.abspath('pip-dist-info')
os.makedirs(dist_dir)

dist_info = b.prepare_metadata_for_build_wheel(dist_dir)

assert os.path.isfile(os.path.join(dist_dir, dist_info,
'METADATA'))