Skip to content

Commit 799f9c3

Browse files
committed
wip: flag to evaluate dep specs
1 parent 7234dda commit 799f9c3

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
load("//python/private:toolchain_types.bzl", "TARGET_TOOLCHAIN_TYPE")
2+
load(":pep508_evaluate.bzl", "evaluate")
3+
4+
def _impl(ctx):
5+
env = {}
6+
7+
runtime = ctx.toolchains[TARGET_TOOLCHAIN_TYPE].py3_runtime
8+
if runtime.interpreter_version_info:
9+
version_info = runtime.interpreter_version_info
10+
env["python_version"] = "{v.major}.{v.minor}".format(v = version_info)
11+
full_version = format_full_version(version_info)
12+
env["python_full_version"] = full_version
13+
env["implementation_version"] = full_version
14+
else:
15+
env["python_version"] = get_flag(ctx.attr._python_version)
16+
full_version = get_flag(ctx.attr._python_full_version)
17+
env["python_full_version"] = full_version
18+
env["implementation_version"] = full_version
19+
20+
# We assume cpython if the toolchain doesn't specify because it's most
21+
# likely to be true.
22+
env["implementation_name"] = runtime.implementation_name or "cpython"
23+
24+
env["os_name"] = struct()
25+
env["platform_machine"] = struct()
26+
env["platform_python_implementation"] = struct()
27+
env["platform_release"] = struct()
28+
env["platform_system"] = struct()
29+
env["platform_version"] = struct()
30+
31+
if evalute(ctx.attr.expression, env):
32+
value = "yes"
33+
else:
34+
value = "no"
35+
return [config_common.FeatureFlagInfo(value = value)]
36+
37+
# Adapted from spec code at:
38+
# https://packaging.python.org/en/latest/specifications/dependency-specifiers/#environment-markers
39+
def format_full_info(info):
40+
kind = info.releaselevel
41+
if kind == "final":
42+
kind = ""
43+
serial = ""
44+
else:
45+
kind = kind[0] if kind else ""
46+
serial = str(info.serial) if info.serial else ""
47+
48+
return "{v.major}.{v.minor}.{v.micro}{kind}{serial}".format(
49+
v = version_info,
50+
kind = kind,
51+
serial = serial,
52+
)
53+
return version
54+
55+
pypa_dependency_specification = rule(
56+
implementation = _impl,
57+
attrs = {
58+
"expression": attt.string(),
59+
"_os_name": attr.label(),
60+
"_sys_platform_flag": attr.label(),
61+
"_platform_machine_flag": attr.label(),
62+
"_platform_python_implementation_flag": attr.label(),
63+
"_platform_release_flag": attr.label(),
64+
"_platform_system_flag": attr.label(),
65+
"_platform_version_flag": attr.label(),
66+
"_python_version_flag": attr.label(
67+
default = "//python/config_settings:_python_version_major_minor",
68+
),
69+
"_python_full_version_flag": attr.label(
70+
default = "//python/config_settings:python_version",
71+
),
72+
"_extra_flag": attr.label(),
73+
},
74+
toolchains = [
75+
TARGET_TOOLCHAIN_TYPE,
76+
],
77+
)
78+
79+
def _get_flag(t):
80+
if config_common.FeatureFlagInfo in t:
81+
return t[config_common.FeatureFlagInfo].value
82+
if BuildSettingInfo in t:
83+
return t[BuildSettingInfo].value
84+
fail("Should not occur: {} does not have necessary providers")

0 commit comments

Comments
 (0)