Skip to content
Open
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
19 changes: 14 additions & 5 deletions docs/concepts/resolver-architecture.rst
Original file line number Diff line number Diff line change
Expand Up @@ -212,22 +212,31 @@ Building the Sdist
Git checkouts can be prepared directly. Configured source resolvers
also select the sdist build method.

For source builds, ``source.build_sdist`` selects one of three methods:
For source builds, ``source.build_sdist`` selects one of four methods:

- ``pep517`` builds the sdist with the package's build backend.
Git checkout resolvers default to it.
- ``tarball`` repacks the prepared source without VCS metadata such as
``.git``. ``pypi-sdist`` and ``pypi-download`` default to it.
- ``tarball-build-dir`` repacks the wheel build directory without VCS
metadata such as ``.git``. ``pypi-sdist`` and ``pypi-download`` default
to it. This is the behaviour of the deprecated ``tarball`` value.
- ``tarball-root-dir`` repacks the whole prepared source tree without VCS
metadata such as ``.git``. Use it when the wheel builds from a
subdirectory but the sdist must ship the full tree, as with a native
library at the repository root.
- ``hook`` runs the package's ``build_sdist`` override hook, which can
generate files or choose which directory to pack.

``tarball`` remains a deprecated alias of ``tarball-build-dir``. Settings
loading normalizes it to ``tarball-build-dir`` and logs a warning. It will
be removed in the future.

The choice follows these rules:

- ``hook`` requires an installed, callable hook. If it is missing, the
build fails before downloading the source. A cached wheel needs no
hook, so it is used without the check.
- A configured ``pep517`` or ``tarball`` skips an installed hook with a
warning.
- A configured ``pep517`` or ``tarball-*`` strategy skips an installed
hook with a warning.
- Without ``source:``, the hook runs when present; otherwise the default
method is used.
- ``hook-sdist`` has no default and always needs an explicit setting.
Expand Down
16 changes: 14 additions & 2 deletions docs/reference/config-reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,26 @@ Source Resolver
.. autoclass:: BuildSDist

.. autoattribute:: pep517
.. autoattribute:: tarball_build_dir
.. autoattribute:: tarball_root_dir
.. autoattribute:: tarball
.. autoattribute:: hook

.. versionchanged:: 0.104.0

``pypi-sdist`` and ``pypi-download`` accept an explicit
``build_sdist: pep517``. Their default remains ``tarball`` because some
build backends require VCS metadata that source archives do not contain.
``build_sdist: pep517``. Their default remains tarball repacking because
some build backends require VCS metadata that source archives do not
contain.

.. versionchanged:: 0.105.0

``tarball`` is split into ``tarball-build-dir`` (pack the wheel build
directory, the previous behaviour) and ``tarball-root-dir`` (pack the
whole source tree). ``tarball`` remains a deprecated alias of
``tarball-build-dir`` and is normalized with a warning when settings
load. It will be removed in the future. ``pypi-sdist`` and
``pypi-download`` now default to ``tarball-build-dir``.

.. autoclass:: DownloadedSource
:members: path, kind
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/hooks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ Source hooks
build_sdist: hook

The hook must be installed. Without it, the build fails before
downloading the source. ``pep517`` and ``tarball`` ignore the hook and log a
downloading the source. ``pep517`` and ``tarball-*`` ignore the hook and log a
warning. Packages without ``source:`` use the hook when it exists.

Any profile that builds from source can select ``hook``.
Expand Down
2 changes: 1 addition & 1 deletion e2e/source_settings_hook_sdist/stevedore.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source:
provider: hook-sdist
build_sdist: tarball
build_sdist: tarball-build-dir
# GitHub archives have no git metadata for pbr to determine the version.
env:
PBR_VERSION: "${__version__}"
5 changes: 4 additions & 1 deletion src/fromager/packagesettings/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,10 @@ def from_mapping(
"""Load from a dict"""
package = Package(canonicalize_name(package, validate=True))
try:
return cls(name=package, has_config=has_config, **parsed)
return cls.model_validate(
{**parsed, "name": package, "has_config": has_config},
context={"package": package, "source": source},
)
except Exception as err:
raise RuntimeError(
f"{package}: failed to load settings (source: {source!r}): {err}"
Expand Down
49 changes: 43 additions & 6 deletions src/fromager/packagesettings/_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,24 @@ class BuildSDist(enum.StrEnum):
"""Source distribution build method."""

pep517 = "pep517"
tarball_build_dir = "tarball-build-dir"
"""Repack the package's wheel build directory.

.. versionadded:: 0.105.0
"""
tarball_root_dir = "tarball-root-dir"
"""Repack the whole prepared source tree without VCS metadata such as
``.git``.

.. versionadded:: 0.105.0
"""
tarball = "tarball"
"""Deprecated alias of ``tarball-build-dir``.

.. deprecated:: 0.105.0
Use ``tarball-build-dir`` or ``tarball-root-dir``. This value will be
removed in the future.
"""
hook = "hook"
"""Call the package's ``build_sdist`` override hook.

Expand Down Expand Up @@ -93,6 +110,25 @@ class AbstractResolver(pydantic.BaseModel):
resolves_prebuilt_wheel: typing.ClassVar[bool] = False
"""Does resolver return pre-built wheels?"""

@pydantic.field_validator("build_sdist", mode="after")
@classmethod
def _normalize_deprecated_tarball(
cls, value: BuildSDist | None, info: pydantic.ValidationInfo
) -> BuildSDist | None:
"""Replace the deprecated ``tarball`` alias and log a warning."""
if value is not BuildSDist.tarball:
return value
context = info.context or {}
package = context.get("package", "<n/a>")
logger.warning(
"%s: source.build_sdist 'tarball' is deprecated and will be "
"removed in the future; use 'tarball-build-dir' to repack the "
"build directory or 'tarball-root-dir' to repack the whole "
"source tree",
package,
)
return BuildSDist.tarball_build_dir

def resolver_provider(
self,
ctx: context.WorkContext,
Expand Down Expand Up @@ -261,7 +297,7 @@ class PyPISDistResolver(AbstractPyPIResolver):

# Tarball repacking is the safe default because some PEP 517 backends
# require VCS metadata to generate a correct sdist.
build_sdist: BuildSDist = BuildSDist.tarball
build_sdist: BuildSDist = BuildSDist.tarball_build_dir
download_kinds: typing.ClassVar[DownloadKindSet] = frozenset({DownloadKind.sdist})

def resolver_provider(
Expand Down Expand Up @@ -373,7 +409,7 @@ class PyPIDownloadResolver(AbstractPyPIResolver):
URL must contain '{version}' template string.
"""

build_sdist: BuildSDist = BuildSDist.tarball
build_sdist: BuildSDist = BuildSDist.tarball_build_dir
download_kinds: typing.ClassVar[DownloadKindSet] = frozenset(
{DownloadKind.sdist, DownloadKind.tarball}
)
Expand Down Expand Up @@ -963,15 +999,16 @@ class HookSDistResolver(AbstractHookResolver):
return ``DownloadedSource`` with a concrete kind, or a legacy archive
``Path`` treated as an sdist.
Without a download hook, the resolved URL is downloaded as an sdist.
Specify ``build_sdist`` explicitly: ``tarball`` to repack an existing
sdist, ``pep517`` to build from a git checkout, or ``hook`` to call
the package's ``build_sdist`` override.
Specify ``build_sdist`` explicitly: ``tarball-build-dir`` to repack an
existing sdist, ``tarball-root-dir`` to repack the whole source tree,
``pep517`` to build from a git checkout, or ``hook`` to call the
package's ``build_sdist`` override.

Example::

provider: hook-sdist
index_url: https://pypi.test/simple
build_sdist: tarball
build_sdist: tarball-build-dir
"""

provider: typing.Literal["hook-sdist"]
Expand Down
42 changes: 37 additions & 5 deletions src/fromager/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,10 +649,17 @@ def build_sdist(
"""Build a source distribution using the configured strategy or legacy hook.

``source.build_sdist: hook`` requires a package override. Configured
``pep517`` and ``tarball`` strategies take precedence over overrides.
``pep517`` and ``tarball-*`` strategies take precedence over overrides.
``tarball`` is a deprecated alias of ``tarball-build-dir`` and will be
removed in the future.

.. versionchanged:: 0.104.0
Configured source profiles can select the ``build_sdist`` override.

.. versionchanged:: 0.105.0
``tarball-build-dir`` and ``tarball-root-dir`` select the packed
directory. ``tarball`` is normalized to ``tarball-build-dir`` with a
deprecation warning when settings load.
"""
pbi = ctx.package_build_info(req)
source_resolver = pbi.source_resolver
Expand Down Expand Up @@ -702,7 +709,11 @@ def build_sdist(
sdist_root_dir=sdist_root_dir,
build_env=build_env,
)
elif strategy is packagesettings.BuildSDist.tarball:
elif strategy in (
packagesettings.BuildSDist.tarball_build_dir,
# deprecated alias, in case a resolver bypassed settings validation
packagesettings.BuildSDist.tarball,
):
sdist_file = default_build_sdist(
ctx=ctx,
extra_environ=extra_environ,
Expand All @@ -712,6 +723,17 @@ def build_sdist(
build_dir=build_dir,
build_env=build_env,
)
elif strategy is packagesettings.BuildSDist.tarball_root_dir:
sdist_file = default_build_sdist(
ctx=ctx,
extra_environ=extra_environ,
req=req,
version=version,
sdist_root_dir=sdist_root_dir,
build_dir=build_dir,
build_env=build_env,
sdist_base_dir=sdist_root_dir,
)
else:
raise ValueError(
f"{req.name}: unsupported source.build_sdist strategy {strategy!r}"
Expand Down Expand Up @@ -740,9 +762,14 @@ def default_build_sdist(
build_env: build_environment.BuildEnvironment,
build_dir: pathlib.Path,
*,
sdist_base_dir: pathlib.Path | None = None,
exclude_vcs: bool | None = None,
) -> pathlib.Path:
"""Repack *build_dir* as an sdist.
"""Repack *sdist_base_dir* as an sdist.

*sdist_base_dir* defaults to *build_dir*. Pass *sdist_root_dir* to pack
the whole source tree while still adding ``PKG-INFO`` to the wheel build
directory.

With *exclude_vcs*, VCS metadata such as ``.git`` is left out. By default,
exclude it when ``source:`` is configured, and retain it otherwise.
Expand All @@ -751,7 +778,12 @@ def default_build_sdist(
.. versionchanged:: 0.104.0
With ``exclude_vcs=None``, exclude VCS metadata when the effective
package or variant has ``source:`` configured; retain it otherwise.

.. versionchanged:: 0.105.0
Added *sdist_base_dir* to select the packed directory.
"""
if sdist_base_dir is None:
sdist_base_dir = build_dir
if exclude_vcs is None:
exclude_vcs = ctx.package_build_info(req).source_resolver is not None
# It seems like the "correct" way to do this would be to run the
Expand Down Expand Up @@ -780,8 +812,8 @@ def default_build_sdist(
with tarfile.open(sdist_filename, "x:gz", format=tarfile.PAX_FORMAT) as sdist:
tarballs.tar_reproducible(
tar=sdist,
basedir=build_dir,
prefix=build_dir.parent,
basedir=sdist_base_dir,
prefix=sdist_base_dir.parent,
exclude_vcs=exclude_vcs,
arcname_root=f"{dist_name}-{version}",
)
Expand Down
5 changes: 3 additions & 2 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,8 @@ def test_download_sequence_calls_configured_hook_download(
) -> None:
"""A hook source uses its normal download result during sequence replay."""
_configure_package(
tmp_context, "source:\n provider: hook-sdist\n build_sdist: tarball\n"
tmp_context,
"source:\n provider: hook-sdist\n build_sdist: tarball-build-dir\n",
)
url = "https://pkg.test/pkg-1.0.tar.gz"
build_order = tmp_path / "build-order.json"
Expand Down Expand Up @@ -1232,7 +1233,7 @@ def test_step_prepare_source_defaults_to_tarball_for_download_provider(
"source_yaml",
[
"source:\n provider: pypi-sdist\n",
"source:\n provider: hook-sdist\n build_sdist: tarball\n",
"source:\n provider: hook-sdist\n build_sdist: tarball-build-dir\n",
"source:\n provider: pypi-download\n"
" download_url: https://pkg.test/pkg-{version}.tar.gz\n"
" download_kind: tarball\n",
Expand Down
12 changes: 12 additions & 0 deletions tests/test_packagesettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,18 @@ def test_variant_info_with_source() -> None:
assert isinstance(vi.source, PyPIPrebuiltResolver)


def test_package_settings_deprecated_tarball_alias(
caplog: pytest.LogCaptureFixture,
) -> None:
"""Normalize the deprecated alias and name the package in the warning."""
yaml_str = "source:\n provider: pypi-sdist\n build_sdist: tarball\n"
ps = PackageSettings.from_string("test-source-pkg", yaml_str)

assert ps.source is not None
assert ps.source.build_sdist is BuildSDist.tarball_build_dir
assert "test-source-pkg: source.build_sdist 'tarball' is deprecated" in caplog.text


def test_package_settings_without_source() -> None:
"""``PackageSettings`` without ``source:`` has ``source=None``."""
ps = PackageSettings.from_default("test-pkg")
Expand Down
Loading
Loading