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
2 changes: 1 addition & 1 deletion python/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -779,8 +779,8 @@ bzl_library(
":stamp_impl",
":toolchain_types",
":transition_labels",
":version",
"//python/private/toolchains:shrubbery_toolchain",
"@pypackaging.bzl//:pypackaging",
],
)

Expand Down
48 changes: 46 additions & 2 deletions python/private/py_wheel.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

"Implementation of py_wheel rule"

load("@pypackaging.bzl", "pypackaging")
load(
"//python/private/toolchains:shrubbery_toolchain.bzl",
"get_shrubbery_tool",
Expand All @@ -29,7 +30,6 @@ load(
"TARGET_TOOLCHAIN_TYPE",
)
load(":transition_labels.bzl", "TRANSITION_LABELS")
load(":version.bzl", "version")

PyWheelInfo = provider(
doc = "Information about a wheel produced by `py_wheel`",
Expand Down Expand Up @@ -389,6 +389,50 @@ def _escape_filename_segment(segment):
escaped += "_"
return escaped

def _normalize_version(version_str):
"""Normalize the version as per PEP 440.

The stamping placeholders, e.g. `{BUILD_TIMESTAMP}`, are not part of PEP
440, so they are swapped with digits for the normalization and are
restored afterwards. A placeholder is assumed to expand to something that
makes sense in the place where it is used.

Args:
version_str: {type}`str` the version, which may have placeholders.

Returns:
{type}`str` the normalized version.
"""
placeholders = {}
for i, part in enumerate(version_str.split("{")[1:]):
name, sep, _ = part.partition("}")
if not sep:
fail("Unbalanced '{{' in the version: {}".format(version_str))

placeholder = "{" + name + "}"
if placeholder in placeholders:
continue

# Find the digits that can be swapped back. The leading digit ensures
# that they are not changed by the integer normalization.
for j in range(len(version_str) + 1):
digits = "9{}{}9".format("0" * j, i)
replaced = version_str.replace(placeholder, digits)
if replaced.replace(digits, placeholder) == version_str:
placeholders[placeholder] = digits
version_str = replaced
break

normalized = pypackaging.version.to_string(pypackaging.version.parse(version_str))

# Restore in the reverse order, because the digits of a later placeholder
# together with the digits next to them may look like the digits of an
# earlier one.
for placeholder, digits in reversed(placeholders.items()):
normalized = normalized.replace(digits, placeholder)

return normalized

def _replace_make_variables(flag, ctx):
"""Replace $(VERSION) etc make variables in flag"""
if "$" in flag:
Expand All @@ -409,7 +453,7 @@ def _py_wheel_impl(ctx):

filename_segments = [
_escape_filename_distribution_name(ctx.attr.distribution),
version.normalize(version_str),
_normalize_version(version_str),
_escape_filename_segment(python_tag),
_escape_filename_segment(abi),
_escape_filename_segment(platform),
Expand Down
40 changes: 40 additions & 0 deletions tests/py_wheel/py_wheel_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,46 @@ def _test_default_make_variables_impl(env, target):

_tests.append(_test_default_make_variables)

def _test_version_normalization(name):
rt_util.helper_target(
py_wheel,
name = name + "_subject",
distribution = "mydist_" + name,
version = "v01.0-DEV_{BUILD_TIMESTAMP}+Ubuntu_{BUILD_USER}",
)
analysis_test(
name = name,
impl = _test_version_normalization_impl,
target = name + "_subject",
)

def _test_version_normalization_impl(env, target):
env.expect.that_target(target).default_outputs().contains_exactly([
"{package}/mydist_{test_name}-1.0.dev{{BUILD_TIMESTAMP}}+ubuntu.{{BUILD_USER}}-py3-none-any.whl",
])

_tests.append(_test_version_normalization)

def _test_version_normalization_with_digits_before_placeholders(name):
rt_util.helper_target(
py_wheel,
name = name + "_subject",
distribution = "mydist_" + name,
version = "0.{BUILD_TIMESTAMP}.90{BUILD_USER}",
)
analysis_test(
name = name,
impl = _test_version_normalization_with_digits_before_placeholders_impl,
target = name + "_subject",
)

def _test_version_normalization_with_digits_before_placeholders_impl(env, target):
env.expect.that_target(target).default_outputs().contains_exactly([
"{package}/mydist_{test_name}-0.{{BUILD_TIMESTAMP}}.90{{BUILD_USER}}-py3-none-any.whl",
])

_tests.append(_test_version_normalization_with_digits_before_placeholders)

def _make_vars_impl(ctx):
return [platform_common.TemplateVariableInfo(ctx.attr.vars)]

Expand Down
Loading