Commit 8055bcf4 by Sébastien Eustace

Support PEP-517 officially

parent 58e3676c
...@@ -217,3 +217,22 @@ To match the example in the setuptools documentation, you would use the followin ...@@ -217,3 +217,22 @@ To match the example in the setuptools documentation, you would use the followin
[tool.poetry.plugins."blogtool.parsers"] [tool.poetry.plugins."blogtool.parsers"]
".rst" = "some_module:SomeClass" ".rst" = "some_module:SomeClass"
``` ```
## Poetry and PEP-517
[PEP-517](https://www.python.org/dev/peps/pep-0517/) introduces a standard way
to define alternative build systems to build a Python project.
Poetry is compliant with PEP-517 so if you use Poetry to manage your Python
project you should reference it in the `build-system` section of the `pyproject.toml`
file like so:
```toml
[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"
```
!!!note
When using the `new` or `init` command this section will be automatically added.
from tomlkit import dumps from tomlkit import dumps
from tomlkit import loads from tomlkit import loads
from tomlkit import table
from poetry.utils.helpers import module_name from poetry.utils.helpers import module_name
...@@ -37,6 +38,9 @@ license = "" ...@@ -37,6 +38,9 @@ license = ""
[tool.poetry.dev-dependencies] [tool.poetry.dev-dependencies]
""" """
BUILD_SYSTEM_MIN_VERSION = "0.12"
BUILD_SYSTEM_MAX_VERSION = None
class Layout(object): class Layout(object):
def __init__( def __init__(
...@@ -102,6 +106,17 @@ class Layout(object): ...@@ -102,6 +106,17 @@ class Layout(object):
for dep_name, dep_constraint in self._dev_dependencies.items(): for dep_name, dep_constraint in self._dev_dependencies.items():
poetry_content["dev-dependencies"][dep_name] = dep_constraint poetry_content["dev-dependencies"][dep_name] = dep_constraint
# Add build system
build_system = table()
build_system_version = ">=" + BUILD_SYSTEM_MIN_VERSION
if BUILD_SYSTEM_MAX_VERSION is not None:
build_system_version += ",<" + BUILD_SYSTEM_MAX_VERSION
build_system.add("requires", ["poetry" + build_system_version])
build_system.add("build-backend", "poetry.masonry.api")
content.add("build-system", build_system)
return dumps(content) return dumps(content)
def _create_default(self, path, src=True): def _create_default(self, path, src=True):
......
...@@ -8,6 +8,7 @@ from pathlib import Path ...@@ -8,6 +8,7 @@ from pathlib import Path
from poetry.poetry import Poetry from poetry.poetry import Poetry
from poetry.io import NullIO from poetry.io import NullIO
from poetry.utils._compat import unicode
from poetry.utils.env import SystemEnv from poetry.utils.env import SystemEnv
from .builders import SdistBuilder from .builders import SdistBuilder
...@@ -15,17 +16,16 @@ from .builders import WheelBuilder ...@@ -15,17 +16,16 @@ from .builders import WheelBuilder
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
# PEP 517 specifies that the CWD will always be the source tree
poetry = Poetry.create(".")
def get_requires_for_build_wheel(config_settings=None): def get_requires_for_build_wheel(config_settings=None):
""" """
Returns a list of requirements for building, as strings Returns a list of requirements for building, as strings
""" """
main, extras = SdistBuilder.convert_dependencies(poetry.package.requires) poetry = Poetry.create(".")
main, _ = SdistBuilder.convert_dependencies(poetry.package, poetry.package.requires)
return main + extras return main
# For now, we require all dependencies to build either a wheel or an sdist. # For now, we require all dependencies to build either a wheel or an sdist.
...@@ -34,15 +34,21 @@ get_requires_for_build_sdist = get_requires_for_build_wheel ...@@ -34,15 +34,21 @@ get_requires_for_build_sdist = get_requires_for_build_wheel
def build_wheel(wheel_directory, config_settings=None, metadata_directory=None): def build_wheel(wheel_directory, config_settings=None, metadata_directory=None):
"""Builds a wheel, places it in wheel_directory""" """Builds a wheel, places it in wheel_directory"""
info = WheelBuilder.make_in(poetry, NullIO(), Path(wheel_directory)) poetry = Poetry.create(".")
return info.file.name return unicode(
WheelBuilder.make_in(
poetry, SystemEnv(Path(sys.prefix)), NullIO(), Path(wheel_directory)
)
)
def build_sdist(sdist_directory, config_settings=None): def build_sdist(sdist_directory, config_settings=None):
"""Builds an sdist, places it in sdist_directory""" """Builds an sdist, places it in sdist_directory"""
path = SdistBuilder(poetry, SystemEnv(sys.prefix), NullIO()).build( poetry = Poetry.create(".")
path = SdistBuilder(poetry, SystemEnv(Path(sys.prefix)), NullIO()).build(
Path(sdist_directory) Path(sdist_directory)
) )
return path.name return unicode(path.name)
...@@ -48,6 +48,8 @@ class WheelBuilder(Builder): ...@@ -48,6 +48,8 @@ class WheelBuilder(Builder):
wb = WheelBuilder(poetry, env, io, target_dir=directory, original=original) wb = WheelBuilder(poetry, env, io, target_dir=directory, original=original)
wb.build() wb.build()
return wb.wheel_filename
@classmethod @classmethod
def make(cls, poetry, env, io): def make(cls, poetry, env, io):
"""Build a wheel in the dist/ directory, and optionally upload it.""" """Build a wheel in the dist/ directory, and optionally upload it."""
......
import os
import tarfile
import zipfile
from contextlib import contextmanager
from poetry.masonry import api
from poetry.utils.helpers import temporary_directory
@contextmanager
def cwd(directory):
prev = os.getcwd()
os.chdir(str(directory))
try:
yield
finally:
os.chdir(prev)
fixtures = os.path.join(os.path.dirname(__file__), "builders", "fixtures")
def test_get_requires_for_build_wheel():
expected = ["cleo>=0.6.0,<0.7.0", "cachy[msgpack]>=0.2.0,<0.3.0"]
with cwd(os.path.join(fixtures, "complete")):
api.get_requires_for_build_wheel() == expected
def test_get_requires_for_build_sdist():
expected = ["cleo>=0.6.0,<0.7.0", "cachy[msgpack]>=0.2.0,<0.3.0"]
with cwd(os.path.join(fixtures, "complete")):
api.get_requires_for_build_sdist() == expected
def test_build_wheel():
with temporary_directory() as tmp_dir, cwd(os.path.join(fixtures, "complete")):
filename = api.build_wheel(tmp_dir)
with zipfile.ZipFile(str(os.path.join(tmp_dir, filename))) as zip:
namelist = zip.namelist()
assert "my_package-1.2.3.dist-info/entry_points.txt" in namelist
assert "my_package-1.2.3.dist-info/WHEEL" in namelist
assert "my_package-1.2.3.dist-info/METADATA" in namelist
def test_build_sdist():
with temporary_directory() as tmp_dir, cwd(os.path.join(fixtures, "complete")):
filename = api.build_sdist(tmp_dir)
with tarfile.open(str(os.path.join(tmp_dir, filename))) as tar:
namelist = tar.getnames()
assert "my-package-1.2.3/LICENSE" in namelist
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment