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 15, 2021
1 parent b57f849 commit 57d9c81
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 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
43 changes: 23 additions & 20 deletions src/build/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,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 @@ -151,21 +150,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 @@ -251,7 +249,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 @@ -289,8 +287,13 @@ def main(cli_args, prog=None): # type: (List[str], Optional[str]) -> None
build_call = build_package
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)
distributions = ['wheel', 'sdist']
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:
print(traceback.format_exc())
_error(str(e))


def entrypoint(): # type: () -> None
Expand Down

0 comments on commit 57d9c81

Please sign in to comment.