Skip to content

Commit

Permalink
main: don't do CLI error handling in build_package*
Browse files Browse the repository at this point in the history
Signed-off-by: Filipe Laíns <[email protected]>
  • Loading branch information
FFY00 committed Jun 16, 2021
1 parent 8dcfdb9 commit 6717501
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Breaking Changes

- Binary distributions are now built via the sdist by default in the CLI (`PR #304`_, Fixes `#257`_)
- ``python -m build`` will now build a sdist, extract it, and build a wheel from the source
- As a side-effect of `PR #304`_, ``build.__main__.build_package`` no longer does CLI error handling (print nice message and exit the program)

.. _PR #303: https://github.com/pypa/build/pull/303
.. _PR #304: https://github.com/pypa/build/pull/304
Expand Down
41 changes: 22 additions & 19 deletions src/build/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,9 @@ def build_package(srcdir, outdir, distributions, config_settings=None, isolation
:param isolation: Isolate the build in a separate environment
:param skip_dependency_check: Do not perform the dependency check
"""
with _handle_build_error():
builder = ProjectBuilder(srcdir)
for distribution in distributions:
_build(isolation, builder, outdir, distribution, config_settings, skip_dependency_check)
builder = ProjectBuilder(srcdir)
for distribution in distributions:
_build(isolation, builder, outdir, distribution, config_settings, skip_dependency_check)


def build_package_via_sdist(srcdir, outdir, distributions, config_settings=None, isolation=True, skip_dependency_check=False):
Expand All @@ -161,21 +160,20 @@ def build_package_via_sdist(srcdir, outdir, distributions, config_settings=None,
if 'sdist' in distributions:
raise ValueError('Only binary distributions are allowed but sdist was specified')

with _handle_build_error():
builder = ProjectBuilder(srcdir)
sdist = _build(isolation, builder, outdir, 'sdist', config_settings, skip_dependency_check)
builder = ProjectBuilder(srcdir)
sdist = _build(isolation, builder, outdir, 'sdist', config_settings, skip_dependency_check)

# extract sdist
sdist_name = os.path.basename(sdist)
sdist_out = tempfile.mkdtemp(dir=outdir, prefix='build-via-sdist-')
with tarfile.open(sdist) as t:
t.extractall(sdist_out)
builder = ProjectBuilder(os.path.join(sdist_out, sdist_name[: -len('.tar.gz')]))
for distribution in distributions:
_build(isolation, builder, outdir, distribution, config_settings, skip_dependency_check)
# extract sdist
sdist_name = os.path.basename(sdist)
sdist_out = tempfile.mkdtemp(dir=outdir, prefix='build-via-sdist-')
with tarfile.open(sdist) as t:
t.extractall(sdist_out)
builder = ProjectBuilder(os.path.join(sdist_out, sdist_name[: -len('.tar.gz')]))
for distribution in distributions:
_build(isolation, builder, outdir, distribution, config_settings, skip_dependency_check)

# remove sdist source if there was no exception
shutil.rmtree(sdist_out, ignore_errors=True)
# remove sdist source if there was no exception
shutil.rmtree(sdist_out, ignore_errors=True)


def main_parser(): # type: () -> argparse.ArgumentParser
Expand Down Expand Up @@ -259,7 +257,7 @@ def main_parser(): # type: () -> argparse.ArgumentParser
return parser


def main(cli_args, prog=None): # type: (List[str], Optional[str]) -> None
def main(cli_args, prog=None): # type: (List[str], Optional[str]) -> None # noqa: C901
"""
Parse the CLI arguments and invoke the build process.
Expand Down Expand Up @@ -298,7 +296,12 @@ def main(cli_args, prog=None): # type: (List[str], Optional[str]) -> None
else:
build_call = build_package_via_sdist
distributions = ['wheel']
build_call(args.srcdir, outdir, distributions, config_settings, not args.no_isolation, args.skip_dependency_check)
try:
with _handle_build_error():
build_call(args.srcdir, outdir, distributions, config_settings, not args.no_isolation, args.skip_dependency_check)
except Exception as e: # pragma: no cover
print(traceback.format_exc())
_error(str(e))


def entrypoint(): # type: () -> None
Expand Down
14 changes: 6 additions & 8 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import contextlib
import io
import os
import re
import sys

import pytest
Expand Down Expand Up @@ -174,24 +175,21 @@ def test_build_no_isolation_with_check_deps(mocker, test_flit_path, missing_deps

@pytest.mark.isolated
def test_build_raises_build_exception(mocker, test_flit_path):
error = mocker.patch('build.__main__._error')
mocker.patch('build.ProjectBuilder.get_requires_for_build', side_effect=build.BuildException)
mocker.patch('build.env._IsolatedEnvVenvPip.install')

build.__main__.build_package(test_flit_path, '.', ['sdist'])

error.assert_called_with('')
with pytest.raises(build.BuildException):
build.__main__.build_package(test_flit_path, '.', ['sdist'])


@pytest.mark.isolated
def test_build_raises_build_backend_exception(mocker, test_flit_path):
error = mocker.patch('build.__main__._error')
mocker.patch('build.ProjectBuilder.get_requires_for_build', side_effect=build.BuildBackendException(Exception('a')))
mocker.patch('build.env._IsolatedEnvVenvPip.install')

build.__main__.build_package(test_flit_path, '.', ['sdist'])
msg = "Backend operation failed: Exception('a'{})".format(',' if sys.version_info < (3, 7) else '')
error.assert_called_with(msg)
with pytest.raises(build.BuildBackendException, match=re.escape(msg)):
build.__main__.build_package(test_flit_path, '.', ['sdist'])


def test_build_package(tmp_dir, test_setuptools_path):
Expand All @@ -213,7 +211,7 @@ def test_build_package_via_sdist(tmp_dir, test_setuptools_path):


def test_build_package_via_sdist_cant_build(tmp_dir, test_cant_build_via_sdist_path):
with pytest.raises(SystemExit):
with pytest.raises(build.BuildBackendException):
build.__main__.build_package_via_sdist(test_cant_build_via_sdist_path, tmp_dir, ['wheel'])


Expand Down

0 comments on commit 6717501

Please sign in to comment.