Skip to content

feat(pypi): add a standards compliant python_tag creator #3110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
16 changes: 11 additions & 5 deletions python/private/pypi/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ bzl_library(
bzl_library(
name = "pep508_env_bzl",
srcs = ["pep508_env.bzl"],
deps = [
"//python/private:version_bzl",
],
)

bzl_library(
Expand All @@ -263,11 +266,6 @@ bzl_library(
],
)

bzl_library(
name = "pep508_platform_bzl",
srcs = ["pep508_platform.bzl"],
)

bzl_library(
name = "pep508_requirement_bzl",
srcs = ["pep508_requirement.bzl"],
Expand Down Expand Up @@ -338,6 +336,14 @@ bzl_library(
],
)

bzl_library(
name = "python_tag_bzl",
srcs = ["python_tag.bzl"],
deps = [
"//python/private:version_bzl",
],
)

bzl_library(
name = "render_pkg_aliases_bzl",
srcs = ["render_pkg_aliases.bzl"],
Expand Down
7 changes: 4 additions & 3 deletions python/private/pypi/extension.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,12 @@ def _platforms(*, python_version, minor_mapping, config):

for platform, values in config.platforms.items():
key = "{}_{}".format(abi, platform)
platforms[key] = env(struct(
abi = abi,
platforms[key] = env(
env = values.env,
os = values.os_name,
arch = values.arch_name,
)) | values.env
python_version = python_version,
)
return platforms

def _create_whl_repos(
Expand Down
86 changes: 53 additions & 33 deletions python/private/pypi/pep508_env.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@
"""This module is for implementing PEP508 environment definition.
"""

load("//python/private:version.bzl", "version")

_DEFAULT = "//conditions:default"

# Here we store the aliases in the platform so that the users can specify any valid target in
# there.
_cpu_aliases = {
"arm": "aarch32",
"arm64": "aarch64",
}
_os_aliases = {
"macos": "osx",
}

# See https://stackoverflow.com/a/45125525
platform_machine_aliases = {
# These pairs mean the same hardware, but different values may be used
Expand Down Expand Up @@ -59,7 +73,7 @@ platform_machine_select_map = {
"@platforms//cpu:x86_64": "x86_64",
# The value is empty string if it cannot be determined:
# https://docs.python.org/3/library/platform.html#platform.machine
"//conditions:default": "",
_DEFAULT: "",
}

# Platform system returns results from the `uname` call.
Expand All @@ -73,7 +87,7 @@ _platform_system_values = {
"linux": "Linux",
"netbsd": "NetBSD",
"openbsd": "OpenBSD",
"osx": "Darwin",
"osx": "Darwin", # NOTE: macos is an alias to osx, we handle it through _os_aliases
"windows": "Windows",
}

Expand All @@ -83,7 +97,7 @@ platform_system_select_map = {
} | {
# The value is empty string if it cannot be determined:
# https://docs.python.org/3/library/platform.html#platform.machine
"//conditions:default": "",
_DEFAULT: "",
}

# The copy of SO [answer](https://stackoverflow.com/a/13874620) containing
Expand Down Expand Up @@ -123,72 +137,78 @@ _sys_platform_values = {
"ios": "ios",
"linux": "linux",
"openbsd": "openbsd",
"osx": "darwin",
"osx": "darwin", # NOTE: macos is an alias to osx, we handle it through _os_aliases
"wasi": "wasi",
"windows": "win32",
}

sys_platform_select_map = {
# These values are decided by the sys.platform docs.
"@platforms//os:{}".format(bazel_os): py_platform
for bazel_os, py_platform in _sys_platform_values.items()
} | {
# For lack of a better option, use empty string. No standard doc/spec
# about sys_platform value.
"//conditions:default": "",
_DEFAULT: "",
}

# The "java" value is documented, but with Jython defunct,
# shouldn't occur in practice.
# The os.name value is technically a property of the runtime, not the
# targetted runtime OS, but the distinction shouldn't matter if
# things are properly configured.
_os_name_values = {
"linux": "posix",
"osx": "posix",
"windows": "nt",
}

os_name_select_map = {
"@platforms//os:{}".format(bazel_os): py_os
for bazel_os, py_os in _os_name_values.items()
} | {
"//conditions:default": "posix",
"@platforms//os:windows": "nt",
_DEFAULT: "posix",
}

def env(target_platform, *, extra = None):
def _set_default(env, env_key, m, key):
"""Set the default value in the env if it is not already set."""
default = m.get(key, m[_DEFAULT])
env.setdefault(env_key, default)

def env(*, env = None, os, arch, python_version = "", extra = None):
"""Return an env target platform

NOTE: This is for use during the loading phase. For the analysis phase,
`env_marker_setting()` constructs the env dict.

Args:
target_platform: {type}`str` the target platform identifier, e.g.
`cp33_linux_aarch64`
env: {type}`str` the environment.
os: {type}`str` the OS name.
arch: {type}`str` the CPU name.
python_version: {type}`str` the full python version.
extra: {type}`str` the extra value to be added into the env.

Returns:
A dict that can be used as `env` in the marker evaluation.
"""
env = create_env()
env = env or {}
env = env | create_env()
if extra != None:
env["extra"] = extra

if target_platform.abi:
minor_version, _, micro_version = target_platform.abi[3:].partition(".")
micro_version = micro_version or "0"
env = env | {
"implementation_version": "3.{}.{}".format(minor_version, micro_version),
"python_full_version": "3.{}.{}".format(minor_version, micro_version),
"python_version": "3.{}".format(minor_version),
}
if target_platform.os and target_platform.arch:
os = target_platform.os
if python_version:
v = version.parse(python_version)
major = v.release[0]
minor = v.release[1]
micro = v.release[2] if len(v.release) > 2 else 0
env = env | {
"os_name": _os_name_values.get(os, ""),
"platform_machine": target_platform.arch,
"platform_system": _platform_system_values.get(os, ""),
"sys_platform": _sys_platform_values.get(os, ""),
"implementation_version": "{}.{}.{}".format(major, minor, micro),
"python_full_version": "{}.{}.{}".format(major, minor, micro),
"python_version": "{}.{}".format(major, minor),
}

if os:
os = "@platforms//os:{}".format(_os_aliases.get(os, os))
_set_default(env, "os_name", os_name_select_map, os)
_set_default(env, "platform_system", platform_system_select_map, os)
_set_default(env, "sys_platform", sys_platform_select_map, os)

if arch:
arch = "@platforms//cpu:{}".format(_cpu_aliases.get(arch, arch))
_set_default(env, "platform_machine", platform_machine_select_map, arch)

set_missing_env_defaults(env)

return env
Expand Down
57 changes: 0 additions & 57 deletions python/private/pypi/pep508_platform.bzl

This file was deleted.

41 changes: 41 additions & 0 deletions python/private/pypi/python_tag.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"A simple utility function to get the python_tag from the implementation name"

load("//python/private:version.bzl", "version")

# Taken from
# https://packaging.python.org/en/latest/specifications/platform-compatibility-tags/#python-tag
_PY_TAGS = {
# "py": Generic Python (does not require implementation-specific features)
"cpython": "cp",
"ironpython": "ip",
"jython": "jy",
"pypy": "pp",
"python": "py",
}
PY_TAG_GENERIC = "py"

def python_tag(implementation_name, python_version = ""):
"""Get the python_tag from the implementation_name.

Args:
implementation_name: {type}`str` the implementation name, e.g. "cpython"
python_version: {type}`str` a version who can be parsed using PEP440 compliant
parser.

Returns:
A {type}`str` that represents the python_tag with a version if the
python_version is given.
"""
if python_version:
v = version.parse(python_version, strict = True)
suffix = "{}{}".format(
v.release[0],
v.release[1] if len(v.release) > 1 else "",
)
else:
suffix = ""

return "{}{}".format(
_PY_TAGS.get(implementation_name, implementation_name),
suffix,
)
5 changes: 5 additions & 0 deletions tests/pypi/pep508/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
load(":deps_tests.bzl", "deps_test_suite")
load(":env_tests.bzl", "env_test_suite")
load(":evaluate_tests.bzl", "evaluate_test_suite")
load(":requirement_tests.bzl", "requirement_test_suite")

deps_test_suite(
name = "deps_tests",
)

env_test_suite(
name = "env_tests",
)

evaluate_test_suite(
name = "evaluate_tests",
)
Expand Down
69 changes: 69 additions & 0 deletions tests/pypi/pep508/env_tests.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"""Tests to check for env construction."""

load("@rules_testing//lib:test_suite.bzl", "test_suite")
load("//python/private/pypi:pep508_env.bzl", pep508_env = "env") # buildifier: disable=bzl-visibility

_tests = []

def _test_env_defaults(env):
got = pep508_env(os = "exotic", arch = "exotic", python_version = "3.1.1")
got.pop("_aliases")
env.expect.that_dict(got).contains_exactly({
"implementation_name": "cpython",
"implementation_version": "3.1.1",
"os_name": "posix",
"platform_machine": "",
"platform_python_implementation": "CPython",
"platform_release": "",
"platform_system": "",
"platform_version": "0",
"python_full_version": "3.1.1",
"python_version": "3.1",
"sys_platform": "",
})

_tests.append(_test_env_defaults)

def _test_env_freebsd(env):
got = pep508_env(os = "freebsd", arch = "arm64", python_version = "3.1.1")
got.pop("_aliases")
env.expect.that_dict(got).contains_exactly({
"implementation_name": "cpython",
"implementation_version": "3.1.1",
"os_name": "posix",
"platform_machine": "aarch64",
"platform_python_implementation": "CPython",
"platform_release": "",
"platform_system": "FreeBSD",
"platform_version": "0",
"python_full_version": "3.1.1",
"python_version": "3.1",
"sys_platform": "freebsd",
})

_tests.append(_test_env_freebsd)

def _test_env_macos(env):
got = pep508_env(os = "macos", arch = "arm64", python_version = "3.1.1")
got.pop("_aliases")
env.expect.that_dict(got).contains_exactly({
"implementation_name": "cpython",
"implementation_version": "3.1.1",
"os_name": "posix",
"platform_machine": "aarch64",
"platform_python_implementation": "CPython",
"platform_release": "",
"platform_system": "Darwin",
"platform_version": "0",
"python_full_version": "3.1.1",
"python_version": "3.1",
"sys_platform": "darwin",
})

_tests.append(_test_env_macos)

def env_test_suite(name): # buildifier: disable=function-docstring
test_suite(
name = name,
basic_tests = _tests,
)
Loading