Skip to content

Commit

Permalink
Keep Python 2 compatibility and allow escape prepare
Browse files Browse the repository at this point in the history
Signed-off-by: Bernát Gábor <[email protected]>
  • Loading branch information
gaborbernat committed Dec 5, 2022
1 parent ba388d9 commit 95fb06c
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 26 deletions.
2 changes: 0 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ repos:
- id: pyupgrade
args: ["--py37-plus"]
exclude: "^(src/pyproject_api/_backend.py|tests/demo_pkg_inline/build.py)$"
- id: pyupgrade
files: "^(src/pyproject_api/_backend.py|tests/demo_pkg_inline/build.py)$"
- repo: https://github.com/PyCQA/isort
rev: 5.10.1
hooks:
Expand Down
5 changes: 5 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Release History
===============

v1.2.1 - (2022-12-04)
---------------------
- Fix Python 2 incompatibility on the backend
- Allow skipping prepare metadata for the full build by returning None as basename

v1.2.0 - (2022-12-04)
---------------------
- Expose which optional hooks are present or missing via :meth:`pyproject_api.Frontend.optional_hooks`
Expand Down
28 changes: 10 additions & 18 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,18 @@ urls.Homepage = "http://pyproject_api.readthedocs.org"
urls.Source = "https://github.com/tox-dev/pyproject-api"
urls.Tracker = "https://github.com/tox-dev/pyproject-api/issues"
authors = [{ name = "Bernát Gábor", email = "[email protected]" }]
maintainers = [
{ name = "Bernát Gábor", email = "[email protected]" },
]
maintainers = [{ name = "Bernát Gábor", email = "[email protected]" }]
requires-python = ">=3.7"
dependencies = [
"packaging>=21.3", 'tomli>=2.0.1; python_version < "3.11"',
]
optional-dependencies.docs = [
"furo>=2022.9.29",
"sphinx>=5.3",
"sphinx-autodoc-typehints>=1.19.5",
]
dependencies = ["packaging>=21.3", 'tomli>=2.0.1; python_version < "3.11"']
optional-dependencies.docs = ["furo>=2022.9.29", "sphinx>=5.3", "sphinx-autodoc-typehints>=1.19.5"]
optional-dependencies.testing = [
"covdefaults>=2.2.2",
"pytest>=7.2",
"pytest-cov>=4",
"pytest-mock>=3.10",
'importlib-metadata>=5.1; python_version < "3.8"',
"wheel>=0.38.4",
"pytest>=7.2",
"pytest-cov>=4",
"pytest-mock>=3.10",
'importlib-metadata>=5.1; python_version < "3.8"',
"wheel>=0.38.4",
"virtualenv>=20.17",
]
dynamic = ["version"]
classifiers = [
Expand Down Expand Up @@ -86,6 +79,7 @@ line_length = 120
python_version = "3.11"
show_error_codes = true
strict = true
overrides = [{ module = ["virtualenv.*"], ignore_missing_imports = true }]

[tool.pep8]
max-line-length = "120"
Expand All @@ -96,5 +90,3 @@ max-line-length = 120
unused-arguments-ignore-abstract-functions = true
noqa-require-code = true
dictionaries = ["en_US", "python", "technical", "django"]
ignore = [
]
1 change: 1 addition & 0 deletions src/pyproject_api/_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Please keep this file Python 2.7 compatible.
See https://tox.readthedocs.io/en/rewrite/development.html#code-style-guide
"""
from __future__ import print_function

import importlib
import json
Expand Down
6 changes: 4 additions & 2 deletions src/pyproject_api/_frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,13 +326,14 @@ def prepare_metadata_for_build_wheel(
:return: metadata generation result
"""
self._check_metadata_dir(metadata_directory)
basename: str | None = None
if self.optional_hooks["prepare_metadata_for_build_wheel"]:
basename, out, err = self._send(
cmd="prepare_metadata_for_build_wheel",
metadata_directory=metadata_directory,
config_settings=config_settings,
)
else:
if basename is None:
# if backend does not provide it acquire it from the wheel
basename, err, out = self._metadata_from_built_wheel(config_settings, metadata_directory, "build_wheel")
if not isinstance(basename, str):
Expand All @@ -358,13 +359,14 @@ def prepare_metadata_for_build_editable(
:return: metadata generation result
"""
self._check_metadata_dir(metadata_directory)
basename = None
if self.optional_hooks["prepare_metadata_for_build_editable"]:
basename, out, err = self._send(
cmd="prepare_metadata_for_build_editable",
metadata_directory=metadata_directory,
config_settings=config_settings,
)
else:
if basename is None:
# if backend does not provide it acquire it from the wheel
basename, err, out = self._metadata_from_built_wheel(config_settings, metadata_directory, "build_editable")
if not isinstance(basename, str):
Expand Down
3 changes: 2 additions & 1 deletion src/pyproject_api/_via_fresh_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def __init__(
:param requires: seed requirements for the backend
"""
super().__init__(root, backend_paths, backend_module, backend_obj, requires, reuse_backend=False)
self.executable = sys.executable

@contextmanager
def _send_msg(self, cmd: str, result_file: Path, msg: str) -> Iterator[SubprocessCmdStatus]: # noqa: U100
Expand All @@ -58,7 +59,7 @@ def _send_msg(self, cmd: str, result_file: Path, msg: str) -> Iterator[Subproces
if backend:
env["PYTHONPATH"] = backend
process = Popen(
args=[sys.executable] + self.backend_args,
args=[self.executable] + self.backend_args,
stdout=PIPE,
stderr=PIPE,
stdin=PIPE,
Expand Down
Empty file added tests/_build_sdist.py
Empty file.
6 changes: 3 additions & 3 deletions tests/demo_pkg_inline/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ def get_requires_for_build_sdist(config_settings=None): # noqa: U100

if "HAS_REQUIRES_EDITABLE" in os.environ:

def get_requires_for_build_editable(config_settings=...): # noqa: U100
def get_requires_for_build_editable(config_settings=None): # noqa: U100
return [1] if "REQUIRES_EDITABLE_BAD_RETURN" in os.environ else ["editables"]


if "HAS_PREPARE_EDITABLE" in os.environ:

def prepare_metadata_for_build_editable(metadata_directory: str, config_settings=None) -> str: # noqa: U100
def prepare_metadata_for_build_editable(metadata_directory, config_settings=None): # noqa: U100
dest = os.path.join(metadata_directory, dist_info)
os.mkdir(dest)
for arc_name, data in content.items():
Expand All @@ -104,7 +104,7 @@ def prepare_metadata_for_build_editable(metadata_directory: str, config_settings
return dist_info


def build_editable(wheel_directory, metadata_directory=None, config_settings=None) -> str:
def build_editable(wheel_directory, metadata_directory=None, config_settings=None):
if "BUILD_EDITABLE_BAD" in os.environ:
return 1 # type: ignore # checking bad type on purpose
return build_wheel(wheel_directory, metadata_directory, config_settings)
10 changes: 10 additions & 0 deletions tests/test_fronted.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import pytest
from packaging.requirements import Requirement
from virtualenv import session_via_cli

from pyproject_api._frontend import BackendFailed
from pyproject_api._via_fresh_subprocess import SubprocessFrontend
Expand Down Expand Up @@ -306,3 +307,12 @@ def test_backend_build_editable_bad(tmp_path: Path, demo_pkg_inline: Path, monke
assert not exc.args
assert exc.exc_type == "TypeError"
assert exc.exc_msg == "'build_editable' on 'build' returned 1 but expected type <class 'str'>"


def test_can_build_on_python_2(demo_pkg_inline: Path, tmp_path: Path) -> None:
fronted = SubprocessFrontend(*SubprocessFrontend.create_args_from_folder(demo_pkg_inline)[:-1])
env = session_via_cli(["-p", "2.7", str(tmp_path / "venv")])
env.run()
fronted.executable = str(env.creator.exe)

fronted.build_sdist(tmp_path)
2 changes: 2 additions & 0 deletions whitelist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ autoclass
autodoc
cfg
delenv
exe
extlinks
intersphinx
iterdir
Expand All @@ -24,4 +25,5 @@ tomli
tomllib
typehints
unbuffered
virtualenv
win32

0 comments on commit 95fb06c

Please sign in to comment.