Skip to content

build: versioningit: replace regex-based substitution with tomllib #6756

@bastimeyer

Description

@bastimeyer

When building Streamlink's sdist, it removes versioningit from its build-system.requires, so that versioningit is only required when building from git, as intended:

# The versioningit build-requirement gets removed from the source distribution,
# as the version string is already built into it (see the onbuild versioningit hook):
"versioningit >=2.0.0,<4", # disabled in sdist

This is currently done by using a regex in a custom versioningit build step that's run when building the sdist from git:

streamlink/pyproject.toml

Lines 185 to 187 in befff8e

[tool.versioningit.onbuild]
# When building the sdist or wheel, remove versioningit build-requirement and set the static version string
method = { module = "build_backend.onbuild", value = "onbuild" }

# Remove versioningit from ``build-system.requires`` in ``pyproject.toml``
if is_source:
with update_file(base_dir / "pyproject.toml") as cmproxy:
cmproxy.set(
re.sub(
r"^(\s*)(\"versioningit\b.+?\",).*$",
"\\1# \\2",
cmproxy.get(),
flags=re.MULTILINE,
count=1,
),
)
# Set the static version string that gets passed directly to setuptools via ``setup.py``.
# This is much easier compared to adding the ``project.version`` field and removing "version" from ``project.dynamic``
# in ``pyproject.toml``.
if is_source:
with update_file(base_dir / "setup.py") as cmproxy:
cmproxy.set(
re.sub(
r"^(\s*)# (version=\"\",).*$",
f'\\1version="{version}",',
cmproxy.get(),
flags=re.MULTILINE,
count=1,
),
)

It was done this way, because it's the simplest way without having to parse the pyproject.toml file. On older Python versions, this would've required the additional tomli dependency. With the drop of Python 3.10 next year though, tomllib will be available in the stdlib, so let's replace this "hacky" regex-based substitution then.

Then the version attribute also doesn't have to be set in setup.py and it can simply be set in pyproject.toml as a static value after removing the version item from project.dynamic. This is much cleaner.

# version="", # static version string template, uncommented and substituted by versioningit's onbuild hook

"version",

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions