Skip to content

Commit

Permalink
Merge pull request #96 from pypa/revert-83-separate-highlevel
Browse files Browse the repository at this point in the history
Revert "Pull out high-level interfaces as an extended example"
  • Loading branch information
pradyunsg authored Oct 13, 2020
2 parents 00be440 + c03a9a9 commit ae41d4e
Show file tree
Hide file tree
Showing 30 changed files with 157 additions and 299 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ jobs:
cache: pip

install:
- pip install -U virtualenv
- pip install tox
- pip install tox tox-venv

script: tox
48 changes: 37 additions & 11 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,38 @@ provides:
the current process.
- Fallbacks for the optional hooks, so that frontends can call the hooks without
checking which are defined.
- Functions to load the build system table from ``pyproject.toml``, with
optional fallback to setuptools.
- Higher-level functions which install the build dependencies into a
temporary environment and build a wheel/sdist using them.

Run the tests with ``pytest`` or `tox <https://pypi.org/project/tox>`_.

Usage:
High level usage, with build requirements handled:

.. code-block:: python
import os
from pep517 import Pep517HookCaller
from pep517.pyproject import load_system
from pep517.envbuild import build_wheel, build_sdist
src = 'path/to/source' # Folder containing 'pyproject.toml'
build_sys = load_system(src)
destination = 'also/a/folder'
whl_filename = build_wheel(src, destination)
assert os.path.isfile(os.path.join(destination, whl_filename))
targz_filename = build_sdist(src, destination)
assert os.path.isfile(os.path.join(destination, targz_filename))
Lower level usage—you are responsible for ensuring build requirements are
available:

.. code-block:: python
import os
import toml
from pep517.wrappers import Pep517HookCaller
src = 'path/to/source' # Folder containing 'pyproject.toml'
with open(os.path.join(src, 'pyproject.toml')) as f:
build_sys = toml.load(f)['build-system']
print(build_sys['requires']) # List of static requirements
Expand All @@ -40,9 +57,18 @@ Usage:
whl_filename = hooks.build_wheel(destination, config_options)
assert os.path.isfile(os.path.join(destination, whl_filename))
The caller is responsible for installing build dependencies.
The static requirements should be installed before trying to call any hooks.
To test the build backend for a project, run in a system shell:

.. code-block:: shell
python3 -m pep517.check path/to/source # source dir containing pyproject.toml
To build a backend into source and/or binary distributions, run in a shell:

.. code-block:: shell
python -m pep517.build path/to/source # source dir containing pyproject.toml
The ``buildtool_demo`` package in this repository gives a more complete
example of how to use the hooks. This is an example, and doesn't get installed
with the ``pep517`` package.
This 'build' module should be considered experimental while the PyPA `decides
on the best place for this functionality
<https://github.com/pypa/packaging-problems/issues/219>`_.
5 changes: 0 additions & 5 deletions examples/README.rst

This file was deleted.

Empty file removed examples/buildtool/__init__.py
Empty file.
Empty file.
62 changes: 0 additions & 62 deletions examples/buildtool/tests/samples/buildsys_pkgs/buildsys.py

This file was deleted.

34 changes: 0 additions & 34 deletions examples/buildtool/tests/samples/buildsys_pkgs/buildsys_minimal.py

This file was deleted.

21 changes: 0 additions & 21 deletions examples/buildtool/tests/samples/pkg1/pkg1-0.5.dist-info/LICENSE

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

3 changes: 0 additions & 3 deletions examples/buildtool/tests/samples/pkg1/pkg1.py

This file was deleted.

3 changes: 0 additions & 3 deletions examples/buildtool/tests/samples/pkg1/pyproject.toml

This file was deleted.

2 changes: 0 additions & 2 deletions pep517/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@
"""

__version__ = '0.8.2'

from .wrappers import * # noqa: F401,F403
46 changes: 44 additions & 2 deletions examples/buildtool/build.py → pep517/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,58 @@
import argparse
import logging
import os
import toml
import shutil

from .envbuild import BuildEnvironment
from pep517 import Pep517HookCaller
from pep517.pyproject import load_system, validate_system
from .wrappers import Pep517HookCaller
from .dirtools import tempdir, mkdir_p
from .compat import FileNotFoundError

log = logging.getLogger(__name__)


def validate_system(system):
"""
Ensure build system has the requisite fields.
"""
required = {'requires', 'build-backend'}
if not (required <= set(system)):
message = "Missing required fields: {missing}".format(
missing=required-set(system),
)
raise ValueError(message)


def load_system(source_dir):
"""
Load the build system from a source dir (pyproject.toml).
"""
pyproject = os.path.join(source_dir, 'pyproject.toml')
with open(pyproject) as f:
pyproject_data = toml.load(f)
return pyproject_data['build-system']


def compat_system(source_dir):
"""
Given a source dir, attempt to get a build system backend
and requirements from pyproject.toml. Fallback to
setuptools but only if the file was not found or a build
system was not indicated.
"""
try:
system = load_system(source_dir)
except (FileNotFoundError, KeyError):
system = {}
system.setdefault(
'build-backend',
'setuptools.build_meta:__legacy__',
)
system.setdefault('requires', ['setuptools', 'wheel'])
return system


def _do_build(hooks, env, dist, dest):
get_requires_name = 'get_requires_for_build_{dist}'.format(**locals())
get_requires = getattr(hooks, get_requires_name)
Expand Down
2 changes: 1 addition & 1 deletion examples/buildtool/check.py → pep517/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from .colorlog import enable_colourful_output
from .envbuild import BuildEnvironment
from pep517 import Pep517HookCaller
from .wrappers import Pep517HookCaller

log = logging.getLogger(__name__)

Expand Down
File renamed without changes.
File renamed without changes.
37 changes: 1 addition & 36 deletions examples/buildtool/envbuild.py → pep517/envbuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
import sys
from sysconfig import get_paths
from tempfile import mkdtemp
import threading

from pep517 import Pep517HookCaller
from .wrappers import Pep517HookCaller, LoggerWrapper

log = logging.getLogger(__name__)

Expand All @@ -27,40 +26,6 @@ def _load_pyproject(source_dir):
)


class LoggerWrapper(threading.Thread):
"""
Read messages from a pipe and redirect them
to a logger (see python's logging module).
"""

def __init__(self, logger, level):
threading.Thread.__init__(self)
self.daemon = True

self.logger = logger
self.level = level

# create the pipe and reader
self.fd_read, self.fd_write = os.pipe()
self.reader = os.fdopen(self.fd_read)

self.start()

def fileno(self):
return self.fd_write

@staticmethod
def remove_newline(msg):
return msg[:-1] if msg.endswith(os.linesep) else msg

def run(self):
for line in self.reader:
self._write(self.remove_newline(line))

def _write(self, message):
self.logger.log(self.level, message)


class BuildEnvironment(object):
"""Context manager to install build deps in a simple temporary environment
Expand Down
4 changes: 2 additions & 2 deletions examples/buildtool/meta.py → pep517/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
from zipp import Path

from .envbuild import BuildEnvironment
from pep517 import Pep517HookCaller, quiet_subprocess_runner
from pep517.pyproject import validate_system, load_system, compat_system
from .wrappers import Pep517HookCaller, quiet_subprocess_runner
from .dirtools import tempdir, mkdir_p, dir_to_zipfile
from .build import validate_system, load_system, compat_system

log = logging.getLogger(__name__)

Expand Down
Loading

0 comments on commit ae41d4e

Please sign in to comment.