Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PEP-517 issues for projects using build scripts #12

Merged
merged 4 commits into from
Apr 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
uses: actions/cache@v1
with:
path: .venv
key: venv-${{ runner.os }}-${{ steps.full-python-version.outputs.version }}-${{ hashFiles('**/poetry.lock') }}
key: venv-${{ runner.os }}-py${{ steps.full-python-version.outputs.version }}-${{ hashFiles('**/poetry.lock') }}
- name: Install dependencies
run: |
source $HOME/.poetry/env
Expand Down Expand Up @@ -81,7 +81,7 @@ jobs:
uses: actions/cache@v1
with:
path: .venv
key: venv-${{ runner.os }}-${{ steps.full-python-version.outputs.version }}-${{ hashFiles('**/poetry.lock') }}
key: venv-${{ runner.os }}-py${{ steps.full-python-version.outputs.version }}-${{ hashFiles('**/poetry.lock') }}
- name: Install dependencies
run: |
source $HOME/.poetry/env
Expand Down Expand Up @@ -118,7 +118,7 @@ jobs:
uses: actions/cache@v1
with:
path: .venv
key: venv-${{ runner.os }}-${{ steps.full-python-version.outputs.version }}-${{ hashFiles('**/poetry.lock') }}
key: venv-${{ runner.os }}-py${{ steps.full-python-version.outputs.version }}-${{ hashFiles('**/poetry.lock') }}
- name: Install dependencies
run: |
$env:Path += ";$env:Userprofile\.poetry\bin"
Expand Down
21 changes: 21 additions & 0 deletions poetry/core/masonry/builders/sdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
import time

from collections import defaultdict
from contextlib import contextmanager
from copy import copy
from gzip import GzipFile
from io import BytesIO
from posixpath import join as pjoin
from pprint import pformat
from typing import Iterator

from poetry.core.utils._compat import Path
from poetry.core.utils._compat import decode
from poetry.core.utils._compat import encode
from poetry.core.utils._compat import to_str

Expand Down Expand Up @@ -198,6 +201,24 @@ def build_setup(self): # type: () -> bytes
)
)

@contextmanager
def setup_py(self): # type: () -> Iterator[Path]
setup = self._path / "setup.py"
has_setup = setup.exists()

if has_setup:
logger.info(
" - <warning>A setup.py file already exists. Using it.</warning>"
)
else:
with setup.open("w", encoding="utf-8") as f:
f.write(decode(self.build_setup()))

yield setup

if not has_setup:
setup.unlink()

def build_pkg_info(self):
return encode(self.get_metadata_content())

Expand Down
66 changes: 33 additions & 33 deletions poetry/core/masonry/builders/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from ..utils.helpers import normalize_file_permissions
from ..utils.package_include import PackageInclude
from .builder import Builder
from .sdist import SdistBuilder


wheel_file_template = """\
Expand Down Expand Up @@ -92,39 +93,38 @@ def build(self):

def _build(self, wheel):
if self._package.build:
setup = self._path / "setup.py"

# We need to place ourselves in the temporary
# directory in order to build the package
current_path = os.getcwd()
try:
os.chdir(str(self._path))
self._run_build_command(setup)
finally:
os.chdir(current_path)

build_dir = self._path / "build"
lib = list(build_dir.glob("lib.*"))
if not lib:
# The result of building the extensions
# does not exist, this may due to conditional
# builds, so we assume that it's okay
return

lib = lib[0]

for pkg in lib.glob("**/*"):
if pkg.is_dir() or self.is_excluded(pkg):
continue

rel_path = str(pkg.relative_to(lib))

if rel_path in wheel.namelist():
continue

logger.debug(" - Adding: {}".format(rel_path))

self._add_file(wheel, pkg, rel_path)
with SdistBuilder(poetry=self._poetry).setup_py() as setup:
# We need to place ourselves in the temporary
# directory in order to build the package
current_path = os.getcwd()
try:
os.chdir(str(self._path))
self._run_build_command(setup)
finally:
os.chdir(current_path)

build_dir = self._path / "build"
lib = list(build_dir.glob("lib.*"))
if not lib:
# The result of building the extensions
# does not exist, this may due to conditional
# builds, so we assume that it's okay
return

lib = lib[0]

for pkg in lib.glob("**/*"):
if pkg.is_dir() or self.is_excluded(pkg):
continue

rel_path = str(pkg.relative_to(lib))

if rel_path in wheel.namelist():
continue

logger.debug(" - Adding: {}".format(rel_path))

self._add_file(wheel, pkg, rel_path)

def _run_build_command(self, setup):
subprocess.check_call(
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ classifiers = [

packages = [
{include = "poetry"},
{include = "tests", format = "sdist"},
]

[tool.poetry.dependencies]
Expand Down
26 changes: 26 additions & 0 deletions tests/masonry/builders/test_sdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from poetry.core.packages.dependency import Dependency
from poetry.core.packages.vcs_dependency import VCSDependency
from poetry.core.utils._compat import Path
from poetry.core.utils._compat import encode
from poetry.core.utils._compat import to_str


Expand Down Expand Up @@ -247,6 +248,31 @@ def test_package():
assert "my-package-1.2.3/LICENSE" in tar.getnames()


def test_setup_py_context():
poetry = Factory().create_poetry(project("complete"))

builder = SdistBuilder(poetry)

project_setup_py = poetry.file.parent / "setup.py"

assert not project_setup_py.exists()

try:
with builder.setup_py() as setup:
assert setup.exists()
assert project_setup_py == setup

with open(str(setup), "rb") as f:
# we convert to string and replace line endings here for compatibility
data = to_str(encode(f.read())).replace("\r\n", "\n")
assert data == to_str(builder.build_setup())

assert not project_setup_py.exists()
finally:
if project_setup_py.exists():
project_setup_py.unlink()


def test_module():
poetry = Factory().create_poetry(project("module1"))

Expand Down
25 changes: 25 additions & 0 deletions tests/masonry/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
from __future__ import unicode_literals

import os
import platform
import sys
import tarfile
import zipfile

from contextlib import contextmanager

import pytest

from poetry.core import __version__
from poetry.core.masonry import api
from poetry.core.utils._compat import Path
Expand Down Expand Up @@ -51,6 +55,27 @@ def test_build_wheel():
assert "my_package-1.2.3.dist-info/METADATA" in namelist


@pytest.mark.skipif(
sys.platform == "win32"
and sys.version_info <= (3, 6)
or platform.python_implementation().lower() == "pypy",
reason="Disable test on Windows for Python <=3.6 and for PyPy",
)
def test_build_wheel_extended():
with temporary_directory() as tmp_dir, cwd(os.path.join(fixtures, "extended")):
filename = api.build_wheel(tmp_dir)

whl = Path(tmp_dir) / filename
assert whl.exists()

with zipfile.ZipFile(str(os.path.join(tmp_dir, filename))) as zip:
namelist = zip.namelist()

assert "extended-0.1.dist-info/RECORD" in namelist
assert "extended-0.1.dist-info/WHEEL" in namelist
assert "extended-0.1.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)
Expand Down